This source file includes following definitions.
- gaussianKernelFactor
- m_stdY
- create
- stdDeviationX
- setStdDeviationX
- stdDeviationY
- setStdDeviationY
- calculateUnscaledKernelSize
- calculateKernelSize
- mapRect
- determineAbsolutePaintRect
- applySoftware
- createImageFilter
- externalRepresentation
#include "config.h"
#include "platform/graphics/filters/FEGaussianBlur.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/cpu/arm/filters/FEGaussianBlurNEON.h"
#include "platform/graphics/filters/ParallelJobs.h"
#include "platform/graphics/filters/SkiaImageFilterBuilder.h"
#include "platform/text/TextStream.h"
#include "wtf/MathExtras.h"
#include "wtf/Uint8ClampedArray.h"
#include "SkBlurImageFilter.h"
using namespace std;
static inline float gaussianKernelFactor()
{
return 3 / 4.f * sqrtf(twoPiFloat);
}
static const int gMaxKernelSize = 1000;
namespace WebCore {
FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y)
: FilterEffect(filter)
, m_stdX(x)
, m_stdY(y)
{
}
PassRefPtr<FEGaussianBlur> FEGaussianBlur::create(Filter* filter, float x, float y)
{
return adoptRef(new FEGaussianBlur(filter, x, y));
}
float FEGaussianBlur::stdDeviationX() const
{
return m_stdX;
}
void FEGaussianBlur::setStdDeviationX(float x)
{
m_stdX = x;
}
float FEGaussianBlur::stdDeviationY() const
{
return m_stdY;
}
void FEGaussianBlur::setStdDeviationY(float y)
{
m_stdY = y;
}
IntSize FEGaussianBlur::calculateUnscaledKernelSize(const FloatPoint& std)
{
ASSERT(std.x() >= 0 && std.y() >= 0);
IntSize kernelSize;
if (std.x()) {
int size = max<unsigned>(2, static_cast<unsigned>(floorf(std.x() * gaussianKernelFactor() + 0.5f)));
kernelSize.setWidth(min(size, gMaxKernelSize));
}
if (std.y()) {
int size = max<unsigned>(2, static_cast<unsigned>(floorf(std.y() * gaussianKernelFactor() + 0.5f)));
kernelSize.setHeight(min(size, gMaxKernelSize));
}
return kernelSize;
}
IntSize FEGaussianBlur::calculateKernelSize(Filter* filter, const FloatPoint& std)
{
FloatPoint stdError(filter->applyHorizontalScale(std.x()), filter->applyVerticalScale(std.y()));
return calculateUnscaledKernelSize(stdError);
}
FloatRect FEGaussianBlur::mapRect(const FloatRect& rect, bool)
{
FloatRect result = rect;
IntSize kernelSize = calculateKernelSize(filter(), FloatPoint(m_stdX, m_stdY));
result.inflateX(3 * kernelSize.width() * 0.5f);
result.inflateY(3 * kernelSize.height() * 0.5f);
return result;
}
FloatRect FEGaussianBlur::determineAbsolutePaintRect(const FloatRect& originalRequestedRect)
{
FloatRect requestedRect = originalRequestedRect;
if (clipsToBounds())
requestedRect.intersect(maxEffectRect());
FilterEffect* input = inputEffect(0);
FloatRect inputRect = input->determineAbsolutePaintRect(mapRect(requestedRect, false));
FloatRect outputRect = mapRect(inputRect, true);
outputRect.intersect(requestedRect);
addAbsolutePaintRect(outputRect);
if (clipsToBounds())
inputRect.intersect(maxEffectRect());
addAbsolutePaintRect(inputRect);
return outputRect;
}
void FEGaussianBlur::applySoftware()
{
ImageBuffer* resultImage = createImageBufferResult();
if (!resultImage)
return;
FilterEffect* in = inputEffect(0);
IntRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
setIsAlphaImage(in->isAlphaImage());
float stdX = filter()->applyHorizontalScale(m_stdX);
float stdY = filter()->applyVerticalScale(m_stdY);
RefPtr<Image> image = in->asImageBuffer()->copyImage(DontCopyBackingStore);
SkPaint paint;
GraphicsContext* dstContext = resultImage->context();
paint.setImageFilter(new SkBlurImageFilter(stdX, stdY))->unref();
SkRect bounds = SkRect::MakeWH(absolutePaintRect().width(), absolutePaintRect().height());
dstContext->saveLayer(&bounds, &paint);
paint.setColor(0xFFFFFFFF);
dstContext->drawImage(image.get(), drawingRegion.location(), CompositeCopy);
dstContext->restoreLayer();
}
PassRefPtr<SkImageFilter> FEGaussianBlur::createImageFilter(SkiaImageFilterBuilder* builder)
{
RefPtr<SkImageFilter> input(builder->build(inputEffect(0), operatingColorSpace()));
float stdX = filter()->applyHorizontalScale(m_stdX);
float stdY = filter()->applyVerticalScale(m_stdY);
SkImageFilter::CropRect rect = getCropRect(builder->cropOffset());
return adoptRef(new SkBlurImageFilter(SkFloatToScalar(stdX), SkFloatToScalar(stdY), input.get(), &rect));
}
TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, int indent) const
{
writeIndent(ts, indent);
ts << "[feGaussianBlur";
FilterEffect::externalRepresentation(ts);
ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n";
inputEffect(0)->externalRepresentation(ts, indent + 1);
return ts;
}
}