Get the style color and number just once
[LibreOffice.git] / vcl / source / bitmap / BitmapLightenBlendFilter.cxx
blob8d06ae1228d9004b31ac8113232bf10071f73f17
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 <comphelper/diagnose_ex.hxx>
13 #include <vcl/bitmap/BitmapLightenBlendFilter.hxx>
14 #include <vcl/BitmapWriteAccess.hxx>
15 #include <vcl/BitmapTools.hxx>
17 BitmapLightenBlendFilter::BitmapLightenBlendFilter(BitmapEx const& rBitmapEx,
18 BitmapEx const& rBitmapEx2)
19 : maBitmapEx(rBitmapEx)
20 , maBitmapEx2(rBitmapEx2)
24 BitmapLightenBlendFilter::~BitmapLightenBlendFilter() {}
26 static sal_uInt8 lcl_calculate(const sal_uInt8 aColor, const sal_uInt8 aAlpha,
27 const sal_uInt8 aColor2, const sal_uInt8 aAlpha2)
29 const double c1 = aColor / 255.0;
30 const double c2 = aColor2 / 255.0;
31 const double a1 = aAlpha / 255.0;
32 const double a2 = aAlpha2 / 255.0;
33 const double result = std::max((1.0 - a1) * c2 + c1, (1.0 - a2) * c1 + c2);
34 return result * 255.0;
37 BitmapEx BitmapLightenBlendFilter::execute()
39 if (maBitmapEx.IsEmpty() || maBitmapEx2.IsEmpty())
40 return BitmapEx();
42 Size aSize = maBitmapEx.GetBitmap().GetSizePixel();
43 Size aSize2 = maBitmapEx2.GetBitmap().GetSizePixel();
44 sal_Int32 nHeight = std::min(aSize.getHeight(), aSize2.getHeight());
45 sal_Int32 nWidth = std::min(aSize.getWidth(), aSize2.getWidth());
47 Bitmap aDstBitmap(Size(nWidth, nHeight), vcl::PixelFormat::N24_BPP);
48 Bitmap aDstAlpha(AlphaMask(Size(nWidth, nHeight)).GetBitmap());
50 BitmapScopedWriteAccess pWriteAccess(aDstBitmap);
51 BitmapScopedWriteAccess pAlphaWriteAccess(aDstAlpha);
53 for (tools::Long y(0); y < nHeight; ++y)
55 Scanline pScanline = pWriteAccess->GetScanline(y);
56 Scanline pScanAlpha = pAlphaWriteAccess->GetScanline(y);
57 for (tools::Long x(0); x < nWidth; ++x)
59 BitmapColor i1 = vcl::bitmap::premultiply(maBitmapEx.GetPixelColor(x, y));
60 BitmapColor i2 = vcl::bitmap::premultiply(maBitmapEx2.GetPixelColor(x, y));
61 sal_uInt8 r(lcl_calculate(i1.GetRed(), i1.GetAlpha(), i2.GetRed(), i2.GetAlpha()));
62 sal_uInt8 g(lcl_calculate(i1.GetGreen(), i1.GetAlpha(), i2.GetGreen(), i2.GetAlpha()));
63 sal_uInt8 b(lcl_calculate(i1.GetBlue(), i1.GetAlpha(), i2.GetBlue(), i2.GetAlpha()));
64 sal_uInt8 a(lcl_calculate(i1.GetAlpha(), i1.GetAlpha(), i2.GetAlpha(), i2.GetAlpha()));
66 pWriteAccess->SetPixelOnData(
67 pScanline, x, vcl::bitmap::unpremultiply(BitmapColor(ColorAlpha, r, g, b, a)));
68 pAlphaWriteAccess->SetPixelOnData(pScanAlpha, x, BitmapColor(a));
72 pWriteAccess.reset();
73 pAlphaWriteAccess.reset();
75 return BitmapEx(aDstBitmap, AlphaMask(aDstAlpha));
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */