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.
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.
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.
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.
28 #include "platform/graphics/filters/FEGaussianBlur.h"
30 #include "platform/graphics/filters/Filter.h"
31 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
32 #include "platform/text/TextStream.h"
34 #include "SkBlurImageFilter.h"
36 static inline float gaussianKernelFactor()
38 return 3 / 4.f * sqrtf(twoPiFloat);
41 static const int gMaxKernelSize = 1000;
45 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y)
46 : FilterEffect(filter)
52 PassRefPtr<FEGaussianBlur> FEGaussianBlur::create(Filter* filter, float x, float y)
54 return adoptRef(new FEGaussianBlur(filter, x, y));
57 float FEGaussianBlur::stdDeviationX() const
62 void FEGaussianBlur::setStdDeviationX(float x)
67 float FEGaussianBlur::stdDeviationY() const
72 void FEGaussianBlur::setStdDeviationY(float y)
77 IntSize FEGaussianBlur::calculateUnscaledKernelSize(const FloatPoint& std)
79 ASSERT(std.x() >= 0 && std.y() >= 0);
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.
85 int size = std::max<unsigned>(2, static_cast<unsigned>(floorf(std.x() * gaussianKernelFactor() + 0.5f)));
86 kernelSize.setWidth(std::min(size, gMaxKernelSize));
90 int size = std::max<unsigned>(2, static_cast<unsigned>(floorf(std.y() * gaussianKernelFactor() + 0.5f)));
91 kernelSize.setHeight(std::min(size, gMaxKernelSize));
97 IntSize FEGaussianBlur::calculateKernelSize(Filter* filter, const FloatPoint& std)
99 FloatPoint stdError(filter->applyHorizontalScale(std.x()), filter->applyVerticalScale(std.y()));
101 return calculateUnscaledKernelSize(stdError);
104 FloatRect FEGaussianBlur::mapRect(const FloatRect& rect, bool)
106 FloatRect result = rect;
107 IntSize kernelSize = calculateKernelSize(filter(), FloatPoint(m_stdX, m_stdY));
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);
115 FloatRect FEGaussianBlur::determineAbsolutePaintRect(const FloatRect& originalRequestedRect)
117 FloatRect requestedRect = originalRequestedRect;
119 requestedRect.intersect(maxEffectRect());
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);
127 // Blur needs space for both input and output pixels in the paint area.
128 // Input is also clipped to subregion.
130 inputRect.intersect(maxEffectRect());
131 addAbsolutePaintRect(inputRect);
135 PassRefPtr<SkImageFilter> FEGaussianBlur::createImageFilter(SkiaImageFilterBuilder* builder)
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));
144 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, int indent) const
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);