This source file includes following definitions.
- m_scale
- create
- mapPaintRect
- xChannelSelector
- setXChannelSelector
- yChannelSelector
- setYChannelSelector
- scale
- setScale
- setResultColorSpace
- transformResultColorSpace
- applySoftware
- toSkiaMode
- createImageFilter
- externalRepresentation
#include "config.h"
#include "platform/graphics/filters/FEDisplacementMap.h"
#include "SkBitmapSource.h"
#include "SkDisplacementMapEffect.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/filters/SkiaImageFilterBuilder.h"
#include "platform/graphics/skia/NativeImageSkia.h"
#include "platform/text/TextStream.h"
#include "wtf/Uint8ClampedArray.h"
namespace WebCore {
FEDisplacementMap::FEDisplacementMap(Filter* filter, ChannelSelectorType xChannelSelector, ChannelSelectorType yChannelSelector, float scale)
: FilterEffect(filter)
, m_xChannelSelector(xChannelSelector)
, m_yChannelSelector(yChannelSelector)
, m_scale(scale)
{
}
PassRefPtr<FEDisplacementMap> FEDisplacementMap::create(Filter* filter, ChannelSelectorType xChannelSelector,
ChannelSelectorType yChannelSelector, float scale)
{
return adoptRef(new FEDisplacementMap(filter, xChannelSelector, yChannelSelector, scale));
}
FloatRect FEDisplacementMap::mapPaintRect(const FloatRect& rect, bool)
{
FloatRect result = rect;
result.inflateX(filter()->applyHorizontalScale(m_scale / 2));
result.inflateY(filter()->applyVerticalScale(m_scale / 2));
return result;
}
ChannelSelectorType FEDisplacementMap::xChannelSelector() const
{
return m_xChannelSelector;
}
bool FEDisplacementMap::setXChannelSelector(const ChannelSelectorType xChannelSelector)
{
if (m_xChannelSelector == xChannelSelector)
return false;
m_xChannelSelector = xChannelSelector;
return true;
}
ChannelSelectorType FEDisplacementMap::yChannelSelector() const
{
return m_yChannelSelector;
}
bool FEDisplacementMap::setYChannelSelector(const ChannelSelectorType yChannelSelector)
{
if (m_yChannelSelector == yChannelSelector)
return false;
m_yChannelSelector = yChannelSelector;
return true;
}
float FEDisplacementMap::scale() const
{
return m_scale;
}
bool FEDisplacementMap::setScale(float scale)
{
if (m_scale == scale)
return false;
m_scale = scale;
return true;
}
void FEDisplacementMap::setResultColorSpace(ColorSpace)
{
FilterEffect::setResultColorSpace(inputEffect(0)->resultColorSpace());
}
void FEDisplacementMap::transformResultColorSpace(FilterEffect* in, const int index)
{
if (index)
in->transformResultColorSpace(operatingColorSpace());
}
void FEDisplacementMap::applySoftware()
{
FilterEffect* in = inputEffect(0);
FilterEffect* in2 = inputEffect(1);
ASSERT(m_xChannelSelector != CHANNEL_UNKNOWN);
ASSERT(m_yChannelSelector != CHANNEL_UNKNOWN);
Uint8ClampedArray* dstPixelArray = createPremultipliedImageResult();
if (!dstPixelArray)
return;
IntRect effectADrawingRect = requestedRegionOfInputImageData(in->absolutePaintRect());
RefPtr<Uint8ClampedArray> srcPixelArrayA = in->asPremultipliedImage(effectADrawingRect);
IntRect effectBDrawingRect = requestedRegionOfInputImageData(in2->absolutePaintRect());
RefPtr<Uint8ClampedArray> srcPixelArrayB = in2->asUnmultipliedImage(effectBDrawingRect);
ASSERT(srcPixelArrayA->length() == srcPixelArrayB->length());
Filter* filter = this->filter();
IntSize paintSize = absolutePaintRect().size();
float scaleX = filter->applyHorizontalScale(m_scale);
float scaleY = filter->applyVerticalScale(m_scale);
float scaleForColorX = scaleX / 255.0;
float scaleForColorY = scaleY / 255.0;
float scaledOffsetX = 0.5 - scaleX * 0.5;
float scaledOffsetY = 0.5 - scaleY * 0.5;
int stride = paintSize.width() * 4;
for (int y = 0; y < paintSize.height(); ++y) {
int line = y * stride;
for (int x = 0; x < paintSize.width(); ++x) {
int dstIndex = line + x * 4;
int srcX = x + static_cast<int>(scaleForColorX * srcPixelArrayB->item(dstIndex + m_xChannelSelector - 1) + scaledOffsetX);
int srcY = y + static_cast<int>(scaleForColorY * srcPixelArrayB->item(dstIndex + m_yChannelSelector - 1) + scaledOffsetY);
for (unsigned channel = 0; channel < 4; ++channel) {
if (srcX < 0 || srcX >= paintSize.width() || srcY < 0 || srcY >= paintSize.height()) {
dstPixelArray->set(dstIndex + channel, static_cast<unsigned char>(0));
} else {
unsigned char pixelValue = srcPixelArrayA->item(srcY * stride + srcX * 4 + channel);
dstPixelArray->set(dstIndex + channel, pixelValue);
}
}
}
}
}
static SkDisplacementMapEffect::ChannelSelectorType toSkiaMode(ChannelSelectorType type)
{
switch (type) {
case CHANNEL_R:
return SkDisplacementMapEffect::kR_ChannelSelectorType;
case CHANNEL_G:
return SkDisplacementMapEffect::kG_ChannelSelectorType;
case CHANNEL_B:
return SkDisplacementMapEffect::kB_ChannelSelectorType;
case CHANNEL_A:
return SkDisplacementMapEffect::kA_ChannelSelectorType;
case CHANNEL_UNKNOWN:
default:
return SkDisplacementMapEffect::kUnknown_ChannelSelectorType;
}
}
PassRefPtr<SkImageFilter> FEDisplacementMap::createImageFilter(SkiaImageFilterBuilder* builder)
{
RefPtr<SkImageFilter> color = builder->build(inputEffect(0), operatingColorSpace());
RefPtr<SkImageFilter> displ = builder->build(inputEffect(1), operatingColorSpace());
SkDisplacementMapEffect::ChannelSelectorType typeX = toSkiaMode(m_xChannelSelector);
SkDisplacementMapEffect::ChannelSelectorType typeY = toSkiaMode(m_yChannelSelector);
SkImageFilter::CropRect cropRect = getCropRect(builder->cropOffset());
return adoptRef(new SkDisplacementMapEffect(typeX, typeY, SkFloatToScalar(filter()->applyHorizontalScale(m_scale)), displ.get(), color.get(), &cropRect));
}
static TextStream& operator<<(TextStream& ts, const ChannelSelectorType& type)
{
switch (type) {
case CHANNEL_UNKNOWN:
ts << "UNKNOWN";
break;
case CHANNEL_R:
ts << "RED";
break;
case CHANNEL_G:
ts << "GREEN";
break;
case CHANNEL_B:
ts << "BLUE";
break;
case CHANNEL_A:
ts << "ALPHA";
break;
}
return ts;
}
TextStream& FEDisplacementMap::externalRepresentation(TextStream& ts, int indent) const
{
writeIndent(ts, indent);
ts << "[feDisplacementMap";
FilterEffect::externalRepresentation(ts);
ts << " scale=\"" << m_scale << "\" "
<< "xChannelSelector=\"" << m_xChannelSelector << "\" "
<< "yChannelSelector=\"" << m_yChannelSelector << "\"]\n";
inputEffect(0)->externalRepresentation(ts, indent + 1);
inputEffect(1)->externalRepresentation(ts, indent + 1);
return ts;
}
}