diff --git a/graphics/managed_surface.cpp b/graphics/managed_surface.cpp index f076ca8acb6..467df2ae585 100644 --- a/graphics/managed_surface.cpp +++ b/graphics/managed_surface.cpp @@ -245,6 +245,20 @@ void ManagedSurface::copyFrom(const Surface &surf) { } } +uint32 ManagedSurface::convertTransparentColor(const ManagedSurface &surf, const PixelFormat &dstFmt) const { + if (surf.format == dstFmt) { + return surf._transparentColor; + } else if (surf.format.isCLUT8()) { + byte r, g, b; + surf._palette->get(surf._transparentColor, r, g, b); + return dstFmt.RGBToColor(r, g, b); + } else { + byte a, r, g, b; + surf.format.colorToARGB(surf._transparentColor, a, r, g, b); + return dstFmt.ARGBToColor(a, r, g, b); + } +} + void ManagedSurface::convertFrom(const ManagedSurface &surf, const PixelFormat &fmt) { // Surface::copyFrom frees pixel pointer so let's free up ManagedSurface to be coherent free(); @@ -258,7 +272,7 @@ void ManagedSurface::convertFrom(const ManagedSurface &surf, const PixelFormat & // Copy miscellaneous properties _transparentColorSet = surf._transparentColorSet; - _transparentColor = surf._transparentColor; + _transparentColor = convertTransparentColor(surf, fmt); _palette = (fmt.isCLUT8() && surf._palette) ? new Palette(*surf._palette) : nullptr; } @@ -282,6 +296,16 @@ void ManagedSurface::convertFrom(const Surface &surf, const PixelFormat &fmt) { } } +void ManagedSurface::convertToInPlace(const PixelFormat &dstFormat) { + // Convert miscellaneous properties + _transparentColor = convertTransparentColor(*this, dstFormat); + + if (_palette) + _innerSurface.convertToInPlace(dstFormat, _palette->data(), _palette->size()); + else + _innerSurface.convertToInPlace(dstFormat); +} + Graphics::ManagedSurface *ManagedSurface::scale(int16 newWidth, int16 newHeight, bool filtering) const { Graphics::ManagedSurface *target = new Graphics::ManagedSurface(); diff --git a/graphics/managed_surface.h b/graphics/managed_surface.h index a741575527c..96e3c8a839c 100644 --- a/graphics/managed_surface.h +++ b/graphics/managed_surface.h @@ -120,6 +120,8 @@ protected: const Common::Rect &destRect, const int flipping, const uint colorMod, const TSpriteBlendMode blend, const AlphaType alphaType); + uint32 convertTransparentColor(const ManagedSurface &surf, const PixelFormat &dstFmt) const; + public: /** * Clip the given source bounds so the passed destBounds will be entirely on-screen. @@ -994,9 +996,7 @@ public: * * @param dstFormat The desired format. */ - void convertToInPlace(const PixelFormat &dstFormat) { - _innerSurface.convertToInPlace(dstFormat); - } + void convertToInPlace(const PixelFormat &dstFormat); /** * Convert the data to another pixel format.