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/BitmapMonochromeFilter.hxx>
12 #include <vcl/BitmapWriteAccess.hxx>
14 BitmapEx
BitmapMonochromeFilter::execute(BitmapEx
const& aBitmapEx
) const
16 Bitmap aBitmap
= aBitmapEx
.GetBitmap();
17 BitmapScopedReadAccess
pReadAcc(aBitmap
);
21 Bitmap
aNewBmp(aBitmap
.GetSizePixel(), vcl::PixelFormat::N8_BPP
, &Bitmap::GetGreyPalette(256));
22 BitmapScopedWriteAccess
pWriteAcc(aNewBmp
);
26 const BitmapColor
aBlack(pWriteAcc
->GetBestMatchingColor(COL_BLACK
));
27 const BitmapColor
aWhite(pWriteAcc
->GetBestMatchingColor(COL_WHITE
));
28 const sal_Int32 nWidth
= pWriteAcc
->Width();
29 const sal_Int32 nHeight
= pWriteAcc
->Height();
31 if (pReadAcc
->HasPalette())
33 for (sal_Int32 nY
= 0; nY
< nHeight
; nY
++)
35 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
36 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
37 for (sal_Int32 nX
= 0; nX
< nWidth
; nX
++)
39 const sal_uInt8 cIndex
= pReadAcc
->GetIndexFromData(pScanlineRead
, nX
);
40 if (pReadAcc
->GetPaletteColor(cIndex
).GetLuminance() >= mcThreshold
)
42 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aWhite
);
46 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aBlack
);
53 for (sal_Int32 nY
= 0; nY
< nHeight
; nY
++)
55 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
56 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
57 for (sal_Int32 nX
= 0; nX
< nWidth
; nX
++)
59 if (pReadAcc
->GetPixelFromData(pScanlineRead
, nX
).GetLuminance() >= mcThreshold
)
61 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aWhite
);
65 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aBlack
);
74 const MapMode
aMap(aBitmap
.GetPrefMapMode());
75 const Size
aSize(aBitmap
.GetPrefSize());
77 aBitmap
= std::move(aNewBmp
);
79 aBitmap
.SetPrefMapMode(aMap
);
80 aBitmap
.SetPrefSize(aSize
);
82 return BitmapEx(aBitmap
);
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */