summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSarper Akdemir <sarper.akdemir@collabora.com>2022-09-01 14:59:51 +0300
committerAndras Timar <andras.timar@collabora.com>2022-09-16 22:39:02 +0200
commit448fd8c392edd5e632eaaacecff823189c7c8376 (patch)
tree49418e62cce8a9125b2d6ae398910e35b99c006e
parented44dd9adea70fd9dcf3d7a2f30e0c49a66aade4 (diff)
tdf#149670 fix color change api and adjust tolerance for ooxml
It appears at the start of Graphic::colorChange aBmpColorFrom & aBmpColorTo gets initialized with wrong colors. Instead of {R,G,B}, they get initialized with {B,G,R}. Instead of bitshifting use the ::Color constructor so that it is initialized correctly. For ooxml import adapt tolerance values of the image format in an attempt to get similar results on how the results appear in PowerPoint. Change-Id: I1fa901691512de82936dba0e47158b7e0ca2223e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139203 Tested-by: Jenkins Reviewed-by: Miklos Vajna <vmiklos@collabora.com> (cherry picked from commit 2b902b6203a87bdca7856e17a9c0fcc403de4264) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139421 Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org> (cherry picked from commit de325e4b603d6b57fa6b46021a1b4c83e2d44e82) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139673 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Andras Timar <andras.timar@collabora.com>
-rw-r--r--oox/source/drawingml/fillproperties.cxx24
-rw-r--r--vcl/qa/cppunit/GraphicTest.cxx33
-rw-r--r--vcl/qa/cppunit/data/testColorChange-red-linear-gradient.pngbin0 -> 9341 bytes
-rw-r--r--vcl/source/graphic/UnoGraphic.cxx4
4 files changed, 58 insertions, 3 deletions
diff --git a/oox/source/drawingml/fillproperties.cxx b/oox/source/drawingml/fillproperties.cxx
index 0ef86bc75f5c..3fb6b4ca372e 100644
--- a/oox/source/drawingml/fillproperties.cxx
+++ b/oox/source/drawingml/fillproperties.cxx
@@ -197,9 +197,31 @@ Reference< XGraphic > lclCheckAndApplyChangeColorTransform(const BlipFillPropert
sal_Int16 nToTransparence = aBlipProps.maColorChangeTo.getTransparency();
sal_Int8 nToAlpha = static_cast< sal_Int8 >( (100 - nToTransparence) * 2.55 );
+ sal_uInt8 nTolerance = 9;
+ Graphic aGraphic{ xGraphic };
+ if( aGraphic.IsGfxLink() )
+ {
+ // tdf#149670: Try to guess tolerance depending on image format
+ switch (aGraphic.GetGfxLink().GetType())
+ {
+ case GfxLinkType::NativeJpg:
+ nTolerance = 15;
+ break;
+ case GfxLinkType::NativePng:
+ case GfxLinkType::NativeTif:
+ nTolerance = 1;
+ break;
+ case GfxLinkType::NativeBmp:
+ nTolerance = 0;
+ break;
+ default:
+ break;
+ }
+ }
+
uno::Reference<graphic::XGraphicTransformer> xTransformer(aBlipProps.mxFillGraphic, uno::UNO_QUERY);
if (xTransformer.is())
- return xTransformer->colorChange(xGraphic, sal_Int32(nFromColor), 9, sal_Int32(nToColor), nToAlpha);
+ return xTransformer->colorChange(xGraphic, sal_Int32(nFromColor), nTolerance, sal_Int32(nToColor), nToAlpha);
}
}
return xGraphic;
diff --git a/vcl/qa/cppunit/GraphicTest.cxx b/vcl/qa/cppunit/GraphicTest.cxx
index 2e398c544c79..40fb316bd93e 100644
--- a/vcl/qa/cppunit/GraphicTest.cxx
+++ b/vcl/qa/cppunit/GraphicTest.cxx
@@ -9,6 +9,8 @@
#include <sal/config.h>
#include <config_oox.h>
+#include <com/sun/star/graphic/XGraphic.hpp>
+#include <com/sun/star/graphic/XGraphicTransformer.hpp>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
@@ -84,6 +86,7 @@ private:
void testLoadWEBP();
void testAvailableThreaded();
+ void testColorChangeToTransparent();
CPPUNIT_TEST_SUITE(GraphicTest);
CPPUNIT_TEST(testUnloadedGraphic);
@@ -122,6 +125,7 @@ private:
CPPUNIT_TEST(testLoadWEBP);
CPPUNIT_TEST(testAvailableThreaded);
+ CPPUNIT_TEST(testColorChangeToTransparent);
CPPUNIT_TEST_SUITE_END();
};
@@ -1344,6 +1348,35 @@ void GraphicTest::testAvailableThreaded()
}
}
+void GraphicTest::testColorChangeToTransparent()
+{
+ Graphic aGraphic = importUnloadedGraphic(u"testColorChange-red-linear-gradient.png");
+
+ auto xGraphic = aGraphic.GetXGraphic();
+ uno::Reference<graphic::XGraphicTransformer> xGraphicTransformer{ xGraphic, uno::UNO_QUERY };
+ ::Color nColorFrom{ ColorTransparency, 0x00, 0xFF, 0x00, 0x00 };
+ ::Color nColorTo{ ColorTransparency, 0xFF, 0xFF, 0x00, 0x00 };
+ sal_uInt8 nTolerance{ 15 };
+
+ auto xGraphicAfter = xGraphicTransformer->colorChange(
+ xGraphic, static_cast<sal_Int32>(nColorFrom), nTolerance, static_cast<sal_Int32>(nColorTo),
+ static_cast<sal_Int8>(nColorTo.GetAlpha()));
+
+ Graphic aGraphicAfter{ xGraphicAfter };
+ const BitmapEx& rBitmapAfter = aGraphicAfter.GetBitmapExRef();
+ const BitmapEx& rBitmapBefore = aGraphic.GetBitmapExRef();
+ // Without the accompanying fix in place, this test would have failed with:
+ // - Expected: rgba[ff000000]
+ // - Actual : rgba[f00000ff]
+ // i.e. the color change to transparent didn't apply correctly
+ CPPUNIT_ASSERT_EQUAL(nColorTo, rBitmapAfter.GetPixelColor(386, 140));
+
+ // Test if color stayed same on 410,140
+ // colorChange with nTolerance 15 shouldn't change this pixel.
+ CPPUNIT_ASSERT_EQUAL(rBitmapBefore.GetPixelColor(410, 140),
+ rBitmapAfter.GetPixelColor(410, 140));
+}
+
} // namespace
CPPUNIT_TEST_SUITE_REGISTRATION(GraphicTest);
diff --git a/vcl/qa/cppunit/data/testColorChange-red-linear-gradient.png b/vcl/qa/cppunit/data/testColorChange-red-linear-gradient.png
new file mode 100644
index 000000000000..6f2a9d0abfbe
--- /dev/null
+++ b/vcl/qa/cppunit/data/testColorChange-red-linear-gradient.png
Binary files differ
diff --git a/vcl/source/graphic/UnoGraphic.cxx b/vcl/source/graphic/UnoGraphic.cxx
index e415de1523d3..4d5f42dfcf82 100644
--- a/vcl/source/graphic/UnoGraphic.cxx
+++ b/vcl/source/graphic/UnoGraphic.cxx
@@ -195,8 +195,8 @@ uno::Reference< graphic::XGraphic > SAL_CALL Graphic::colorChange(
::Graphic aGraphic(rxGraphic);
::Graphic aReturnGraphic;
- BitmapColor aBmpColorFrom(static_cast< sal_uInt8 >(nColorFrom), static_cast< sal_uInt8 >(nColorFrom >> 8), static_cast< sal_uInt8 >(nColorFrom >> 16));
- BitmapColor aBmpColorTo( static_cast< sal_uInt8 >(nColorTo), static_cast< sal_uInt8 >(nColorTo >> 8), static_cast< sal_uInt8 >(nColorTo >> 16));
+ BitmapColor aBmpColorFrom(Color(ColorTransparency, static_cast<sal_uInt32>(nColorFrom)));
+ BitmapColor aBmpColorTo(Color(ColorTransparency, static_cast<sal_uInt32>(nColorTo)));
Color aColorFrom(aBmpColorFrom);
Color aColorTo(aBmpColorTo);