From dd75517d61f93d6fcf31c7d83f5b2d89d3228756 Mon Sep 17 00:00:00 2001 From: lb_ii Date: Sun, 27 Jun 2021 21:16:43 +0300 Subject: [PATCH] IMAGE: Use 0-bit alpha for non-transparent PNGs --- image/png.cpp | 12 +++++++----- image/png.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/image/png.cpp b/image/png.cpp index fc4134b2e6c..b8ef5d3f96a 100644 --- a/image/png.cpp +++ b/image/png.cpp @@ -63,11 +63,11 @@ void PNGDecoder::destroy() { _palette = NULL; } -Graphics::PixelFormat PNGDecoder::getByteOrderRgbaPixelFormat() const { +Graphics::PixelFormat PNGDecoder::getByteOrderRgbaPixelFormat(bool isAlpha) const { #ifdef SCUMM_BIG_ENDIAN - return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0); + return Graphics::PixelFormat(4, 8, 8, 8, isAlpha ? 8 : 0, 24, 16, 8, 0); #else - return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24); + return Graphics::PixelFormat(4, 8, 8, 8, isAlpha ? 8 : 0, 0, 8, 16, 24); #endif } @@ -201,7 +201,7 @@ bool PNGDecoder::loadStream(Common::SeekableReadStream &stream) { } _outputSurface->create(width, height, - hasRgbaPalette ? getByteOrderRgbaPixelFormat() : Graphics::PixelFormat::createFormatCLUT8()); + hasRgbaPalette ? getByteOrderRgbaPixelFormat(true) : Graphics::PixelFormat::createFormatCLUT8()); png_set_packing(pngPtr); if (hasRgbaPalette) { @@ -219,11 +219,13 @@ bool PNGDecoder::loadStream(Common::SeekableReadStream &stream) { _palette = nullptr; } } else { + bool isAlpha = (colorType & PNG_COLOR_MASK_ALPHA); if (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) { + isAlpha = true; png_set_expand(pngPtr); } - _outputSurface->create(width, height, getByteOrderRgbaPixelFormat()); + _outputSurface->create(width, height, getByteOrderRgbaPixelFormat(isAlpha)); if (!_outputSurface->getPixels()) { error("Could not allocate memory for output image."); } diff --git a/image/png.h b/image/png.h index 59f0aa2dc67..f6057587251 100644 --- a/image/png.h +++ b/image/png.h @@ -67,7 +67,7 @@ public: void setSkipSignature(bool skip) { _skipSignature = skip; } void setKeepTransparencyPaletted(bool keep) { _keepTransparencyPaletted = keep; } private: - Graphics::PixelFormat getByteOrderRgbaPixelFormat() const; + Graphics::PixelFormat getByteOrderRgbaPixelFormat(bool isAlpha) const; byte *_palette; uint16 _paletteColorCount;