diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2021-04-16 20:33:10 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-07-25 08:38:12 +0200 |
commit | 81994cb2b8b32453a92bcb011830fcb884f22ff3 (patch) | |
tree | ae1750e92421ad2e0ec3f50351c3be6581841598 /drawinglayer | |
parent | dabedcaf27b0af1e38a611b8d8e48444f848e01d (diff) |
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 <plubius@neooffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'drawinglayer')
5 files changed, 26 insertions, 11 deletions
diff --git a/drawinglayer/source/primitive2d/GlowSoftEgdeShadowTools.cxx b/drawinglayer/source/primitive2d/GlowSoftEgdeShadowTools.cxx index 3c45fdd030f4..9bbdf7176935 100644 --- a/drawinglayer/source/primitive2d/GlowSoftEgdeShadowTools.cxx +++ b/drawinglayer/source/primitive2d/GlowSoftEgdeShadowTools.cxx @@ -58,10 +58,10 @@ AlphaMask ProcessAndBlurAlphaMask(const Bitmap& rMask, double fErodeDilateRadius else if (fErodeDilateRadius < 0) BitmapFilter::Filter(mask, BitmapErodeFilter(-fErodeDilateRadius, 0xFF)); - if (nTransparency) + if (nTransparency != 255) { const Color aTransparency(nTransparency, nTransparency, nTransparency); - mask.Replace(COL_BLACK, aTransparency); + mask.Replace(COL_WHITE, aTransparency); } // We need 8-bit grey mask for blurring @@ -72,6 +72,8 @@ AlphaMask ProcessAndBlurAlphaMask(const Bitmap& rMask, double fErodeDilateRadius mask.Scale(rMask.GetSizePixel()); + mask.Invert(); // convert transparency to alpha + return AlphaMask(mask.GetBitmap()); } diff --git a/drawinglayer/source/primitive2d/discreteshadowprimitive2d.cxx b/drawinglayer/source/primitive2d/discreteshadowprimitive2d.cxx index 4a64da368a2f..89c4335bb0b3 100644 --- a/drawinglayer/source/primitive2d/discreteshadowprimitive2d.cxx +++ b/drawinglayer/source/primitive2d/discreteshadowprimitive2d.cxx @@ -32,6 +32,7 @@ namespace drawinglayer::primitive2d DiscreteShadow::DiscreteShadow(const BitmapEx& rBitmapEx) : maBitmapEx(rBitmapEx) { + maBitmapEx.Invert(); // convert transparency to alpha const Size& rBitmapSize = getBitmapEx().GetSizePixel(); if(rBitmapSize.Width() != rBitmapSize.Height() || rBitmapSize.Width() < 7) diff --git a/drawinglayer/source/processor2d/d2dpixelprocessor2d.cxx b/drawinglayer/source/processor2d/d2dpixelprocessor2d.cxx index 783060c2be4c..7671e0c29a05 100644 --- a/drawinglayer/source/processor2d/d2dpixelprocessor2d.cxx +++ b/drawinglayer/source/processor2d/d2dpixelprocessor2d.cxx @@ -410,7 +410,7 @@ sal::systools::COMReference<ID2D1Bitmap> createB2DBitmap(const BitmapEx& rBitmap { const BitmapColor aColor(pReadAccess->GetColor(y, x)); const BitmapColor aAlpha(pAlphaReadAccess->GetColor(y, x)); - const sal_uInt16 nAlpha(255 - aAlpha.GetRed()); + const sal_uInt16 nAlpha(aAlpha.GetRed()); *pTarget++ = sal_uInt32(BitmapColor( ColorAlpha, sal_uInt8((sal_uInt16(aColor.GetRed()) * nAlpha) >> 8), diff --git a/drawinglayer/source/processor2d/vclhelperbufferdevice.cxx b/drawinglayer/source/processor2d/vclhelperbufferdevice.cxx index c76225194dd7..f90049bf8773 100644 --- a/drawinglayer/source/processor2d/vclhelperbufferdevice.cxx +++ b/drawinglayer/source/processor2d/vclhelperbufferdevice.cxx @@ -476,6 +476,7 @@ void impBufferDevice::paint(double fTrans) { mpAlpha->EnableMapMode(false); AlphaMask aAlphaMask(mpAlpha->GetBitmap(aEmptyPoint, aSizePixel)); + aAlphaMask.Invert(); // convert transparency to alpha #ifdef DBG_UTIL if (!sDumpPath.isEmpty() && bDoSaveForVisualControl) 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::BColorModifier_replace>(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, |