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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "GlowSoftEgdeShadowTools.hxx"
21 #include <vcl/bitmapex.hxx>
22 #include <vcl/BitmapFilter.hxx>
23 #include <vcl/BitmapBasicMorphologyFilter.hxx>
24 #include <vcl/BitmapFilterStackBlur.hxx>
26 namespace drawinglayer::primitive2d
28 /* Returns 8-bit alpha mask created from passed mask.
30 Negative fErodeDilateRadius values mean erode, positive - dilate.
31 nTransparency defines minimal transparency level.
33 AlphaMask
ProcessAndBlurAlphaMask(const Bitmap
& rMask
, double fErodeDilateRadius
,
34 double fBlurRadius
, sal_uInt8 nTransparency
, bool bConvertTo1Bit
)
36 // Only completely white pixels on the initial mask must be considered for transparency. Any
37 // other color must be treated as black. This creates 1-bit B&W bitmap.
38 BitmapEx
mask(bConvertTo1Bit
? rMask
.CreateMask(COL_WHITE
) : rMask
);
40 // Scaling down increases performance without noticeable quality loss. Additionally,
41 // current blur implementation can only handle blur radius between 2 and 254.
42 Size aSize
= mask
.GetSizePixel();
44 while (fBlurRadius
> 254 || aSize
.Height() > 1000 || aSize
.Width() > 1000)
48 fErodeDilateRadius
/= 2;
49 aSize
.setHeight(aSize
.Height() / 2);
50 aSize
.setWidth(aSize
.Width() / 2);
53 // BmpScaleFlag::NearestNeighbor is important for following color replacement
54 mask
.Scale(fScale
, fScale
, BmpScaleFlag::NearestNeighbor
);
56 if (fErodeDilateRadius
> 0)
57 BitmapFilter::Filter(mask
, BitmapDilateFilter(fErodeDilateRadius
));
58 else if (fErodeDilateRadius
< 0)
59 BitmapFilter::Filter(mask
, BitmapErodeFilter(-fErodeDilateRadius
, 0xFF));
63 const Color
aTransparency(nTransparency
, nTransparency
, nTransparency
);
64 mask
.Replace(COL_BLACK
, aTransparency
);
67 // We need 8-bit grey mask for blurring
68 mask
.Convert(BmpConversion::N8BitGreys
);
70 // calculate blurry effect
71 BitmapFilter::Filter(mask
, BitmapFilterStackBlur(fBlurRadius
));
73 mask
.Scale(rMask
.GetSizePixel());
75 return AlphaMask(mask
.GetBitmap());
78 drawinglayer::geometry::ViewInformation2D
79 expandB2DRangeAtViewInformation2D(const drawinglayer::geometry::ViewInformation2D
& rViewInfo
,
82 drawinglayer::geometry::ViewInformation2D
aRetval(rViewInfo
);
83 basegfx::B2DRange
viewport(rViewInfo
.getViewport());
84 viewport
.grow(nAmount
);
85 aRetval
.setViewport(viewport
);
89 } // end of namespace drawinglayer::primitive2d
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */