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>
14 #include <vcl/bitmapaccess.hxx>
16 #include <bitmapwriteaccess.hxx>
18 BitmapEx
BitmapMonochromeFilter::execute(BitmapEx
const& aBitmapEx
) const
20 Bitmap aBitmap
= aBitmapEx
.GetBitmap();
21 Bitmap::ScopedReadAccess
pReadAcc(aBitmap
);
26 Bitmap
aNewBmp(aBitmap
.GetSizePixel(), 1);
27 BitmapScopedWriteAccess
pWriteAcc(aNewBmp
);
31 const BitmapColor
aBlack(pWriteAcc
->GetBestMatchingColor(COL_BLACK
));
32 const BitmapColor
aWhite(pWriteAcc
->GetBestMatchingColor(COL_WHITE
));
33 const long nWidth
= pWriteAcc
->Width();
34 const long nHeight
= pWriteAcc
->Height();
36 if (pReadAcc
->HasPalette())
38 for (long nY
= 0; nY
< nHeight
; nY
++)
40 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
41 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
42 for (long nX
= 0; nX
< nWidth
; nX
++)
44 const sal_uInt8 cIndex
= pReadAcc
->GetIndexFromData(pScanlineRead
, nX
);
45 if (pReadAcc
->GetPaletteColor(cIndex
).GetLuminance() >= mcThreshold
)
47 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aWhite
);
51 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aBlack
);
58 for (long nY
= 0; nY
< nHeight
; nY
++)
60 Scanline pScanline
= pWriteAcc
->GetScanline(nY
);
61 Scanline pScanlineRead
= pReadAcc
->GetScanline(nY
);
62 for (long nX
= 0; nX
< nWidth
; nX
++)
64 if (pReadAcc
->GetPixelFromData(pScanlineRead
, nX
).GetLuminance()
67 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aWhite
);
71 pWriteAcc
->SetPixelOnData(pScanline
, nX
, aBlack
);
85 const MapMode
aMap(aBitmap
.GetPrefMapMode());
86 const Size
aSize(aBitmap
.GetPrefSize());
90 aBitmap
.SetPrefMapMode(aMap
);
91 aBitmap
.SetPrefSize(aSize
);
96 return BitmapEx(aBitmap
);
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */