diff options
-rw-r--r-- | include/vcl/BitmapColorReplaceFilter.hxx | 44 | ||||
-rw-r--r-- | vcl/Library_vcl.mk | 1 | ||||
-rw-r--r-- | vcl/source/bitmap/BitmapColorReplaceFilter.cxx | 45 |
3 files changed, 90 insertions, 0 deletions
diff --git a/include/vcl/BitmapColorReplaceFilter.hxx b/include/vcl/BitmapColorReplaceFilter.hxx new file mode 100644 index 000000000000..caeac83d0fda --- /dev/null +++ b/include/vcl/BitmapColorReplaceFilter.hxx @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#ifndef INCLUDED_INCLUDE_VCL_BITMAPCOLORREPLACEFILTER_HXX +#define INCLUDED_INCLUDE_VCL_BITMAPCOLORREPLACEFILTER_HXX + +#include <vcl/BitmapFilter.hxx> + +class VCL_DLLPUBLIC BitmapColorReplaceFilter final : public BitmapFilter +{ +public: + /** Replaces a color with another by changing pixels, without shortcuts like modifying palette + (that is how it's different from using Bitmap::Replace). + + @param cReplaceWhat + Color that will be replaced. + + @param cReplaceTo + New color that will replace cReplaceWhat. + + */ + BitmapColorReplaceFilter(const Color& cReplaceWhat, const Color& cReplaceTo) + : m_aReplaceWhat(cReplaceWhat) + , m_aReplaceTo(cReplaceTo) + { + } + + virtual BitmapEx execute(BitmapEx const& rBitmapEx) const override; + +private: + Color m_aReplaceWhat; + Color m_aReplaceTo; +}; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index ffe065b24fff..def727918433 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -332,6 +332,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/bitmap/bitmapfilter \ vcl/source/bitmap/BitmapAlphaClampFilter \ vcl/source/bitmap/BitmapBasicMorphologyFilter \ + vcl/source/bitmap/BitmapColorReplaceFilter \ vcl/source/bitmap/BitmapMonochromeFilter \ vcl/source/bitmap/BitmapSmoothenFilter \ vcl/source/bitmap/BitmapLightenFilter \ diff --git a/vcl/source/bitmap/BitmapColorReplaceFilter.cxx b/vcl/source/bitmap/BitmapColorReplaceFilter.cxx new file mode 100644 index 000000000000..151cfbfbb48b --- /dev/null +++ b/vcl/source/bitmap/BitmapColorReplaceFilter.cxx @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <vcl/bitmap.hxx> +#include <vcl/bitmapaccess.hxx> +#include <vcl/BitmapColorReplaceFilter.hxx> +#include <vcl/bitmapex.hxx> + +#include <bitmapwriteaccess.hxx> + +BitmapEx BitmapColorReplaceFilter::execute(BitmapEx const& aBitmapEx) const +{ + Bitmap aBitmap = aBitmapEx.GetBitmap(); + + if (BitmapScopedWriteAccess pWriteAcc{ aBitmap }) + { + const BitmapColor aReplaceWhat(pWriteAcc->GetBestMatchingColor(m_aReplaceWhat)); + const BitmapColor aReplaceTo(pWriteAcc->GetBestMatchingColor(m_aReplaceTo)); + const long nWidth = pWriteAcc->Width(); + const long nHeight = pWriteAcc->Height(); + + for (long nY = 0; nY < nHeight; nY++) + { + Scanline pScanline = pWriteAcc->GetScanline(nY); + for (long nX = 0; nX < nWidth; nX++) + { + if (pWriteAcc->GetPixelFromData(pScanline, nX) == aReplaceWhat) + { + pWriteAcc->SetPixelOnData(pScanline, nX, aReplaceTo); + } + } + } + } + + return BitmapEx(aBitmap); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |