LibreOffice Module vcl (master)  1
BitmapDuoToneFilter.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  */
10 
11 #include <vcl/bitmap.hxx>
12 #include <vcl/bitmapex.hxx>
14 
16 
17 static sal_uInt8 lcl_getDuotoneColorComponent(sal_uInt8 base, sal_uInt16 color1, sal_uInt16 color2)
18 {
19  color2 = color2 * base / 0xFF;
20  color1 = color1 * (0xFF - base) / 0xFF;
21 
22  return static_cast<sal_uInt8>(color1 + color2);
23 }
24 
26 {
27  Bitmap aBitmap(rBitmapEx.GetBitmap());
28 
29  const tools::Long nWidth = aBitmap.GetSizePixel().Width();
30  const tools::Long nHeight = aBitmap.GetSizePixel().Height();
31 
32  Bitmap aResultBitmap(aBitmap.GetSizePixel(), vcl::PixelFormat::N24_BPP);
33  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
34  BitmapScopedWriteAccess pWriteAcc(aResultBitmap);
35  const BitmapColor aColorOne(static_cast<sal_uInt8>(mnColorOne >> 16),
36  static_cast<sal_uInt8>(mnColorOne >> 8),
37  static_cast<sal_uInt8>(mnColorOne));
38  const BitmapColor aColorTwo(static_cast<sal_uInt8>(mnColorTwo >> 16),
39  static_cast<sal_uInt8>(mnColorTwo >> 8),
40  static_cast<sal_uInt8>(mnColorTwo));
41 
42  for (tools::Long x = 0; x < nWidth; x++)
43  {
44  for (tools::Long y = 0; y < nHeight; y++)
45  {
46  BitmapColor aColor = pReadAcc->GetColor(y, x);
47  sal_uInt8 nLuminance = aColor.GetLuminance();
48  BitmapColor aResultColor(
49  lcl_getDuotoneColorComponent(nLuminance, aColorOne.GetRed(), aColorTwo.GetRed()),
50  lcl_getDuotoneColorComponent(nLuminance, aColorOne.GetGreen(),
51  aColorTwo.GetGreen()),
52  lcl_getDuotoneColorComponent(nLuminance, aColorOne.GetBlue(), aColorTwo.GetBlue()));
53  pWriteAcc->SetPixel(y, x, aResultColor);
54  }
55  }
56 
57  pWriteAcc.reset();
58  pReadAcc.reset();
59  aBitmap.ReassignWithSize(aResultBitmap);
60 
61  return BitmapEx(aBitmap);
62 }
63 
64 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetRed() const
long Long
sal_uInt8 GetLuminance() const
Size GetSizePixel() const
float x
sal_uInt8 GetBlue() const
float y
tools::Long Width() const
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:229
sal_uInt8 GetGreen() const
static sal_uInt8 lcl_getDuotoneColorComponent(sal_uInt8 base, sal_uInt16 color1, sal_uInt16 color2)
unsigned char sal_uInt8
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override