| 1 |
/* |
| 2 |
* Copyright 2020 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "modules/svg/include/SkSVGFeDisplacementMap.h" |
| 9 |
|
| 10 |
#include "include/core/SkImageFilter.h" |
| 11 |
#include "include/core/SkM44.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/effects/SkImageFilters.h" |
| 15 |
#include "modules/svg/include/SkSVGAttributeParser.h" |
| 16 |
#include "modules/svg/include/SkSVGFilterContext.h" |
| 17 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 18 |
|
| 19 |
#include <tuple> |
| 20 |
|
| 21 |
bool SkSVGFeDisplacementMap::parseAndSetAttribute(const char* name, const char* value) { |
| 22 |
return INHERITED::parseAndSetAttribute(name, value) || |
| 23 |
this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) || |
| 24 |
this->setXChannelSelector( |
| 25 |
SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>( |
| 26 |
"xChannelSelector", name, value)) || |
| 27 |
this->setYChannelSelector( |
| 28 |
SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>( |
| 29 |
"yChannelSelector", name, value)) || |
| 30 |
this->setScale(SkSVGAttributeParser::parse<SkSVGNumberType>("scale", name, value)); |
| 31 |
} |
| 32 |
|
| 33 |
sk_sp<SkImageFilter> SkSVGFeDisplacementMap::onMakeImageFilter( |
| 34 |
const SkSVGRenderContext& ctx, const SkSVGFilterContext& fctx) const { |
| 35 |
const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx); |
| 36 |
const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx); |
| 37 |
|
| 38 |
// According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement, |
| 39 |
// the 'in' source image must remain in its current colorspace. |
| 40 |
sk_sp<SkImageFilter> in = fctx.resolveInput(ctx, this->getIn()); |
| 41 |
sk_sp<SkImageFilter> in2 = fctx.resolveInput(ctx, this->getIn2(), colorspace); |
| 42 |
|
| 43 |
SkScalar scale = fScale; |
| 44 |
if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) { |
| 45 |
const auto obbt = ctx.transformForCurrentOBB(fctx.primitiveUnits()); |
| 46 |
scale = SkSVGLengthContext({obbt.scale.x, obbt.scale.y}) |
| 47 |
.resolve(SkSVGLength(scale, SkSVGLength::Unit::kPercentage), |
| 48 |
SkSVGLengthContext::LengthType::kOther); |
| 49 |
} |
| 50 |
|
| 51 |
return SkImageFilters::DisplacementMap( |
| 52 |
fXChannelSelector, fYChannelSelector, scale, in2, in, cropRect); |
| 53 |
} |
| 54 |
|
| 55 |
SkSVGColorspace SkSVGFeDisplacementMap::resolveColorspace(const SkSVGRenderContext& ctx, |
| 56 |
const SkSVGFilterContext& fctx) const { |
| 57 |
// According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement, |
| 58 |
// the 'in' source image must remain in its current colorspace, which means the colorspace of |
| 59 |
// this FE node is the same as the input. |
| 60 |
return fctx.resolveInputColorspace(ctx, this->getIn()); |
| 61 |
} |
| 62 |
|
| 63 |
template <> |
| 64 |
bool SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>( |
| 65 |
SkSVGFeDisplacementMap::ChannelSelector* channel) { |
| 66 |
static constexpr std::tuple<const char*, SkSVGFeDisplacementMap::ChannelSelector> gMap[] = { |
| 67 |
{ "R", SkSVGFeDisplacementMap::ChannelSelector::kR }, |
| 68 |
{ "G", SkSVGFeDisplacementMap::ChannelSelector::kG }, |
| 69 |
{ "B", SkSVGFeDisplacementMap::ChannelSelector::kB }, |
| 70 |
{ "A", SkSVGFeDisplacementMap::ChannelSelector::kA }, |
| 71 |
}; |
| 72 |
|
| 73 |
return this->parseEnumMap(gMap, channel) && this->parseEOSToken(); |
| 74 |
} |