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 #include <vcl/bitmapaccess.hxx>
15 
16 #include <bitmapwriteaccess.hxx>
17 
18 static sal_uInt8 lcl_getDuotoneColorComponent(sal_uInt8 base, sal_uInt16 color1, sal_uInt16 color2)
19 {
20  color2 = color2 * base / 0xFF;
21  color1 = color1 * (0xFF - base) / 0xFF;
22 
23  return static_cast<sal_uInt8>(color1 + color2);
24 }
25 
27 {
28  Bitmap aBitmap(rBitmapEx.GetBitmap());
29 
30  const long nWidth = aBitmap.GetSizePixel().Width();
31  const long nHeight = aBitmap.GetSizePixel().Height();
32 
33  Bitmap aResultBitmap(aBitmap.GetSizePixel(), 24);
34  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
35  BitmapScopedWriteAccess pWriteAcc(aResultBitmap);
36  const BitmapColor aColorOne(static_cast<sal_uInt8>(mnColorOne >> 16),
37  static_cast<sal_uInt8>(mnColorOne >> 8),
38  static_cast<sal_uInt8>(mnColorOne));
39  const BitmapColor aColorTwo(static_cast<sal_uInt8>(mnColorTwo >> 16),
40  static_cast<sal_uInt8>(mnColorTwo >> 8),
41  static_cast<sal_uInt8>(mnColorTwo));
42 
43  for (long x = 0; x < nWidth; x++)
44  {
45  for (long y = 0; y < nHeight; y++)
46  {
47  BitmapColor aColor = pReadAcc->GetColor(y, x);
48  sal_uInt8 nLuminance = aColor.GetLuminance();
49  BitmapColor aResultColor(
50  lcl_getDuotoneColorComponent(nLuminance, aColorOne.GetRed(), aColorTwo.GetRed()),
51  lcl_getDuotoneColorComponent(nLuminance, aColorOne.GetGreen(),
52  aColorTwo.GetGreen()),
53  lcl_getDuotoneColorComponent(nLuminance, aColorOne.GetBlue(), aColorTwo.GetBlue()));
54  pWriteAcc->SetPixel(y, x, aResultColor);
55  }
56  }
57 
58  pWriteAcc.reset();
59  pReadAcc.reset();
60  aBitmap.ReassignWithSize(aResultBitmap);
61 
62  return BitmapEx(aBitmap);
63 }
64 
65 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
sal_uInt8 GetRed() const
sal_uInt8 GetLuminance() const
void SetPixel(long nY, long nX, const BitmapColor &rBitmapColor)
sal_uLong const mnColorTwo
Size GetSizePixel() const
float x
sal_uInt8 GetBlue() const
float y
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
sal_uInt8 GetGreen() const
static sal_uInt8 lcl_getDuotoneColorComponent(sal_uInt8 base, sal_uInt16 color1, sal_uInt16 color2)
unsigned char sal_uInt8
sal_uLong const mnColorOne
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override