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 {
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 sal_Int32 nWidth = pWriteAcc->Width();
33 const sal_Int32 nHeight = pWriteAcc->Height();
34
35 if (pReadAcc->HasPalette())
36 {
37 for (sal_Int32 nY = 0; nY < nHeight; nY++)
38 {
39 Scanline pScanline = pWriteAcc->GetScanline(nY);
40 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
41 for (sal_Int32 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 (sal_Int32 nY = 0; nY < nHeight; nY++)
58 {
59 Scanline pScanline = pWriteAcc->GetScanline(nY);
60 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
61 for (sal_Int32 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 * Scanline
Definition: Scanline.hxx:26
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:203
bool HasPalette() const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
BitmapColor GetPixelFromData(const sal_uInt8 *pData, tools::Long nX) const
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, tools::Long nX) const
Scanline GetScanline(tools::Long nY) const
void SetPrefMapMode(const MapMode &rMapMode)
const MapMode & GetPrefMapMode() const
Size GetSizePixel() const
void SetPrefSize(const Size &rSize)
const Size & GetPrefSize() const
sal_uInt8 GetLuminance() const
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
HashMap_OWString_Interface aMap
unsigned char sal_uInt8