From 4e5f6f01a1b8dfd2dc07f760e79a0ced6d4836a5 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 15 Feb 2020 12:47:29 +1100 Subject: Backport: Extract image conversion from RGBA to BGRA premultiplied into common function. Backport of changeset 7987:beeac488af4d. --- gtk/PlatGTK.cxx | 15 +++++---------- src/XPM.cxx | 15 +++++++++++++++ src/XPM.h | 2 ++ win32/PlatWin.cxx | 27 ++++++--------------------- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/gtk/PlatGTK.cxx b/gtk/PlatGTK.cxx index 97009d4ff..f884ac565 100755 --- a/gtk/PlatGTK.cxx +++ b/gtk/PlatGTK.cxx @@ -597,18 +597,13 @@ void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height, const unsi rc.top += (rc.Height() - height) / 2; rc.bottom = rc.top + height; - int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); + const int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); const int ucs = stride * height; std::vector image(ucs); - for (int iy=0; iy(alpha); } +// 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 / 255; + pixelsBGRA[1] = pixelsRGBA[1] * alpha / 255; + pixelsBGRA[0] = pixelsRGBA[2] * alpha / 255; + pixelsBGRA[3] = alpha; + pixelsRGBA += bytesPerPixel; + pixelsBGRA += bytesPerPixel; + } +} + RGBAImageSet::RGBAImageSet() : height(-1), width(-1) { } diff --git a/src/XPM.h b/src/XPM.h index 9ff612d64..36e541a59 100644 --- a/src/XPM.h +++ b/src/XPM.h @@ -50,6 +50,7 @@ class RGBAImage { float scale; std::vector pixelBytes; public: + static constexpr size_t bytesPerPixel = 4; RGBAImage(int width_, int height_, float scale_, const unsigned char *pixels_); explicit RGBAImage(const XPM &xpm); RGBAImage(const RGBAImage &) = default; @@ -65,6 +66,7 @@ public: int CountBytes() const; const unsigned char *Pixels() const; void SetPixel(int x, int y, ColourDesired colour, int alpha); + static void BGRAFromRGBA(unsigned char *pixelsBGRA, const unsigned char *pixelsRGBA, size_t count) noexcept; }; /** diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index 3c5a5adf3..27d40fe22 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -784,15 +784,10 @@ void SurfaceGDI::DrawRGBAImage(PRectangle rc, int width, int height, const unsig HBITMAP hbmOld = SelectBitmap(hMemDC, hbmMem); for (int y=height-1; y>=0; y--) { - for (int x=0; x(image) + (y*width+x) * 4; - const unsigned char alpha = pixelsImage[3]; - // Input is RGBA, output is BGRA with premultiplied alpha - pixel[2] = static_cast((*pixelsImage++) * alpha / 255); - pixel[1] = static_cast((*pixelsImage++) * alpha / 255); - pixel[0] = static_cast((*pixelsImage++) * alpha / 255); - pixel[3] = *pixelsImage++; - } + // Bits flipped vertically + unsigned char *pixel = static_cast(image) + RGBAImage::bytesPerPixel * y * width; + RGBAImage::BGRAFromRGBA(pixel, pixelsImage, width); + pixelsImage += RGBAImage::bytesPerPixel * width; } const BLENDFUNCTION merge = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; @@ -1436,18 +1431,8 @@ void SurfaceD2D::DrawRGBAImage(PRectangle rc, int width, int height, const unsig rc.top += std::floor((rc.Height() - height) / 2); rc.bottom = rc.top + height; - std::vector image(height * width * 4); - for (int yPixel=0; yPixel image(RGBAImage::bytesPerPixel * height * width); + RGBAImage::BGRAFromRGBA(image.data(), pixelsImage, static_cast(height) * width); ID2D1Bitmap *bitmap = nullptr; const D2D1_SIZE_U size = D2D1::SizeU(width, height); -- cgit v1.2.3