bump product version to 7.2.5.1
[LibreOffice.git] / vcl / source / bitmap / BitmapMonochromeFilter.cxx
blob365e06a7a15c4052f18595407963980e88d16aa5
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>
15 #include <bitmap/BitmapWriteAccess.hxx>
17 BitmapEx BitmapMonochromeFilter::execute(BitmapEx const& aBitmapEx) const
19 Bitmap aBitmap = aBitmapEx.GetBitmap();
20 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
21 bool bRet = false;
23 if (pReadAcc)
25 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N1_BPP);
26 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
28 if (pWriteAcc)
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);
48 else
50 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
55 else
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()
64 >= mcThreshold)
66 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
68 else
70 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
76 pWriteAcc.reset();
77 bRet = true;
80 pReadAcc.reset();
82 if (bRet)
84 const MapMode aMap(aBitmap.GetPrefMapMode());
85 const Size aSize(aBitmap.GetPrefSize());
87 aBitmap = aNewBmp;
89 aBitmap.SetPrefMapMode(aMap);
90 aBitmap.SetPrefSize(aSize);
94 if (bRet)
95 return BitmapEx(aBitmap);
97 return BitmapEx();
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */