From 448fd8c392edd5e632eaaacecff823189c7c8376 Mon Sep 17 00:00:00 2001
From: Sarper Akdemir <sarper.akdemir@collabora.com>
Date: Thu, 1 Sep 2022 14:59:51 +0300
Subject: [PATCH] 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>
---
 oox/source/drawingml/fillproperties.cxx       |  24 ++++++++++++-
 vcl/source/graphic/UnoGraphic.cxx             |   4 +--
 4 files changed, 58 insertions(+), 3 deletions(-)
 create mode 100644 vcl/qa/cppunit/data/testColorChange-red-linear-gradient.png

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/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);
--
2.35.3

