Fix saving RGBA images

PNG_FORMAT_RGB and PNG_COLOR_TYPE_RGB both evaluate to 2, but PNG_FORMAT_RGBA evaluates to 3 while PNG_COLOR_TYPE_RGBA evaluates to 6; the bit indicating a palette is 1 while the bit indicating alpha is 4.
This commit is contained in:
Pokechu22 2021-11-24 14:39:07 -08:00
parent 99e589cc98
commit 8cf841ecc7
3 changed files with 8 additions and 7 deletions

View File

@ -68,18 +68,19 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u
timer.Start();
size_t byte_per_pixel;
int png_format;
int color_type;
switch (format)
{
case ImageByteFormat::RGB:
png_format = PNG_FORMAT_RGB;
color_type = PNG_COLOR_TYPE_RGB;
byte_per_pixel = 3;
break;
case ImageByteFormat::RGBA:
png_format = PNG_FORMAT_RGBA;
color_type = PNG_COLOR_TYPE_RGBA;
byte_per_pixel = 4;
break;
default:
ASSERT_MSG(FRAMEDUMP, false, "Invalid format %d", static_cast<int>(format));
return false;
}
@ -110,7 +111,7 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u
bool success = false;
if (png_ptr != nullptr && info_ptr != nullptr)
{
success = SavePNG0(png_ptr, info_ptr, png_format, width, height, level, &buffer, WriteCallback,
success = SavePNG0(png_ptr, info_ptr, color_type, width, height, level, &buffer, WriteCallback,
const_cast<u8**>(rows.data()));
}
png_destroy_write_struct(&png_ptr, &info_ptr);

View File

@ -9,7 +9,7 @@
// 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,
bool SavePNG0(png_structp png_ptr, png_infop info_ptr, int color_type, png_uint_32 width,
png_uint_32 height, int level, png_voidp io_ptr, png_rw_ptr write_fn,
png_bytepp row_pointers)
{
@ -17,7 +17,7 @@ bool SavePNG0(png_structp png_ptr, png_infop info_ptr, int png_format, png_uint_
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_set_IHDR(png_ptr, info_ptr, width, height, 8, color_type, 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);

View File

@ -17,7 +17,7 @@ struct ErrorHandler
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,
bool SavePNG0(png_structp png_ptr, png_infop info_ptr, int color_type, png_uint_32 width,
png_uint_32 height, int level, png_voidp io_ptr, png_rw_ptr write_fn,
png_bytepp row_pointers);