mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 05:09:34 -06:00
Merge pull request #10143 from Pokechu22/png-compression-level
Add option for setting the PNG zlib compression level
This commit is contained in:
@ -64,6 +64,8 @@ add_library(common
|
||||
HttpRequest.h
|
||||
Image.cpp
|
||||
Image.h
|
||||
ImageC.c
|
||||
ImageC.h
|
||||
IniFile.cpp
|
||||
IniFile.h
|
||||
Inline.h
|
||||
|
@ -10,6 +10,9 @@
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/IOFile.h"
|
||||
#include "Common/ImageC.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/Timer.h"
|
||||
|
||||
namespace Common
|
||||
{
|
||||
@ -39,23 +42,28 @@ bool LoadPNG(const std::vector<u8>& input, std::vector<u8>* data_out, u32* width
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
|
||||
u32 height, int stride)
|
||||
static void WriteCallback(png_structp png_ptr, png_bytep data, size_t length)
|
||||
{
|
||||
png_image png = {};
|
||||
png.version = PNG_IMAGE_VERSION;
|
||||
png.width = width;
|
||||
png.height = height;
|
||||
std::vector<u8>* buffer = static_cast<std::vector<u8>*>(png_get_io_ptr(png_ptr));
|
||||
buffer->insert(buffer->end(), data, data + length);
|
||||
}
|
||||
|
||||
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
|
||||
u32 height, int stride, int level)
|
||||
{
|
||||
Common::Timer timer;
|
||||
timer.Start();
|
||||
|
||||
size_t byte_per_pixel;
|
||||
int png_format;
|
||||
switch (format)
|
||||
{
|
||||
case ImageByteFormat::RGB:
|
||||
png.format = PNG_FORMAT_RGB;
|
||||
png_format = PNG_FORMAT_RGB;
|
||||
byte_per_pixel = 3;
|
||||
break;
|
||||
case ImageByteFormat::RGBA:
|
||||
png.format = PNG_FORMAT_RGBA;
|
||||
png_format = PNG_FORMAT_RGBA;
|
||||
byte_per_pixel = 4;
|
||||
break;
|
||||
default:
|
||||
@ -64,30 +72,47 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u
|
||||
|
||||
// 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)
|
||||
return false;
|
||||
std::vector<u8> buffer;
|
||||
buffer.reserve(byte_per_pixel * width * height);
|
||||
|
||||
File::IOFile outfile(path, "wb");
|
||||
if (!outfile)
|
||||
return false;
|
||||
return outfile.WriteBytes(buffer.data(), size);
|
||||
std::vector<const u8*> rows;
|
||||
rows.reserve(height);
|
||||
for (u32 row = 0; row < height; row++)
|
||||
{
|
||||
rows.push_back(&input[row * stride]);
|
||||
}
|
||||
|
||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
|
||||
bool success = false;
|
||||
if (png_ptr != nullptr && info_ptr != nullptr)
|
||||
{
|
||||
success = SavePNG0(png_ptr, info_ptr, png_format, width, height, level, &buffer, WriteCallback,
|
||||
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());
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ConvertRGBAToRGBAndSavePNG(const std::string& path, const u8* input, u32 width, u32 height,
|
||||
int stride)
|
||||
int stride, int level)
|
||||
{
|
||||
const std::vector<u8> data = RGBAToRGB(input, width, height, stride);
|
||||
return SavePNG(path, data.data(), ImageByteFormat::RGB, width, height);
|
||||
return SavePNG(path, data.data(), ImageByteFormat::RGB, width, height, width * 3, level);
|
||||
}
|
||||
|
||||
std::vector<u8> RGBAToRGB(const u8* input, u32 width, u32 height, int row_stride)
|
||||
|
@ -20,9 +20,9 @@ enum class ImageByteFormat
|
||||
};
|
||||
|
||||
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
|
||||
u32 height, int stride = 0);
|
||||
u32 height, int stride, int level = 6);
|
||||
bool ConvertRGBAToRGBAndSavePNG(const std::string& path, const u8* input, u32 width, u32 height,
|
||||
int stride = 0);
|
||||
int stride, int level);
|
||||
|
||||
std::vector<u8> RGBAToRGB(const u8* input, u32 width, u32 height, int row_stride = 0);
|
||||
|
||||
|
27
Source/Core/Common/ImageC.c
Normal file
27
Source/Core/Common/ImageC.c
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright 2021 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "Common/ImageC.h"
|
||||
|
||||
// Since libpng requires use of setjmp, and setjmp interacts poorly with destructors and other C++
|
||||
// features, this is in a separate C file.
|
||||
|
||||
// The main purpose of this function is to allow specifying the compression level, which
|
||||
// png_image_write_to_memory does not allow. row_pointers is not modified by libpng, but also isn't
|
||||
// const for some reason.
|
||||
bool SavePNG0(png_structp png_ptr, png_infop info_ptr, int png_format, png_uint_32 width,
|
||||
png_uint_32 height, int level, png_voidp io_ptr, png_rw_ptr write_fn,
|
||||
png_bytepp row_pointers)
|
||||
{
|
||||
if (setjmp(png_jmpbuf(png_ptr)) != 0)
|
||||
return false;
|
||||
|
||||
png_set_compression_level(png_ptr, level);
|
||||
png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_format, PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
||||
png_set_rows(png_ptr, info_ptr, row_pointers);
|
||||
png_set_write_fn(png_ptr, io_ptr, write_fn, NULL);
|
||||
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
|
||||
|
||||
return true;
|
||||
}
|
15
Source/Core/Common/ImageC.h
Normal file
15
Source/Core/Common/ImageC.h
Normal file
@ -0,0 +1,15 @@
|
||||
// Copyright 2021 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <png.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
#endif
|
||||
bool
|
||||
SavePNG0(png_structp png_ptr, png_infop info_ptr, int png_format, png_uint_32 width,
|
||||
png_uint_32 height, int level, png_voidp io_ptr, png_rw_ptr write_fn,
|
||||
png_bytepp row_pointers);
|
Reference in New Issue
Block a user