Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / filters / FEGaussianBlur.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
5  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6  * Copyright (C) 2010 Igalia, S.L.
7  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
8  * Copyright (C) 2013 Google Inc. All rights reserved.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25
26 #include "config.h"
27
28 #include "platform/graphics/filters/FEGaussianBlur.h"
29
30 #include "platform/graphics/filters/Filter.h"
31 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
32 #include "platform/text/TextStream.h"
33
34 #include "SkBlurImageFilter.h"
35
36 static inline float gaussianKernelFactor()
37 {
38     return 3 / 4.f * sqrtf(twoPiFloat);
39 }
40
41 static const int gMaxKernelSize = 1000;
42
43 namespace blink {
44
45 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y)
46     : FilterEffect(filter)
47     , m_stdX(x)
48     , m_stdY(y)
49 {
50 }
51
52 PassRefPtr<FEGaussianBlur> FEGaussianBlur::create(Filter* filter, float x, float y)
53 {
54     return adoptRef(new FEGaussianBlur(filter, x, y));
55 }
56
57 float FEGaussianBlur::stdDeviationX() const
58 {
59     return m_stdX;
60 }
61
62 void FEGaussianBlur::setStdDeviationX(float x)
63 {
64     m_stdX = x;
65 }
66
67 float FEGaussianBlur::stdDeviationY() const
68 {
69     return m_stdY;
70 }
71
72 void FEGaussianBlur::setStdDeviationY(float y)
73 {
74     m_stdY = y;
75 }
76
77 IntSize FEGaussianBlur::calculateUnscaledKernelSize(const FloatPoint& std)
78 {
79     ASSERT(std.x() >= 0 && std.y() >= 0);
80
81     IntSize kernelSize;
82     // Limit the kernel size to 1000. A bigger radius won't make a big difference for the result image but
83     // inflates the absolute paint rect to much. This is compatible with Firefox' behavior.
84     if (std.x()) {
85         int size = std::max<unsigned>(2, static_cast<unsigned>(floorf(std.x() * gaussianKernelFactor() + 0.5f)));
86         kernelSize.setWidth(std::min(size, gMaxKernelSize));
87     }
88
89     if (std.y()) {
90         int size = std::max<unsigned>(2, static_cast<unsigned>(floorf(std.y() * gaussianKernelFactor() + 0.5f)));
91         kernelSize.setHeight(std::min(size, gMaxKernelSize));
92     }
93
94     return kernelSize;
95 }
96
97 IntSize FEGaussianBlur::calculateKernelSize(Filter* filter, const FloatPoint& std)
98 {
99     FloatPoint stdError(filter->applyHorizontalScale(std.x()), filter->applyVerticalScale(std.y()));
100
101     return calculateUnscaledKernelSize(stdError);
102 }
103
104 FloatRect FEGaussianBlur::mapRect(const FloatRect& rect, bool)
105 {
106     FloatRect result = rect;
107     IntSize kernelSize = calculateKernelSize(filter(), FloatPoint(m_stdX, m_stdY));
108
109     // We take the half kernel size and multiply it with three, because we run box blur three times.
110     result.inflateX(3 * kernelSize.width() * 0.5f);
111     result.inflateY(3 * kernelSize.height() * 0.5f);
112     return result;
113 }
114
115 FloatRect FEGaussianBlur::determineAbsolutePaintRect(const FloatRect& originalRequestedRect)
116 {
117     FloatRect requestedRect = originalRequestedRect;
118     if (clipsToBounds())
119         requestedRect.intersect(maxEffectRect());
120
121     FilterEffect* input = inputEffect(0);
122     FloatRect inputRect = input->determineAbsolutePaintRect(mapRect(requestedRect, false));
123     FloatRect outputRect = mapRect(inputRect, true);
124     outputRect.intersect(requestedRect);
125     addAbsolutePaintRect(outputRect);
126
127     // Blur needs space for both input and output pixels in the paint area.
128     // Input is also clipped to subregion.
129     if (clipsToBounds())
130         inputRect.intersect(maxEffectRect());
131     addAbsolutePaintRect(inputRect);
132     return outputRect;
133 }
134
135 PassRefPtr<SkImageFilter> FEGaussianBlur::createImageFilter(SkiaImageFilterBuilder* builder)
136 {
137     RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
138     float stdX = filter()->applyHorizontalScale(m_stdX);
139     float stdY = filter()->applyVerticalScale(m_stdY);
140     SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
141     return adoptRef(SkBlurImageFilter::Create(SkFloatToScalar(stdX), SkFloatToScalar(stdY), input.get(), &rect));
142 }
143
144 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, int indent) const
145 {
146     writeIndent(ts, indent);
147     ts << "[feGaussianBlur";
148     FilterEffect::externalRepresentation(ts);
149     ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n";
150     inputEffect(0)->externalRepresentation(ts, indent + 1);
151     return ts;
152 }
153
154 } // namespace blink