bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / bitmap / BitmapMonochromeFilter.cxx
blob72bacf84955af34cb053b1d00151d1bbc40bf485
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
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);
22 bool bRet = false;
24 if (pReadAcc)
26 Bitmap aNewBmp(aBitmap.GetSizePixel(), 1);
27 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
29 if (pWriteAcc)
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);
49 else
51 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
56 else
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()
65 >= mcThreshold)
67 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
69 else
71 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
77 pWriteAcc.reset();
78 bRet = true;
81 pReadAcc.reset();
83 if (bRet)
85 const MapMode aMap(aBitmap.GetPrefMapMode());
86 const Size aSize(aBitmap.GetPrefSize());
88 aBitmap = aNewBmp;
90 aBitmap.SetPrefMapMode(aMap);
91 aBitmap.SetPrefSize(aSize);
95 if (bRet)
96 return BitmapEx(aBitmap);
98 return BitmapEx();
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */