• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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/skottie/src/effects/Effects.h"
9 
10 #include "include/effects/SkColorMatrix.h"
11 #include "modules/skottie/src/SkottieValue.h"
12 #include "modules/sksg/include/SkSGColorFilter.h"
13 #include "src/utils/SkJSON.h"
14 
15 namespace skottie {
16 namespace internal {
17 
18 namespace {
19 
20 class InvertEffectAdapter final : public AnimatablePropertyContainer {
21 public:
Make(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer,const AnimationBuilder * abuilder)22     static sk_sp<InvertEffectAdapter> Make(const skjson::ArrayValue& jprops,
23                                            sk_sp<sksg::RenderNode> layer,
24                                            const AnimationBuilder* abuilder) {
25         return sk_sp<InvertEffectAdapter>(
26                     new InvertEffectAdapter(jprops, std::move(layer), abuilder));
27     }
28 
node() const29     const sk_sp<sksg::ExternalColorFilter>& node() const { return fColorFilter; }
30 
31 private:
InvertEffectAdapter(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer,const AnimationBuilder * abuilder)32     InvertEffectAdapter(const skjson::ArrayValue& jprops,
33                         sk_sp<sksg::RenderNode> layer,
34                         const AnimationBuilder* abuilder)
35         : fColorFilter(sksg::ExternalColorFilter::Make(std::move(layer))) {
36         enum : size_t {
37             kChannel_Index = 0,
38         };
39 
40         EffectBinder(jprops, *abuilder, this).bind(kChannel_Index, fChannel);
41     }
42 
onSync()43     void onSync() override {
44         enum class CS { kRGB, kHSL, kYIQ };
45 
46         struct STColorMatrix {
47             std::array<float,4> scale,
48                                 trans;
49             CS                  cs;
50         };
51 
52         const auto stcm = [this]() -> STColorMatrix {
53             // https://helpx.adobe.com/after-effects/using/channel-effects.html#invert_effect
54             enum : uint8_t {
55                 kRGB_Channel =  1,
56                   kR_Channel =  2,
57                   kG_Channel =  3,
58                   kB_Channel =  4,
59 
60                 // NB: HLS vs. HSL
61                 kHLS_Channel =  6,
62                   kH_Channel =  7,
63                   kL_Channel =  8,
64                   kS_Channel =  9,
65 
66                 kYIQ_Channel = 11,
67                   kY_Channel = 12,
68                   kI_Channel = 13,
69                   kQ_Channel = 14,
70 
71                   kA_Channel = 16,
72             };
73 
74             switch (static_cast<uint8_t>(fChannel)) {
75             case   kR_Channel: return { {-1, 1, 1, 1}, {  1,0,0,0}, CS::kRGB }; // r' = 1 - r
76             case   kG_Channel: return { { 1,-1, 1, 1}, {  0,1,0,0}, CS::kRGB }; // g' = 1 - g
77             case   kB_Channel: return { { 1, 1,-1, 1}, {  0,0,1,0}, CS::kRGB }; // b' = 1 - b
78             case   kA_Channel: return { { 1, 1, 1,-1}, {  0,0,0,1}, CS::kRGB }; // a' = 1 - a
79             case kRGB_Channel: return { {-1,-1,-1, 1}, {  1,1,1,0}, CS::kRGB };
80 
81             case   kH_Channel: return { {-1, 1, 1, 1}, {.5f,0,0,0}, CS::kHSL }; // h' = .5 - h
82             case   kS_Channel: return { { 1,-1, 1, 1}, {  0,1,0,0}, CS::kHSL }; // s' = 1 - s
83             case   kL_Channel: return { { 1, 1,-1, 1}, {  0,0,1,0}, CS::kHSL }; // l' = 1 - l
84             case kHLS_Channel: return { {-1,-1,-1, 1}, {.5f,1,1,0}, CS::kHSL };
85 
86             case   kY_Channel: return { {-1, 1, 1, 1}, {  1,0,0,0}, CS::kYIQ }; // y' = 1 - y
87             case   kI_Channel: return { { 1,-1, 1, 1}, {  0,0,0,0}, CS::kYIQ }; // i' = -i
88             case   kQ_Channel: return { { 1, 1,-1, 1}, {  0,0,0,0}, CS::kYIQ }; // q' = -q
89             case kYIQ_Channel: return { {-1,-1,-1, 1}, {  1,0,0,0}, CS::kYIQ };
90 
91                       default: return { { 1, 1, 1, 1}, {  0,0,0,0}, CS::kRGB };
92             }
93 
94             SkUNREACHABLE;
95         }();
96 
97         SkColorMatrix m(
98             stcm.scale[0],             0,             0,             0, stcm.trans[0],
99                         0, stcm.scale[1],             0,             0, stcm.trans[1],
100                         0,             0, stcm.scale[2],             0, stcm.trans[2],
101                         0,             0,             0, stcm.scale[3], stcm.trans[3]
102 
103         );
104 
105         if (stcm.cs == CS::kYIQ) {
106             // https://en.wikipedia.org/wiki/YIQ
107             static constexpr SkColorMatrix RGB2YIQ(
108                 0.2990f,  0.5870f,  0.1140f, 0, 0,
109                 0.5959f, -0.2746f, -0.3213f, 0, 0,
110                 0.2115f, -0.5227f,  0.3112f, 0, 0,
111                       0,        0,        0, 1, 0
112             );
113             static constexpr SkColorMatrix YIQ2RGB(
114                       1,  0.9560f,  0.6190f, 0, 0,
115                       1, -0.2720f, -0.6470f, 0, 0,
116                       1, -1.1060f,  1.7030f, 0, 0,
117                       0,        0,        0, 1, 0
118             );
119 
120             m.preConcat (RGB2YIQ);
121             m.postConcat(YIQ2RGB);
122         }
123 
124         fColorFilter->setColorFilter(stcm.cs == CS::kHSL ? SkColorFilters::HSLAMatrix(m)
125                                                          : SkColorFilters::Matrix(m));
126     }
127 
128     const sk_sp<sksg::ExternalColorFilter> fColorFilter;
129 
130     float fChannel = 0;
131 };
132 
133 } // namespace
134 
attachInvertEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const135 sk_sp<sksg::RenderNode> EffectBuilder::attachInvertEffect(const skjson::ArrayValue& jprops,
136                                                           sk_sp<sksg::RenderNode> layer) const {
137     return fBuilder->attachDiscardableAdapter<InvertEffectAdapter>(jprops,
138                                                                    std::move(layer),
139                                                                    fBuilder);
140 }
141 
142 } // namespace internal
143 } // namespace skottie
144