LibreOffice Module vcl (master)  1
BitmapMonochromeFilter.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 
19 {
20  Bitmap aBitmap = aBitmapEx.GetBitmap();
21  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
22  bool bRet = false;
23 
24  if (pReadAcc)
25  {
26  Bitmap aNewBmp(aBitmap.GetSizePixel(), 1);
27  BitmapScopedWriteAccess pWriteAcc(aNewBmp);
28 
29  if (pWriteAcc)
30  {
31  const BitmapColor aBlack(pWriteAcc->GetBestMatchingColor(COL_BLACK));
32  const BitmapColor aWhite(pWriteAcc->GetBestMatchingColor(COL_WHITE));
33  const long nWidth = pWriteAcc->Width();
34  const long nHeight = pWriteAcc->Height();
35 
36  if (pReadAcc->HasPalette())
37  {
38  for (long nY = 0; nY < nHeight; nY++)
39  {
40  Scanline pScanline = pWriteAcc->GetScanline(nY);
41  Scanline pScanlineRead = pReadAcc->GetScanline(nY);
42  for (long nX = 0; nX < nWidth; nX++)
43  {
44  const sal_uInt8 cIndex = pReadAcc->GetIndexFromData(pScanlineRead, nX);
45  if (pReadAcc->GetPaletteColor(cIndex).GetLuminance() >= mcThreshold)
46  {
47  pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
48  }
49  else
50  {
51  pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
52  }
53  }
54  }
55  }
56  else
57  {
58  for (long nY = 0; nY < nHeight; nY++)
59  {
60  Scanline pScanline = pWriteAcc->GetScanline(nY);
61  Scanline pScanlineRead = pReadAcc->GetScanline(nY);
62  for (long nX = 0; nX < nWidth; nX++)
63  {
64  if (pReadAcc->GetPixelFromData(pScanlineRead, nX).GetLuminance()
65  >= mcThreshold)
66  {
67  pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
68  }
69  else
70  {
71  pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
72  }
73  }
74  }
75  }
76 
77  pWriteAcc.reset();
78  bRet = true;
79  }
80 
81  pReadAcc.reset();
82 
83  if (bRet)
84  {
85  const MapMode aMap(aBitmap.GetPrefMapMode());
86  const Size aSize(aBitmap.GetPrefSize());
87 
88  aBitmap = aNewBmp;
89 
90  aBitmap.SetPrefMapMode(aMap);
91  aBitmap.SetPrefSize(aSize);
92  }
93  }
94 
95  if (bRet)
96  return BitmapEx(aBitmap);
97 
98  return BitmapEx();
99 }
100 
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
Scanline GetScanline(long nY) const
sal_uInt8 GetLuminance() const
Size GetSizePixel() const
const Size & GetPrefSize() const
Definition: bitmap.hxx:566
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
const MapMode & GetPrefMapMode() const
Definition: bitmap.hxx:556
void SetPrefMapMode(const MapMode &rMapMode)
Definition: bitmap.hxx:561
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
bool HasPalette() const
unsigned char sal_uInt8
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
constexpr::Color COL_WHITE(0xFF, 0xFF, 0xFF)
BitmapColor GetPixelFromData(const sal_uInt8 *pData, long nX) const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, long nX) const