diff options
-rw-r--r-- | src/XPM.cxx | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/XPM.cxx b/src/XPM.cxx index 31d87a06d..42c4dd96a 100644 --- a/src/XPM.cxx +++ b/src/XPM.cxx @@ -264,15 +264,23 @@ void RGBAImage::SetPixel(int x, int y, ColourRGBA colour) noexcept { pixel[3] = colour.GetAlpha(); } +namespace { + +unsigned char AlphaMultiplied(unsigned char value, unsigned char alpha) { + return (value * alpha / UCHAR_MAX) & 0xffU; +} + +} + // Transform a block of pixels from RGBA to BGRA with premultiplied alpha. // Used for DrawRGBAImage on some platforms. void RGBAImage::BGRAFromRGBA(unsigned char *pixelsBGRA, const unsigned char *pixelsRGBA, size_t count) noexcept { for (size_t i = 0; i < count; i++) { const unsigned char alpha = pixelsRGBA[3]; // Input is RGBA, output is BGRA with premultiplied alpha - pixelsBGRA[2] = pixelsRGBA[0] * alpha / UCHAR_MAX; - pixelsBGRA[1] = pixelsRGBA[1] * alpha / UCHAR_MAX; - pixelsBGRA[0] = pixelsRGBA[2] * alpha / UCHAR_MAX; + pixelsBGRA[2] = AlphaMultiplied(pixelsRGBA[0], alpha); + pixelsBGRA[1] = AlphaMultiplied(pixelsRGBA[1], alpha); + pixelsBGRA[0] = AlphaMultiplied(pixelsRGBA[2], alpha); pixelsBGRA[3] = alpha; pixelsRGBA += bytesPerPixel; pixelsBGRA += bytesPerPixel; |