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