2 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "FEDropShadow.h"
25 #include "ColorSpace.h"
26 #include "FEGaussianBlur.h"
28 #include "GraphicsContext.h"
29 #include "RenderTreeAsText.h"
30 #include "ShadowBlur.h"
31 #include "TextStream.h"
32 #include <wtf/ByteArray.h>
33 #include <wtf/MathExtras.h>
39 FEDropShadow::FEDropShadow(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
40 : FilterEffect(filter)
45 , m_shadowColor(shadowColor)
46 , m_shadowOpacity(shadowOpacity)
50 PassRefPtr<FEDropShadow> FEDropShadow::create(Filter* filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
52 return adoptRef(new FEDropShadow(filter, stdX, stdY, dx, dy, shadowColor, shadowOpacity));
55 void FEDropShadow::determineAbsolutePaintRect()
57 Filter* filter = this->filter();
60 FloatRect absolutePaintRect = inputEffect(0)->absolutePaintRect();
61 FloatRect absoluteOffsetPaintRect(absolutePaintRect);
62 absoluteOffsetPaintRect.move(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
63 absolutePaintRect.unite(absoluteOffsetPaintRect);
64 absolutePaintRect.intersect(maxEffectRect());
66 unsigned kernelSizeX = 0;
67 unsigned kernelSizeY = 0;
68 FEGaussianBlur::calculateKernelSize(filter, kernelSizeX, kernelSizeY, m_stdX, m_stdY);
70 // We take the half kernel size and multiply it with three, because we run box blur three times.
71 absolutePaintRect.inflateX(3 * kernelSizeX * 0.5f);
72 absolutePaintRect.inflateY(3 * kernelSizeY * 0.5f);
73 setAbsolutePaintRect(enclosingIntRect(absolutePaintRect));
76 void FEDropShadow::apply()
81 FilterEffect* in = inputEffect(0);
86 ImageBuffer* resultImage = createImageBufferResult();
90 Filter* filter = this->filter();
91 FloatSize blurRadius(filter->applyHorizontalScale(m_stdX), filter->applyVerticalScale(m_stdY));
92 FloatSize offset(filter->applyHorizontalScale(m_dx), filter->applyVerticalScale(m_dy));
94 FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
95 FloatRect drawingRegionWithOffset(drawingRegion);
96 drawingRegionWithOffset.move(offset);
98 ImageBuffer* sourceImage = in->asImageBuffer();
100 GraphicsContext* resultContext = resultImage->context();
101 ASSERT(resultContext);
102 resultContext->setAlpha(m_shadowOpacity);
103 resultContext->drawImageBuffer(sourceImage, ColorSpaceDeviceRGB, drawingRegionWithOffset);
104 resultContext->setAlpha(1);
106 ShadowBlur contextShadow(blurRadius, offset, m_shadowColor, ColorSpaceDeviceRGB);
108 // TODO: Direct pixel access to ImageBuffer would avoid copying the ImageData.
109 IntRect shadowArea(IntPoint(), resultImage->size());
110 RefPtr<ByteArray> srcPixelArray = resultImage->getPremultipliedImageData(shadowArea);
112 contextShadow.blurLayerImage(srcPixelArray->data(), shadowArea.size(), 4 * shadowArea.size().width());
114 resultImage->putPremultipliedImageData(srcPixelArray.get(), shadowArea.size(), shadowArea, IntPoint());
116 resultContext->setCompositeOperation(CompositeSourceIn);
117 resultContext->fillRect(FloatRect(FloatPoint(), absolutePaintRect().size()), m_shadowColor, ColorSpaceDeviceRGB);
118 resultContext->setCompositeOperation(CompositeDestinationOver);
120 resultImage->context()->drawImageBuffer(sourceImage, ColorSpaceDeviceRGB, drawingRegion);
123 void FEDropShadow::dump()
127 TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const
129 writeIndent(ts, indent);
130 ts << "[feDropShadow";
131 FilterEffect::externalRepresentation(ts);
132 ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\" dx=\"" << m_dx << "\" dy=\"" << m_dy << "\" flood-color=\"" << m_shadowColor.nameForRenderTreeAsText() <<"\" flood-opacity=\"" << m_shadowOpacity << "]\n";
133 inputEffect(0)->externalRepresentation(ts, indent + 1);
137 } // namespace WebCore
139 #endif // ENABLE(FILTERS)