From 81994cb2b8b32453a92bcb011830fcb884f22ff3 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Fri, 16 Apr 2021 20:33:10 +0200 Subject: Convert internal vcl bitmap formats transparency->alpha (II) (Second attempt at landing this) Image formats and graphics APIs use alpha, not transparency, so change our internal formats and data structures to work directly with alpha, so we don't need to modify data before we push it to graphics APIs. Add a couple of new Color constants to make the intention of the vcl code clearer. Notes (*) On macOS, tweaking the logic in CreateWithSalBitmapAndMask to more accurately reflect the requirements of the CGImageCreateWithMask function seems to fix some tests. (*) The vcl code does not properly support gradients with transparency. So the previous code was wrong, and this change is going to result in slightly different wrongness. Change-Id: I9e21c2e98d88ecfdc5f75db13bd1ffff7c38db98 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114168 Tested-by: Jenkins Reviewed-by: Patrick Luby Reviewed-by: Noel Grandin --- drawinglayer/source/tools/converters.cxx | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'drawinglayer/source/tools') diff --git a/drawinglayer/source/tools/converters.cxx b/drawinglayer/source/tools/converters.cxx index 001019cd9623..80ae15a3b6a9 100644 --- a/drawinglayer/source/tools/converters.cxx +++ b/drawinglayer/source/tools/converters.cxx @@ -95,7 +95,7 @@ AlphaMask implcreateAlphaMask(drawinglayer::primitive2d::Primitive2DContainer& r // prepare for mask creation pContent->SetMapMode(MapMode(MapUnit::MapPixel)); - // set alpha to all white (fully transparent) + // set transparency to all white (fully transparent) pContent->Erase(); basegfx::BColorModifierSharedPtr aBColorModifier; @@ -107,7 +107,7 @@ AlphaMask implcreateAlphaMask(drawinglayer::primitive2d::Primitive2DContainer& r } else { - // Embed primitives to paint them black + // Embed primitives to paint them black (fully opaque) aBColorModifier = std::make_shared(basegfx::BColor(0.0, 0.0, 0.0)); } @@ -123,7 +123,14 @@ AlphaMask implcreateAlphaMask(drawinglayer::primitive2d::Primitive2DContainer& r // get alpha channel from vdev pContent->EnableMapMode(false); const Point aEmptyPoint; - return AlphaMask(pContent->GetBitmap(aEmptyPoint, rSizePixel)); + + // Convert from transparency->alpha. + // FIXME in theory I should be able to directly construct alpha by using black as background + // and white as foreground, but that doesn't work for some reason. + Bitmap aContentBitmap = pContent->GetBitmap(aEmptyPoint, rSizePixel); + aContentBitmap.Invert(); + + return AlphaMask(aContentBitmap); } } @@ -257,12 +264,16 @@ BitmapEx convertToBitmapEx(drawinglayer::primitive2d::Primitive2DContainer&& rSe if (aAlpha.hasAlpha()) { // Need to correct content using known alpha to get to background-free - // RGBA result, usable e.g. in PNG export(s) or convert-to-bitmap - aRetval.RemoveBlendedStartColor(COL_WHITE, aAlpha); + // RGBA result, usable e.g. in PNG export(s) or convert-to-bitmap. + // Now that vcl supports bitmaps with an alpha channel, only apply + // this correction to bitmaps without an alpha channel. + if (pContent->GetBitCount() < 32) + aRetval.RemoveBlendedStartColor(COL_WHITE, aAlpha); + // return combined result + return BitmapEx(aRetval, aAlpha); } - - // return combined result - return BitmapEx(aRetval, aAlpha); + else + return BitmapEx(aRetval); } BitmapEx convertPrimitive2DContainerToBitmapEx(primitive2d::Primitive2DContainer&& rSequence, -- cgit