Log libpng warnings and errors

This commit is contained in:
Pokechu22 2021-11-24 14:19:40 -08:00
parent 676c500cdc
commit 99e589cc98
3 changed files with 75 additions and 6 deletions

View File

@ -8,6 +8,7 @@
#include <png.h> #include <png.h>
#include "Common/Assert.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/IOFile.h" #include "Common/IOFile.h"
#include "Common/ImageC.h" #include "Common/ImageC.h"
@ -48,6 +49,18 @@ static void WriteCallback(png_structp png_ptr, png_bytep data, size_t length)
buffer->insert(buffer->end(), data, data + length); buffer->insert(buffer->end(), data, data + length);
} }
static void ErrorCallback(ErrorHandler* self, const char* msg)
{
std::vector<std::string>* errors = static_cast<std::vector<std::string>*>(self->error_list);
errors->emplace_back(msg);
}
static void WarningCallback(ErrorHandler* self, const char* msg)
{
std::vector<std::string>* warnings = static_cast<std::vector<std::string>*>(self->warning_list);
warnings->emplace_back(msg);
}
bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width, bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u32 width,
u32 height, int stride, int level) u32 height, int stride, int level)
{ {
@ -75,6 +88,14 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u
std::vector<u8> buffer; std::vector<u8> buffer;
buffer.reserve(byte_per_pixel * width * height); buffer.reserve(byte_per_pixel * width * height);
std::vector<std::string> warnings;
std::vector<std::string> errors;
ErrorHandler error_handler;
error_handler.error_list = &errors;
error_handler.warning_list = &warnings;
error_handler.StoreError = ErrorCallback;
error_handler.StoreWarning = WarningCallback;
std::vector<const u8*> rows; std::vector<const u8*> rows;
rows.reserve(height); rows.reserve(height);
for (u32 row = 0; row < height; row++) for (u32 row = 0; row < height; row++)
@ -82,7 +103,8 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u
rows.push_back(&input[row * stride]); rows.push_back(&input[row * stride]);
} }
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); png_structp png_ptr =
png_create_write_struct(PNG_LIBPNG_VER_STRING, &error_handler, PngError, PngWarning);
png_infop info_ptr = png_create_info_struct(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr);
bool success = false; bool success = false;
@ -103,6 +125,23 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u
timer.Stop(); timer.Stop();
INFO_LOG_FMT(FRAMEDUMP, "{} byte {} by {} image saved to {} at level {} in {}", buffer.size(), INFO_LOG_FMT(FRAMEDUMP, "{} byte {} by {} image saved to {} at level {} in {}", buffer.size(),
width, height, path, level, timer.GetTimeElapsedFormatted()); width, height, path, level, timer.GetTimeElapsedFormatted());
ASSERT(errors.size() == 0);
if (warnings.size() != 0)
{
WARN_LOG_FMT(FRAMEDUMP, "Saved with {} warnings:", warnings.size());
for (auto& warning : warnings)
WARN_LOG_FMT(FRAMEDUMP, "libpng warning: {}", warning);
}
}
else
{
ERROR_LOG_FMT(FRAMEDUMP,
"Failed to save {} by {} image to {} at level {}: {} warnings, {} errors", width,
height, path, level, warnings.size(), errors.size());
for (auto& error : errors)
ERROR_LOG_FMT(FRAMEDUMP, "libpng error: {}", error);
for (auto& warning : warnings)
WARN_LOG_FMT(FRAMEDUMP, "libpng warning: {}", warning);
} }
return success; return success;

View File

@ -25,3 +25,19 @@ bool SavePNG0(png_structp png_ptr, png_infop info_ptr, int png_format, png_uint_
return true; return true;
} }
// pngerror.c says: "Note that the error function MUST NOT return to the calling routine or serious
// problems will occur. The return method used in the default routine calls
// longjmp(png_ptr->jmp_buf_ptr, 1)"
void PngError(png_structp png_ptr, png_const_charp msg)
{
struct ErrorHandler* error_logger = (struct ErrorHandler*)png_get_error_ptr(png_ptr);
error_logger->StoreError(error_logger, msg);
png_longjmp(png_ptr, 1);
}
void PngWarning(png_structp png_ptr, png_const_charp msg)
{
struct ErrorHandler* error_logger = (struct ErrorHandler*)png_get_error_ptr(png_ptr);
error_logger->StoreWarning(error_logger, msg);
}

View File

@ -7,9 +7,23 @@
#include <stdbool.h> #include <stdbool.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
#endif
struct ErrorHandler
{
void* error_list; // std::vector<std::string>*
void* warning_list; // std::vector<std::string>*
void (*StoreError)(struct ErrorHandler* self, const char* msg);
void (*StoreWarning)(struct ErrorHandler* self, const char* msg);
};
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);
void PngError(png_structp png_ptr, png_const_charp msg);
void PngWarning(png_structp png_ptr, png_const_charp msg);
#ifdef __cplusplus
}
#endif #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);