blob: 966faa55886338c5d806fe99fcd73f47b1ec21c6 [file] [log] [blame]
Tyler Dennistonf005c8a2021-01-25 12:56:13 -05001/*
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 "include/effects/SkImageFilters.h"
9#include "modules/svg/include/SkSVGAttributeParser.h"
10#include "modules/svg/include/SkSVGFeDisplacementMap.h"
11#include "modules/svg/include/SkSVGFilterContext.h"
12#include "modules/svg/include/SkSVGRenderContext.h"
13#include "modules/svg/include/SkSVGValue.h"
14
15bool SkSVGFeDisplacementMap::parseAndSetAttribute(const char* name, const char* value) {
16 return INHERITED::parseAndSetAttribute(name, value) ||
17 this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) ||
18 this->setXChannelSelector(
19 SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
20 "xChannelSelector", name, value)) ||
21 this->setYChannelSelector(
22 SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
23 "yChannelSelector", name, value)) ||
24 this->setScale(SkSVGAttributeParser::parse<SkSVGNumberType>("scale", name, value));
25}
26
27sk_sp<SkImageFilter> SkSVGFeDisplacementMap::onMakeImageFilter(
28 const SkSVGRenderContext& ctx, const SkSVGFilterContext& fctx) const {
29 const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
30 const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
31
32 // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement,
33 // the 'in' source image must remain in its current colorspace.
34 sk_sp<SkImageFilter> in = fctx.resolveInput(ctx, this->getIn());
35 sk_sp<SkImageFilter> in2 = fctx.resolveInput(ctx, this->getIn2(), colorspace);
36
37 SkScalar scale = fScale;
38 if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) {
39 SkASSERT(ctx.node());
40 const SkRect objBounds = ctx.node()->objectBoundingBox(ctx);
41 const SkSVGLengthContext lctx({objBounds.width(), objBounds.height()});
42 scale = lctx.resolve(SkSVGLength(scale, SkSVGLength::Unit::kPercentage),
43 SkSVGLengthContext::LengthType::kOther);
44 }
45
46 return SkImageFilters::DisplacementMap(
47 fXChannelSelector, fYChannelSelector, scale, in2, in, cropRect);
48}
49
50SkSVGColorspace SkSVGFeDisplacementMap::resolveColorspace(const SkSVGRenderContext& ctx,
51 const SkSVGFilterContext& fctx) const {
52 // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement,
53 // the 'in' source image must remain in its current colorspace, which means the colorspace of
54 // this FE node is the same as the input.
55 return fctx.resolveInputColorspace(ctx, this->getIn());
56}
57
58template <>
59bool SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
60 SkSVGFeDisplacementMap::ChannelSelector* channel) {
61 static constexpr std::tuple<const char*, SkSVGFeDisplacementMap::ChannelSelector> gMap[] = {
62 { "R", SkSVGFeDisplacementMap::ChannelSelector::kR },
63 { "G", SkSVGFeDisplacementMap::ChannelSelector::kG },
64 { "B", SkSVGFeDisplacementMap::ChannelSelector::kB },
65 { "A", SkSVGFeDisplacementMap::ChannelSelector::kA },
66 };
67
68 return this->parseEnumMap(gMap, channel) && this->parseEOSToken();
69}