| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org> | 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org> |
| 3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org> | 3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org> |
| 4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org> | 4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org> |
| 5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> | 5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
| 6 * Copyright (C) 2010 Igalia, S.L. | 6 * Copyright (C) 2010 Igalia, S.L. |
| 7 * Copyright (C) Research In Motion Limited 2010. All rights reserved. | 7 * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
| 8 * Copyright (C) 2013 Google Inc. All rights reserved. | 8 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 9 * | 9 * |
| 10 * This library is free software; you can redistribute it and/or | 10 * This library is free software; you can redistribute it and/or |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "platform/graphics/filters/FEGaussianBlur.h" | 26 #include "platform/graphics/filters/FEGaussianBlur.h" |
| 27 | 27 |
| 28 #include "platform/graphics/filters/Filter.h" | 28 #include "platform/graphics/filters/Filter.h" |
| 29 #include "platform/graphics/filters/SkiaImageFilterBuilder.h" | 29 #include "platform/graphics/filters/SkiaImageFilterBuilder.h" |
| 30 #include "platform/text/TextStream.h" | 30 #include "platform/text/TextStream.h" |
| 31 | 31 |
| 32 #include "SkBlurImageFilter.h" | 32 #include "SkBlurImageFilter.h" |
| 33 | 33 |
| 34 static inline float gaussianKernelFactor() { | 34 namespace blink { |
| 35 return 3 / 4.f * sqrtf(twoPiFloat); | 35 |
| 36 namespace { |
| 37 |
| 38 inline unsigned approximateBoxWidth(float s) { |
| 39 return static_cast<unsigned>( |
| 40 floorf(s * (3 / 4.f * sqrtf(twoPiFloat)) + 0.5f)); |
| 36 } | 41 } |
| 37 | 42 |
| 38 namespace blink { | 43 IntSize calculateKernelSize(const FloatSize& std) { |
| 44 DCHECK(std.width() >= 0 && std.height() >= 0); |
| 45 IntSize kernelSize; |
| 46 if (std.width()) { |
| 47 int size = std::max<unsigned>(2, approximateBoxWidth(std.width())); |
| 48 kernelSize.setWidth(size); |
| 49 } |
| 50 if (std.height()) { |
| 51 int size = std::max<unsigned>(2, approximateBoxWidth(std.height())); |
| 52 kernelSize.setHeight(size); |
| 53 } |
| 54 return kernelSize; |
| 55 } |
| 56 |
| 57 } |
| 39 | 58 |
| 40 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y) | 59 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y) |
| 41 : FilterEffect(filter), m_stdX(x), m_stdY(y) {} | 60 : FilterEffect(filter), m_stdX(x), m_stdY(y) {} |
| 42 | 61 |
| 43 FEGaussianBlur* FEGaussianBlur::create(Filter* filter, float x, float y) { | 62 FEGaussianBlur* FEGaussianBlur::create(Filter* filter, float x, float y) { |
| 44 return new FEGaussianBlur(filter, x, y); | 63 return new FEGaussianBlur(filter, x, y); |
| 45 } | 64 } |
| 46 | 65 |
| 47 IntSize FEGaussianBlur::calculateUnscaledKernelSize(const FloatPoint& std) { | 66 FloatRect FEGaussianBlur::mapEffect(const FloatSize& stdDeviation, |
| 48 ASSERT(std.x() >= 0 && std.y() >= 0); | 67 const FloatRect& rect) { |
| 49 | 68 IntSize kernelSize = calculateKernelSize(stdDeviation); |
| 50 IntSize kernelSize; | |
| 51 // Limit the kernel size to 1000. A bigger radius won't make a big difference | |
| 52 // for the result image but inflates the absolute paint rect too much. This is | |
| 53 // compatible with Firefox' behavior. | |
| 54 if (std.x()) { | |
| 55 int size = std::max<unsigned>( | |
| 56 2, | |
| 57 static_cast<unsigned>(floorf(std.x() * gaussianKernelFactor() + 0.5f))); | |
| 58 kernelSize.setWidth(size); | |
| 59 } | |
| 60 | |
| 61 if (std.y()) { | |
| 62 int size = std::max<unsigned>( | |
| 63 2, | |
| 64 static_cast<unsigned>(floorf(std.y() * gaussianKernelFactor() + 0.5f))); | |
| 65 kernelSize.setHeight(size); | |
| 66 } | |
| 67 | |
| 68 return kernelSize; | |
| 69 } | |
| 70 | |
| 71 IntSize FEGaussianBlur::calculateKernelSize(const Filter* filter, | |
| 72 const FloatPoint& std) { | |
| 73 FloatPoint stdError(filter->applyHorizontalScale(std.x()), | |
| 74 filter->applyVerticalScale(std.y())); | |
| 75 return calculateUnscaledKernelSize(stdError); | |
| 76 } | |
| 77 | |
| 78 FloatRect FEGaussianBlur::mapEffect(const FloatRect& rect) const { | |
| 79 IntSize kernelSize = | |
| 80 calculateKernelSize(getFilter(), FloatPoint(m_stdX, m_stdY)); | |
| 81 | |
| 82 // We take the half kernel size and multiply it by three, because we run box | 69 // We take the half kernel size and multiply it by three, because we run box |
| 83 // blur three times. | 70 // blur three times. |
| 84 FloatRect result = rect; | 71 FloatRect result = rect; |
| 85 result.inflateX(3.0f * kernelSize.width() * 0.5f); | 72 result.inflateX(3.0f * kernelSize.width() * 0.5f); |
| 86 result.inflateY(3.0f * kernelSize.height() * 0.5f); | 73 result.inflateY(3.0f * kernelSize.height() * 0.5f); |
| 87 return result; | 74 return result; |
| 88 } | 75 } |
| 89 | 76 |
| 77 FloatRect FEGaussianBlur::mapEffect(const FloatRect& rect) const { |
| 78 FloatSize stdError(getFilter()->applyHorizontalScale(m_stdX), |
| 79 getFilter()->applyVerticalScale(m_stdY)); |
| 80 return mapEffect(stdError, rect); |
| 81 } |
| 82 |
| 90 sk_sp<SkImageFilter> FEGaussianBlur::createImageFilter() { | 83 sk_sp<SkImageFilter> FEGaussianBlur::createImageFilter() { |
| 91 sk_sp<SkImageFilter> input( | 84 sk_sp<SkImageFilter> input( |
| 92 SkiaImageFilterBuilder::build(inputEffect(0), operatingColorSpace())); | 85 SkiaImageFilterBuilder::build(inputEffect(0), operatingColorSpace())); |
| 93 float stdX = getFilter()->applyHorizontalScale(m_stdX); | 86 float stdX = getFilter()->applyHorizontalScale(m_stdX); |
| 94 float stdY = getFilter()->applyVerticalScale(m_stdY); | 87 float stdY = getFilter()->applyVerticalScale(m_stdY); |
| 95 SkImageFilter::CropRect rect = getCropRect(); | 88 SkImageFilter::CropRect rect = getCropRect(); |
| 96 return SkBlurImageFilter::Make(SkFloatToScalar(stdX), SkFloatToScalar(stdY), | 89 return SkBlurImageFilter::Make(SkFloatToScalar(stdX), SkFloatToScalar(stdY), |
| 97 std::move(input), &rect); | 90 std::move(input), &rect); |
| 98 } | 91 } |
| 99 | 92 |
| 100 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, | 93 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, |
| 101 int indent) const { | 94 int indent) const { |
| 102 writeIndent(ts, indent); | 95 writeIndent(ts, indent); |
| 103 ts << "[feGaussianBlur"; | 96 ts << "[feGaussianBlur"; |
| 104 FilterEffect::externalRepresentation(ts); | 97 FilterEffect::externalRepresentation(ts); |
| 105 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n"; | 98 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n"; |
| 106 inputEffect(0)->externalRepresentation(ts, indent + 1); | 99 inputEffect(0)->externalRepresentation(ts, indent + 1); |
| 107 return ts; | 100 return ts; |
| 108 } | 101 } |
| 109 | 102 |
| 110 } // namespace blink | 103 } // namespace blink |
| OLD | NEW |