/src/skia/modules/svg/src/SkSVGFeDisplacementMap.cpp
Line | Count | Source (jump to first uncovered line) |
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 "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 | | |
15 | 0 | bool SkSVGFeDisplacementMap::parseAndSetAttribute(const char* name, const char* value) { |
16 | 0 | return INHERITED::parseAndSetAttribute(name, value) || |
17 | 0 | this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) || |
18 | 0 | this->setXChannelSelector( |
19 | 0 | SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>( |
20 | 0 | "xChannelSelector", name, value)) || |
21 | 0 | this->setYChannelSelector( |
22 | 0 | SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>( |
23 | 0 | "yChannelSelector", name, value)) || |
24 | 0 | this->setScale(SkSVGAttributeParser::parse<SkSVGNumberType>("scale", name, value)); |
25 | 0 | } |
26 | | |
27 | | sk_sp<SkImageFilter> SkSVGFeDisplacementMap::onMakeImageFilter( |
28 | 0 | const SkSVGRenderContext& ctx, const SkSVGFilterContext& fctx) const { |
29 | 0 | const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx); |
30 | 0 | 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 | 0 | sk_sp<SkImageFilter> in = fctx.resolveInput(ctx, this->getIn()); |
35 | 0 | sk_sp<SkImageFilter> in2 = fctx.resolveInput(ctx, this->getIn2(), colorspace); |
36 | |
|
37 | 0 | SkScalar scale = fScale; |
38 | 0 | if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) { |
39 | 0 | const auto obbt = ctx.transformForCurrentOBB(fctx.primitiveUnits()); |
40 | 0 | scale = SkSVGLengthContext({obbt.scale.x, obbt.scale.y}) |
41 | 0 | .resolve(SkSVGLength(scale, SkSVGLength::Unit::kPercentage), |
42 | 0 | SkSVGLengthContext::LengthType::kOther); |
43 | 0 | } |
44 | |
|
45 | 0 | return SkImageFilters::DisplacementMap( |
46 | 0 | fXChannelSelector, fYChannelSelector, scale, in2, in, cropRect); |
47 | 0 | } |
48 | | |
49 | | SkSVGColorspace SkSVGFeDisplacementMap::resolveColorspace(const SkSVGRenderContext& ctx, |
50 | 0 | const SkSVGFilterContext& fctx) const { |
51 | | // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement, |
52 | | // the 'in' source image must remain in its current colorspace, which means the colorspace of |
53 | | // this FE node is the same as the input. |
54 | 0 | return fctx.resolveInputColorspace(ctx, this->getIn()); |
55 | 0 | } |
56 | | |
57 | | template <> |
58 | | bool SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>( |
59 | 0 | SkSVGFeDisplacementMap::ChannelSelector* channel) { |
60 | 0 | static constexpr std::tuple<const char*, SkSVGFeDisplacementMap::ChannelSelector> gMap[] = { |
61 | 0 | { "R", SkSVGFeDisplacementMap::ChannelSelector::kR }, |
62 | 0 | { "G", SkSVGFeDisplacementMap::ChannelSelector::kG }, |
63 | 0 | { "B", SkSVGFeDisplacementMap::ChannelSelector::kB }, |
64 | 0 | { "A", SkSVGFeDisplacementMap::ChannelSelector::kA }, |
65 | 0 | }; |
66 | |
|
67 | 0 | return this->parseEnumMap(gMap, channel) && this->parseEOSToken(); |
68 | 0 | } |