| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2010 Sencha, Inc. All rights reserved. | |
| 4 * Copyright (C) 2010 Igalia S.L. All rights reserved. | |
| 5 * Copyright (C) Research In Motion Limited 2011. All rights reserved. | |
| 6 * | |
| 7 * Redistribution and use in source and binary forms, with or without | |
| 8 * modification, are permitted provided that the following conditions | |
| 9 * are met: | |
| 10 * 1. Redistributions of source code must retain the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer. | |
| 12 * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 * notice, this list of conditions and the following disclaimer in the | |
| 14 * documentation and/or other materials provided with the distribution. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
| 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 */ | |
| 28 | |
| 29 #include "config.h" | |
| 30 #include "core/platform/graphics/ShadowBlur.h" | |
| 31 | |
| 32 #include "wtf/MathExtras.h" | |
| 33 #include <algorithm> | |
| 34 | |
| 35 using namespace std; | |
| 36 | |
| 37 namespace WebCore { | |
| 38 | |
| 39 enum { | |
| 40 leftLobe = 0, | |
| 41 rightLobe = 1 | |
| 42 }; | |
| 43 | |
| 44 ShadowBlur::ShadowBlur(const FloatSize& radius, const FloatSize& offset, const C
olor& color) | |
| 45 : m_color(color) | |
| 46 , m_blurRadius(radius) | |
| 47 , m_offset(offset) | |
| 48 , m_shadowsIgnoreTransforms(false) | |
| 49 { | |
| 50 updateShadowBlurValues(); | |
| 51 } | |
| 52 | |
| 53 void ShadowBlur::updateShadowBlurValues() | |
| 54 { | |
| 55 // Limit blur radius to 128 to avoid lots of very expensive blurring. | |
| 56 m_blurRadius = m_blurRadius.shrunkTo(FloatSize(128, 128)); | |
| 57 | |
| 58 // The type of shadow is decided by the blur radius, shadow offset, and shad
ow color. | |
| 59 if (!m_color.isValid() || !m_color.alpha()) { | |
| 60 // Can't paint the shadow with invalid or invisible color. | |
| 61 m_type = NoShadow; | |
| 62 } else if (m_blurRadius.width() > 0 || m_blurRadius.height() > 0) { | |
| 63 // Shadow is always blurred, even the offset is zero. | |
| 64 m_type = BlurShadow; | |
| 65 } else if (!m_offset.width() && !m_offset.height()) { | |
| 66 // Without blur and zero offset means the shadow is fully hidden. | |
| 67 m_type = NoShadow; | |
| 68 } else | |
| 69 m_type = SolidShadow; | |
| 70 } | |
| 71 | |
| 72 // Instead of integer division, we use 17.15 for fixed-point division. | |
| 73 static const int blurSumShift = 15; | |
| 74 | |
| 75 // Takes a two dimensional array with three rows and two columns for the lobes. | |
| 76 static void calculateLobes(int lobes[][2], float blurRadius, bool shadowsIgnoreT
ransforms) | |
| 77 { | |
| 78 int diameter; | |
| 79 if (shadowsIgnoreTransforms) | |
| 80 diameter = max(2, static_cast<int>(floorf((2 / 3.f) * blurRadius))); //
Canvas shadow. FIXME: we should adjust the blur radius higher up. | |
| 81 else { | |
| 82 // http://dev.w3.org/csswg/css3-background/#box-shadow | |
| 83 // Approximate a Gaussian blur with a standard deviation equal to half t
he blur radius, | |
| 84 // which http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement tel
l us how to do. | |
| 85 // However, shadows rendered according to that spec will extend a little
further than m_blurRadius, | |
| 86 // so we apply a fudge factor to bring the radius down slightly. | |
| 87 float stdDev = blurRadius / 2; | |
| 88 const float gaussianKernelFactor = 3 / 4.f * sqrtf(2 * piFloat); | |
| 89 const float fudgeFactor = 0.88f; | |
| 90 diameter = max(2, static_cast<int>(floorf(stdDev * gaussianKernelFactor
* fudgeFactor + 0.5f))); | |
| 91 } | |
| 92 | |
| 93 if (diameter & 1) { | |
| 94 // if d is odd, use three box-blurs of size 'd', centered on the output
pixel. | |
| 95 int lobeSize = (diameter - 1) / 2; | |
| 96 lobes[0][leftLobe] = lobeSize; | |
| 97 lobes[0][rightLobe] = lobeSize; | |
| 98 lobes[1][leftLobe] = lobeSize; | |
| 99 lobes[1][rightLobe] = lobeSize; | |
| 100 lobes[2][leftLobe] = lobeSize; | |
| 101 lobes[2][rightLobe] = lobeSize; | |
| 102 } else { | |
| 103 // if d is even, two box-blurs of size 'd' (the first one centered on th
e pixel boundary | |
| 104 // between the output pixel and the one to the left, the second one cent
ered on the pixel | |
| 105 // boundary between the output pixel and the one to the right) and one b
ox blur of size 'd+1' centered on the output pixel | |
| 106 int lobeSize = diameter / 2; | |
| 107 lobes[0][leftLobe] = lobeSize; | |
| 108 lobes[0][rightLobe] = lobeSize - 1; | |
| 109 lobes[1][leftLobe] = lobeSize - 1; | |
| 110 lobes[1][rightLobe] = lobeSize; | |
| 111 lobes[2][leftLobe] = lobeSize; | |
| 112 lobes[2][rightLobe] = lobeSize; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void ShadowBlur::blurLayerImage(unsigned char* imageData, const IntSize& size, i
nt rowStride) | |
| 117 { | |
| 118 const int channels[4] = { 3, 0, 1, 3 }; | |
| 119 | |
| 120 int lobes[3][2]; // indexed by pass, and left/right lobe | |
| 121 calculateLobes(lobes, m_blurRadius.width(), m_shadowsIgnoreTransforms); | |
| 122 | |
| 123 // First pass is horizontal. | |
| 124 int stride = 4; | |
| 125 int delta = rowStride; | |
| 126 int final = size.height(); | |
| 127 int dim = size.width(); | |
| 128 | |
| 129 // Two stages: horizontal and vertical | |
| 130 for (int pass = 0; pass < 2; ++pass) { | |
| 131 unsigned char* pixels = imageData; | |
| 132 | |
| 133 if (!pass && !m_blurRadius.width()) | |
| 134 final = 0; // Do no work if horizonal blur is zero. | |
| 135 | |
| 136 for (int j = 0; j < final; ++j, pixels += delta) { | |
| 137 // For each step, we blur the alpha in a channel and store the resul
t | |
| 138 // in another channel for the subsequent step. | |
| 139 // We use sliding window algorithm to accumulate the alpha values. | |
| 140 // This is much more efficient than computing the sum of each pixels | |
| 141 // covered by the box kernel size for each x. | |
| 142 for (int step = 0; step < 3; ++step) { | |
| 143 int side1 = lobes[step][leftLobe]; | |
| 144 int side2 = lobes[step][rightLobe]; | |
| 145 int pixelCount = side1 + 1 + side2; | |
| 146 int invCount = ((1 << blurSumShift) + pixelCount - 1) / pixelCou
nt; | |
| 147 int ofs = 1 + side2; | |
| 148 int alpha1 = pixels[channels[step]]; | |
| 149 int alpha2 = pixels[(dim - 1) * stride + channels[step]]; | |
| 150 | |
| 151 unsigned char* ptr = pixels + channels[step + 1]; | |
| 152 unsigned char* prev = pixels + stride + channels[step]; | |
| 153 unsigned char* next = pixels + ofs * stride + channels[step]; | |
| 154 | |
| 155 int i; | |
| 156 int sum = side1 * alpha1 + alpha1; | |
| 157 int limit = (dim < side2 + 1) ? dim : side2 + 1; | |
| 158 | |
| 159 for (i = 1; i < limit; ++i, prev += stride) | |
| 160 sum += *prev; | |
| 161 | |
| 162 if (limit <= side2) | |
| 163 sum += (side2 - limit + 1) * alpha2; | |
| 164 | |
| 165 limit = (side1 < dim) ? side1 : dim; | |
| 166 for (i = 0; i < limit; ptr += stride, next += stride, ++i, ++ofs
) { | |
| 167 *ptr = (sum * invCount) >> blurSumShift; | |
| 168 sum += ((ofs < dim) ? *next : alpha2) - alpha1; | |
| 169 } | |
| 170 | |
| 171 prev = pixels + channels[step]; | |
| 172 for (; ofs < dim; ptr += stride, prev += stride, next += stride,
++i, ++ofs) { | |
| 173 *ptr = (sum * invCount) >> blurSumShift; | |
| 174 sum += (*next) - (*prev); | |
| 175 } | |
| 176 | |
| 177 for (; i < dim; ptr += stride, prev += stride, ++i) { | |
| 178 *ptr = (sum * invCount) >> blurSumShift; | |
| 179 sum += alpha2 - (*prev); | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 // Last pass is vertical. | |
| 185 stride = rowStride; | |
| 186 delta = 4; | |
| 187 final = size.width(); | |
| 188 dim = size.height(); | |
| 189 | |
| 190 if (!m_blurRadius.height()) | |
| 191 break; | |
| 192 | |
| 193 if (m_blurRadius.width() != m_blurRadius.height()) | |
| 194 calculateLobes(lobes, m_blurRadius.height(), m_shadowsIgnoreTransfor
ms); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 } // namespace WebCore | |
| OLD | NEW |