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 if (!pReadAcc)
22 return BitmapEx();
23
25 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
26 if (!pWriteAcc)
27 return BitmapEx();
28
29 const BitmapColor aBlack(pWriteAcc->GetBestMatchingColor(COL_BLACK));
30 const BitmapColor aWhite(pWriteAcc->GetBestMatchingColor(COL_WHITE));
31 const sal_Int32 nWidth = pWriteAcc->Width();
32 const sal_Int32 nHeight = pWriteAcc->Height();
33
34 if (pReadAcc->HasPalette())
35 {
36 for (sal_Int32 nY = 0; nY < nHeight; nY++)
37 {
38 Scanline pScanline = pWriteAcc->GetScanline(nY);
39 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
40 for (sal_Int32 nX = 0; nX < nWidth; nX++)
41 {
42 const sal_uInt8 cIndex = pReadAcc->GetIndexFromData(pScanlineRead, nX);
43 if (pReadAcc->GetPaletteColor(cIndex).GetLuminance() >= mcThreshold)
44 {
45 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
46 }
47 else
48 {
49 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
50 }
51 }
52 }
53 }
54 else
55 {
56 for (sal_Int32 nY = 0; nY < nHeight; nY++)
57 {
58 Scanline pScanline = pWriteAcc->GetScanline(nY);
59 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
60 for (sal_Int32 nX = 0; nX < nWidth; nX++)
61 {
62 if (pReadAcc->GetPixelFromData(pScanlineRead, nX).GetLuminance() >= mcThreshold)
63 {
64 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
65 }
66 else
67 {
68 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
69 }
70 }
71 }
72 }
73
74 pWriteAcc.reset();
75 pReadAcc.reset();
76
77 const MapMode aMap(aBitmap.GetPrefMapMode());
78 const Size aSize(aBitmap.GetPrefSize());
79
80 aBitmap = aNewBmp;
81
82 aBitmap.SetPrefMapMode(aMap);
83 aBitmap.SetPrefSize(aSize);
84
85 return BitmapEx(aBitmap);
86}
87
88/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
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)
static const BitmapPalette & GetGreyPalette(int nEntries)
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