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 // Limit the kernel size to 1000. A bigger radius won't make a big difference | |
47 // for the result image but inflates the absolute paint rect too much. This is | |
48 // compatible with Firefox' behavior. | |
Stephen Chennney
2016/10/10 19:43:25
Where do we do this limiting? It would be nice of
fs
2016/10/10 20:40:03
It looks like this comment has outstayed its welco
fs
2016/10/10 20:43:53
Dropped.
| |
49 if (std.width()) { | |
50 int size = std::max<unsigned>(2, approximateBoxWidth(std.width())); | |
51 kernelSize.setWidth(size); | |
52 } | |
53 if (std.height()) { | |
54 int size = std::max<unsigned>(2, approximateBoxWidth(std.height())); | |
55 kernelSize.setHeight(size); | |
56 } | |
57 return kernelSize; | |
58 } | |
59 | |
60 } | |
39 | 61 |
40 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y) | 62 FEGaussianBlur::FEGaussianBlur(Filter* filter, float x, float y) |
41 : FilterEffect(filter), m_stdX(x), m_stdY(y) {} | 63 : FilterEffect(filter), m_stdX(x), m_stdY(y) {} |
42 | 64 |
43 FEGaussianBlur* FEGaussianBlur::create(Filter* filter, float x, float y) { | 65 FEGaussianBlur* FEGaussianBlur::create(Filter* filter, float x, float y) { |
44 return new FEGaussianBlur(filter, x, y); | 66 return new FEGaussianBlur(filter, x, y); |
45 } | 67 } |
46 | 68 |
47 IntSize FEGaussianBlur::calculateUnscaledKernelSize(const FloatPoint& std) { | 69 FloatRect FEGaussianBlur::mapEffect(const FloatSize& stdDeviation, |
48 ASSERT(std.x() >= 0 && std.y() >= 0); | 70 const FloatRect& rect) { |
49 | 71 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 | 72 // We take the half kernel size and multiply it by three, because we run box |
83 // blur three times. | 73 // blur three times. |
84 FloatRect result = rect; | 74 FloatRect result = rect; |
85 result.inflateX(3.0f * kernelSize.width() * 0.5f); | 75 result.inflateX(3.0f * kernelSize.width() * 0.5f); |
86 result.inflateY(3.0f * kernelSize.height() * 0.5f); | 76 result.inflateY(3.0f * kernelSize.height() * 0.5f); |
87 return result; | 77 return result; |
88 } | 78 } |
89 | 79 |
80 FloatRect FEGaussianBlur::mapEffect(const FloatRect& rect) const { | |
81 FloatSize stdError(getFilter()->applyHorizontalScale(m_stdX), | |
82 getFilter()->applyVerticalScale(m_stdY)); | |
83 return mapEffect(stdError, rect); | |
84 } | |
85 | |
90 sk_sp<SkImageFilter> FEGaussianBlur::createImageFilter() { | 86 sk_sp<SkImageFilter> FEGaussianBlur::createImageFilter() { |
91 sk_sp<SkImageFilter> input( | 87 sk_sp<SkImageFilter> input( |
92 SkiaImageFilterBuilder::build(inputEffect(0), operatingColorSpace())); | 88 SkiaImageFilterBuilder::build(inputEffect(0), operatingColorSpace())); |
93 float stdX = getFilter()->applyHorizontalScale(m_stdX); | 89 float stdX = getFilter()->applyHorizontalScale(m_stdX); |
94 float stdY = getFilter()->applyVerticalScale(m_stdY); | 90 float stdY = getFilter()->applyVerticalScale(m_stdY); |
95 SkImageFilter::CropRect rect = getCropRect(); | 91 SkImageFilter::CropRect rect = getCropRect(); |
96 return SkBlurImageFilter::Make(SkFloatToScalar(stdX), SkFloatToScalar(stdY), | 92 return SkBlurImageFilter::Make(SkFloatToScalar(stdX), SkFloatToScalar(stdY), |
97 std::move(input), &rect); | 93 std::move(input), &rect); |
98 } | 94 } |
99 | 95 |
100 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, | 96 TextStream& FEGaussianBlur::externalRepresentation(TextStream& ts, |
101 int indent) const { | 97 int indent) const { |
102 writeIndent(ts, indent); | 98 writeIndent(ts, indent); |
103 ts << "[feGaussianBlur"; | 99 ts << "[feGaussianBlur"; |
104 FilterEffect::externalRepresentation(ts); | 100 FilterEffect::externalRepresentation(ts); |
105 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n"; | 101 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\"]\n"; |
106 inputEffect(0)->externalRepresentation(ts, indent + 1); | 102 inputEffect(0)->externalRepresentation(ts, indent + 1); |
107 return ts; | 103 return ts; |
108 } | 104 } |
109 | 105 |
110 } // namespace blink | 106 } // namespace blink |
OLD | NEW |