1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <vcl/bitmap.hxx>
#include <vcl/bitmapex.hxx>
#include <vcl/BitmapMonochromeFilter.hxx>
#include <vcl/bitmapaccess.hxx>
#include <bitmapwriteaccess.hxx>
BitmapEx BitmapMonochromeFilter::execute(BitmapEx const& aBitmapEx)
{
Bitmap aBitmap = aBitmapEx.GetBitmap();
Bitmap::ScopedReadAccess pReadAcc(aBitmap);
bool bRet = false;
if (pReadAcc)
{
Bitmap aNewBmp(aBitmap.GetSizePixel(), 1);
BitmapScopedWriteAccess pWriteAcc(aNewBmp);
if (pWriteAcc)
{
const BitmapColor aBlack(pWriteAcc->GetBestMatchingColor(COL_BLACK));
const BitmapColor aWhite(pWriteAcc->GetBestMatchingColor(COL_WHITE));
const long nWidth = pWriteAcc->Width();
const long nHeight = pWriteAcc->Height();
if (pReadAcc->HasPalette())
{
for (long nY = 0; nY < nHeight; nY++)
{
Scanline pScanline = pWriteAcc->GetScanline(nY);
Scanline pScanlineRead = pReadAcc->GetScanline(nY);
for (long nX = 0; nX < nWidth; nX++)
{
const sal_uInt8 cIndex = pReadAcc->GetIndexFromData(pScanlineRead, nX);
if (pReadAcc->GetPaletteColor(cIndex).GetLuminance() >= mcThreshold)
{
pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
}
else
{
pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
}
}
}
}
else
{
for (long nY = 0; nY < nHeight; nY++)
{
Scanline pScanline = pWriteAcc->GetScanline(nY);
Scanline pScanlineRead = pReadAcc->GetScanline(nY);
for (long nX = 0; nX < nWidth; nX++)
{
if (pReadAcc->GetPixelFromData(pScanlineRead, nX).GetLuminance()
>= mcThreshold)
{
pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
}
else
{
pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
}
}
}
}
pWriteAcc.reset();
bRet = true;
}
pReadAcc.reset();
if (bRet)
{
const MapMode aMap(aBitmap.GetPrefMapMode());
const Size aSize(aBitmap.GetPrefSize());
aBitmap = aNewBmp;
aBitmap.SetPrefMapMode(aMap);
aBitmap.SetPrefSize(aSize);
}
}
if (bRet)
return BitmapEx(aBitmap);
return BitmapEx();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|