#include "config.h"
#include "FEDropShadow.h"
#include "FEGaussianBlur.h"
#include "Filter.h"
#include "GraphicsContext.h"
#include "ShadowBlur.h"
#include "TextStream.h"
#include <runtime/Uint8ClampedArray.h>
#include <wtf/MathExtras.h>
namespace WebCore {
FEDropShadow::FEDropShadow(Filter& filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
: FilterEffect(filter)
, m_stdX(stdX)
, m_stdY(stdY)
, m_dx(dx)
, m_dy(dy)
, m_shadowColor(shadowColor)
, m_shadowOpacity(shadowOpacity)
{
}
Ref<FEDropShadow> FEDropShadow::create(Filter& filter, float stdX, float stdY, float dx, float dy, const Color& shadowColor, float shadowOpacity)
{
return adoptRef(*new FEDropShadow(filter, stdX, stdY, dx, dy, shadowColor, shadowOpacity));
}
void FEDropShadow::determineAbsolutePaintRect()
{
Filter& filter = this->filter();
FloatRect absolutePaintRect = inputEffect(0)->absolutePaintRect();
FloatRect absoluteOffsetPaintRect(absolutePaintRect);
absoluteOffsetPaintRect.move(filter.applyHorizontalScale(m_dx), filter.applyVerticalScale(m_dy));
absolutePaintRect.unite(absoluteOffsetPaintRect);
IntSize kernelSize = FEGaussianBlur::calculateKernelSize(filter, FloatPoint(m_stdX, m_stdY));
absolutePaintRect.inflateX(3 * kernelSize.width() * 0.5f);
absolutePaintRect.inflateY(3 * kernelSize.height() * 0.5f);
if (clipsToBounds())
absolutePaintRect.intersect(maxEffectRect());
else
absolutePaintRect.unite(maxEffectRect());
setAbsolutePaintRect(enclosingIntRect(absolutePaintRect));
}
void FEDropShadow::platformApplySoftware()
{
FilterEffect* in = inputEffect(0);
ImageBuffer* resultImage = createImageBufferResult();
if (!resultImage)
return;
Filter& filter = this->filter();
FloatSize blurRadius(2 * filter.applyHorizontalScale(m_stdX), 2 * filter.applyVerticalScale(m_stdY));
blurRadius.scale(filter.filterScale());
FloatSize offset(filter.applyHorizontalScale(m_dx), filter.applyVerticalScale(m_dy));
FloatRect drawingRegion = drawingRegionOfInputImage(in->absolutePaintRect());
FloatRect drawingRegionWithOffset(drawingRegion);
drawingRegionWithOffset.move(offset);
ImageBuffer* sourceImage = in->asImageBuffer();
if (!sourceImage)
return;
GraphicsContext& resultContext = resultImage->context();
resultContext.setAlpha(m_shadowOpacity);
resultContext.drawImageBuffer(*sourceImage, drawingRegionWithOffset);
resultContext.setAlpha(1);
ShadowBlur contextShadow(blurRadius, offset, m_shadowColor);
IntRect shadowArea(IntPoint(), resultImage->internalSize());
RefPtr<Uint8ClampedArray> srcPixelArray = resultImage->getPremultipliedImageData(shadowArea, nullptr, ImageBuffer::BackingStoreCoordinateSystem);
contextShadow.blurLayerImage(srcPixelArray->data(), shadowArea.size(), 4 * shadowArea.size().width());
resultImage->putByteArray(Premultiplied, srcPixelArray.get(), shadowArea.size(), shadowArea, IntPoint(), ImageBuffer::BackingStoreCoordinateSystem);
resultContext.setCompositeOperation(CompositeSourceIn);
resultContext.fillRect(FloatRect(FloatPoint(), absolutePaintRect().size()), m_shadowColor);
resultContext.setCompositeOperation(CompositeDestinationOver);
resultImage->context().drawImageBuffer(*sourceImage, drawingRegion);
}
void FEDropShadow::dump()
{
}
TextStream& FEDropShadow::externalRepresentation(TextStream& ts, int indent) const
{
writeIndent(ts, indent);
ts << "[feDropShadow";
FilterEffect::externalRepresentation(ts);
ts << " stdDeviation=\"" << m_stdX << ", " << m_stdY << "\" dx=\"" << m_dx << "\" dy=\"" << m_dy << "\" flood-color=\"" << m_shadowColor.nameForRenderTreeAsText() <<"\" flood-opacity=\"" << m_shadowOpacity << "]\n";
inputEffect(0)->externalRepresentation(ts, indent + 1);
return ts;
}
}