1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
11 #include <vcl/bitmap.hxx>
12 #include <vcl/bitmapex.hxx>
13 #include <vcl/BitmapMonochromeFilter.hxx>
15 #include <bitmap/BitmapWriteAccess.hxx>
17 BitmapEx
BitmapMonochromeFilter::execute(BitmapEx
const& aBitmapEx
) const
19 Bitmap aBitmap
= aBitmapEx
.GetBitmap();
20 Bitmap::ScopedReadAccess
pReadAcc(aBitmap
);
25 Bitmap
aNewBmp(aBitmap
.GetSizePixel(), vcl::PixelFormat::N1_BPP
);
26 BitmapScopedWriteAccess
pWriteAcc(aNewBmp
);
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();
35 if (pReadAcc
->HasPalette())
37 for (tools::Long nY
= 0; nY
< nHeight
; nY
++)
39 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
40 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
41 for (tools::Long nX
= 0; nX
< nWidth
; nX
++)
43 const sal_uInt8 cIndex
= pReadAcc
->GetIndexFromData(pScanlineRead
, nX
);
44 if (pReadAcc
->GetPaletteColor(cIndex
).GetLuminance() >= mcThreshold
)
46 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aWhite
);
50 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aBlack
);
57 for (tools::Long nY
= 0; nY
< nHeight
; nY
++)
59 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
60 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
61 for (tools::Long nX
= 0; nX
< nWidth
; nX
++)
63 if (pReadAcc
->GetPixelFromData(pScanlineRead
, nX
).GetLuminance()
66 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aWhite
);
70 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aBlack
);
84 const MapMode
aMap(aBitmap
.GetPrefMapMode());
85 const Size
aSize(aBitmap
.GetPrefSize());
89 aBitmap
.SetPrefMapMode(aMap
);
90 aBitmap
.SetPrefSize(aSize
);
95 return BitmapEx(aBitmap
);
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */