LibreOffice Module vcl (master)  1
BitmapColorizeFilter.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 <tools/color.hxx>
12 #include <tools/helpers.hxx>
13 
14 #include <bitmapwriteaccess.hxx>
15 #include <BitmapColorizeFilter.hxx>
16 
18 {
19  Bitmap aBitmap = rBitmapEx.GetBitmap();
20  BitmapScopedWriteAccess pWriteAccess(aBitmap);
21 
22  if (!pWriteAccess)
23  return rBitmapEx;
24 
25  BitmapColor aBitmapColor;
26  const long nW = pWriteAccess->Width();
27  const long nH = pWriteAccess->Height();
28  std::vector<sal_uInt8> aMapR(256);
29  std::vector<sal_uInt8> aMapG(256);
30  std::vector<sal_uInt8> aMapB(256);
31  long nX;
32  long nY;
33 
34  const sal_uInt8 cR = maColor.GetRed();
35  const sal_uInt8 cG = maColor.GetGreen();
36  const sal_uInt8 cB = maColor.GetBlue();
37 
38  for (nX = 0; nX < 256; ++nX)
39  {
40  aMapR[nX] = MinMax((nX + cR) / 2, 0, 255);
41  aMapG[nX] = MinMax((nX + cG) / 2, 0, 255);
42  aMapB[nX] = MinMax((nX + cB) / 2, 0, 255);
43  }
44 
45  if (pWriteAccess->HasPalette())
46  {
47  for (sal_uInt16 i = 0, nCount = pWriteAccess->GetPaletteEntryCount(); i < nCount; i++)
48  {
49  const BitmapColor& rCol = pWriteAccess->GetPaletteColor(i);
50  aBitmapColor.SetRed(aMapR[rCol.GetRed()]);
51  aBitmapColor.SetGreen(aMapG[rCol.GetGreen()]);
52  aBitmapColor.SetBlue(aMapB[rCol.GetBlue()]);
53  pWriteAccess->SetPaletteColor(i, aBitmapColor);
54  }
55  }
56  else if (pWriteAccess->GetScanlineFormat() == ScanlineFormat::N24BitTcBgr)
57  {
58  for (nY = 0; nY < nH; ++nY)
59  {
60  Scanline pScan = pWriteAccess->GetScanline(nY);
61 
62  for (nX = 0; nX < nW; ++nX)
63  {
64  *pScan = aMapB[*pScan];
65  pScan++;
66  *pScan = aMapG[*pScan];
67  pScan++;
68  *pScan = aMapR[*pScan];
69  pScan++;
70  }
71  }
72  }
73  else
74  {
75  for (nY = 0; nY < nH; ++nY)
76  {
77  Scanline pScanline = pWriteAccess->GetScanline(nY);
78  for (nX = 0; nX < nW; ++nX)
79  {
80  aBitmapColor = pWriteAccess->GetPixelFromData(pScanline, nX);
81  aBitmapColor.SetRed(aMapR[aBitmapColor.GetRed()]);
82  aBitmapColor.SetGreen(aMapG[aBitmapColor.GetGreen()]);
83  aBitmapColor.SetBlue(aMapB[aBitmapColor.GetBlue()]);
84  pWriteAccess->SetPixelOnData(pScanline, nX, aBitmapColor);
85  }
86  }
87  }
88 
89  return rBitmapEx;
90 }
91 
92 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetRed() const
void SetBlue(sal_uInt8 nBlue)
Scanline GetScanline(long nY) const
long Width() const
void SetPixelOnData(sal_uInt8 *pData, long nX, const BitmapColor &rBitmapColor)
sal_uInt8 GetBlue() const
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
bool HasPalette() const
ScanlineFormat GetScanlineFormat() const
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
int i
void SetRed(sal_uInt8 nRed)
sal_uInt16 GetPaletteEntryCount() const
sal_uInt8 GetGreen() const
long Height() const
unsigned char sal_uInt8
void SetGreen(sal_uInt8 nGreen)
BitmapColor GetPixelFromData(const sal_uInt8 *pData, long nX) const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
std::enable_if< std::is_signed< T >::value||std::is_floating_point< T >::value, long >::type MinMax(T nVal, long nMin, long nMax)
void SetPaletteColor(sal_uInt16 nColor, const BitmapColor &rBitmapColor)