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/BitmapDuoToneFilter.hxx>
14 #include <vcl/bitmapaccess.hxx>
16 #include <bitmapwriteaccess.hxx>
18 static sal_uInt8
lcl_getDuotoneColorComponent(sal_uInt8 base
, sal_uInt16 color1
, sal_uInt16 color2
)
20 color2
= color2
* base
/ 0xFF;
21 color1
= color1
* (0xFF - base
) / 0xFF;
23 return static_cast<sal_uInt8
>(color1
+ color2
);
26 BitmapEx
BitmapDuoToneFilter::execute(BitmapEx
const& rBitmapEx
) const
28 Bitmap
aBitmap(rBitmapEx
.GetBitmap());
30 const long nWidth
= aBitmap
.GetSizePixel().Width();
31 const long nHeight
= aBitmap
.GetSizePixel().Height();
33 Bitmap
aResultBitmap(aBitmap
.GetSizePixel(), 24);
34 Bitmap::ScopedReadAccess
pReadAcc(aBitmap
);
35 BitmapScopedWriteAccess
pWriteAcc(aResultBitmap
);
36 const BitmapColor
aColorOne(static_cast<sal_uInt8
>(mnColorOne
>> 16),
37 static_cast<sal_uInt8
>(mnColorOne
>> 8),
38 static_cast<sal_uInt8
>(mnColorOne
));
39 const BitmapColor
aColorTwo(static_cast<sal_uInt8
>(mnColorTwo
>> 16),
40 static_cast<sal_uInt8
>(mnColorTwo
>> 8),
41 static_cast<sal_uInt8
>(mnColorTwo
));
43 for (long x
= 0; x
< nWidth
; x
++)
45 for (long y
= 0; y
< nHeight
; y
++)
47 BitmapColor aColor
= pReadAcc
->GetColor(y
, x
);
48 sal_uInt8 nLuminance
= aColor
.GetLuminance();
49 BitmapColor
aResultColor(
50 lcl_getDuotoneColorComponent(nLuminance
, aColorOne
.GetRed(), aColorTwo
.GetRed()),
51 lcl_getDuotoneColorComponent(nLuminance
, aColorOne
.GetGreen(),
52 aColorTwo
.GetGreen()),
53 lcl_getDuotoneColorComponent(nLuminance
, aColorOne
.GetBlue(), aColorTwo
.GetBlue()));
54 pWriteAcc
->SetPixel(y
, x
, aResultColor
);
60 aBitmap
.ReassignWithSize(aResultBitmap
);
62 return BitmapEx(aBitmap
);
65 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */