1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "effect/image_filter.h"
17
18 #include "impl_factory.h"
19
20 #include "impl_interface/image_filter_impl.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
ImageFilter(FilterType t,scalar x,scalar y,std::shared_ptr<ImageFilter> input,const Rect & cropRect)25 ImageFilter::ImageFilter(FilterType t, scalar x, scalar y,
26 std::shared_ptr<ImageFilter> input, const Rect& cropRect) noexcept : ImageFilter()
27 {
28 type_ = t;
29 impl_->InitWithOffset(x, y, input, cropRect);
30 }
31
ImageFilter(FilterType t,scalar x,scalar y,TileMode mode,std::shared_ptr<ImageFilter> input,ImageBlurType blurType,const Rect & cropRect)32 ImageFilter::ImageFilter(FilterType t, scalar x, scalar y, TileMode mode, std::shared_ptr<ImageFilter> input,
33 ImageBlurType blurType, const Rect& cropRect) noexcept
34 : ImageFilter()
35 {
36 type_ = t;
37 impl_->InitWithBlur(x, y, mode, input, blurType, cropRect);
38 }
39
ImageFilter(FilterType t,const ColorFilter & cf,std::shared_ptr<ImageFilter> input,const Rect & cropRect)40 ImageFilter::ImageFilter(FilterType t, const ColorFilter& cf,
41 std::shared_ptr<ImageFilter> input, const Rect& cropRect) noexcept
42 : ImageFilter()
43 {
44 type_ = t;
45 impl_->InitWithColor(cf, input, cropRect);
46 }
47
ImageFilter(FilterType t,const ColorFilter & cf,scalar x,scalar y,ImageBlurType blurType,const Rect & cropRect)48 ImageFilter::ImageFilter(FilterType t, const ColorFilter& cf, scalar x, scalar y,
49 ImageBlurType blurType, const Rect& cropRect) noexcept
50 : ImageFilter()
51 {
52 type_ = t;
53 impl_->InitWithColorBlur(cf, x, y, blurType, cropRect);
54 }
55
InitWithColorBlur(const ColorFilter & cf,scalar x,scalar y,ImageBlurType blurType,const Rect & cropRect)56 void ImageFilter::InitWithColorBlur(const ColorFilter& cf, scalar x, scalar y,
57 ImageBlurType blurType, const Rect& cropRect)
58 {
59 type_ = ImageFilter::FilterType::COLOR_FILTER;
60 impl_->InitWithColorBlur(cf, x, y, blurType, cropRect);
61 }
62
ImageFilter(FilterType t,const std::vector<scalar> & coefficients,bool enforcePMColor,std::shared_ptr<ImageFilter> background,std::shared_ptr<ImageFilter> foreground,const Rect & cropRect)63 ImageFilter::ImageFilter(FilterType t, const std::vector<scalar>& coefficients, bool enforcePMColor,
64 std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground, const Rect& cropRect) noexcept
65 :ImageFilter()
66 {
67 type_ = t;
68 impl_->InitWithArithmetic(coefficients, enforcePMColor, background, foreground, cropRect);
69 }
70
ImageFilter(FilterType t,std::shared_ptr<ImageFilter> f1,std::shared_ptr<ImageFilter> f2)71 ImageFilter::ImageFilter(FilterType t, std::shared_ptr<ImageFilter> f1, std::shared_ptr<ImageFilter> f2) noexcept
72 : ImageFilter()
73 {
74 type_ = t;
75 impl_->InitWithCompose(f1, f2);
76 }
77
ImageFilter(FilterType t,float radius,const std::vector<std::pair<float,float>> & fractionStops,GradientDir direction,GradientBlurType blurType,std::shared_ptr<ImageFilter> input)78 ImageFilter::ImageFilter(FilterType t, float radius, const std::vector<std::pair<float, float>>& fractionStops,
79 GradientDir direction, GradientBlurType blurType, std::shared_ptr<ImageFilter> input) noexcept
80 : ImageFilter()
81 {
82 type_ = t;
83 impl_->InitWithGradientBlur(radius, fractionStops, direction, blurType, input);
84 }
85
ImageFilter(FilterType t)86 ImageFilter::ImageFilter(FilterType t) noexcept
87 : type_(t), impl_(ImplFactory::CreateImageFilterImpl())
88 {}
89
ImageFilter()90 ImageFilter::ImageFilter() noexcept
91 : type_(ImageFilter::FilterType::NO_TYPE), impl_(ImplFactory::CreateImageFilterImpl())
92 {}
93
GetType() const94 ImageFilter::FilterType ImageFilter::GetType() const
95 {
96 return type_;
97 }
98
CreateBlurImageFilter(scalar sigmaX,scalar sigmaY,TileMode mode,std::shared_ptr<ImageFilter> input,ImageBlurType blurType,const Rect & cropRect)99 std::shared_ptr<ImageFilter> ImageFilter::CreateBlurImageFilter(scalar sigmaX, scalar sigmaY, TileMode mode,
100 std::shared_ptr<ImageFilter> input, ImageBlurType blurType, const Rect& cropRect)
101 {
102 return std::make_shared<ImageFilter>(ImageFilter::FilterType::BLUR, sigmaX, sigmaY,
103 mode, input, blurType, cropRect);
104 }
105
CreateColorFilterImageFilter(const ColorFilter & cf,std::shared_ptr<ImageFilter> input,const Rect & cropRect)106 std::shared_ptr<ImageFilter> ImageFilter::CreateColorFilterImageFilter(
107 const ColorFilter& cf, std::shared_ptr<ImageFilter> input, const Rect& cropRect)
108 {
109 return std::make_shared<ImageFilter>(ImageFilter::FilterType::COLOR_FILTER, cf, input, cropRect);
110 }
111
CreateColorBlurImageFilter(const ColorFilter & cf,scalar sigmaX,scalar sigmaY,ImageBlurType blurType,const Rect & cropRect)112 std::shared_ptr<ImageFilter> ImageFilter::CreateColorBlurImageFilter(const ColorFilter& cf,
113 scalar sigmaX, scalar sigmaY, ImageBlurType blurType, const Rect& cropRect)
114 {
115 return std::make_shared<ImageFilter>(ImageFilter::FilterType::COLOR_FILTER, cf,
116 sigmaX, sigmaY, blurType, cropRect);
117 }
118
CreateOffsetImageFilter(scalar dx,scalar dy,std::shared_ptr<ImageFilter> input,const Rect & cropRect)119 std::shared_ptr<ImageFilter> ImageFilter::CreateOffsetImageFilter(
120 scalar dx, scalar dy, std::shared_ptr<ImageFilter> input, const Rect& cropRect)
121 {
122 return std::make_shared<ImageFilter>(ImageFilter::FilterType::OFFSET,
123 dx, dy, input, cropRect);
124 }
125
CreateGradientBlurImageFilter(float radius,const std::vector<std::pair<float,float>> & fractionStops,GradientDir direction,GradientBlurType blurType,std::shared_ptr<ImageFilter> input)126 std::shared_ptr<ImageFilter> ImageFilter::CreateGradientBlurImageFilter(float radius,
127 const std::vector<std::pair<float, float>>& fractionStops, GradientDir direction,
128 GradientBlurType blurType, std::shared_ptr<ImageFilter> input)
129 {
130 return std::make_shared<ImageFilter>(
131 ImageFilter::FilterType::GRADIENT_BLUR, radius, fractionStops, direction, blurType, input);
132 }
133
CreateArithmeticImageFilter(const std::vector<scalar> & coefficients,bool enforcePMColor,std::shared_ptr<ImageFilter> background,std::shared_ptr<ImageFilter> foreground,const Rect & cropRect)134 std::shared_ptr<ImageFilter> ImageFilter::CreateArithmeticImageFilter(const std::vector<scalar>& coefficients,
135 bool enforcePMColor, std::shared_ptr<ImageFilter> background,
136 std::shared_ptr<ImageFilter> foreground, const Rect& cropRect)
137 {
138 return std::make_shared<ImageFilter>(
139 ImageFilter::FilterType::ARITHMETIC, coefficients, enforcePMColor, background, foreground, cropRect);
140 }
141
CreateComposeImageFilter(std::shared_ptr<ImageFilter> f1,std::shared_ptr<ImageFilter> f2)142 std::shared_ptr<ImageFilter> ImageFilter::CreateComposeImageFilter(
143 std::shared_ptr<ImageFilter> f1, std::shared_ptr<ImageFilter> f2)
144 {
145 return std::make_shared<ImageFilter>(ImageFilter::FilterType::COMPOSE, f1, f2);
146 }
147
Serialize() const148 std::shared_ptr<Data> ImageFilter::Serialize() const
149 {
150 return impl_->Serialize();
151 }
152
Deserialize(std::shared_ptr<Data> data)153 bool ImageFilter::Deserialize(std::shared_ptr<Data> data)
154 {
155 return impl_->Deserialize(data);
156 }
157
CreateBlendImageFilter(BlendMode mode,std::shared_ptr<ImageFilter> background,std::shared_ptr<ImageFilter> foreground,const Rect & cropRect)158 std::shared_ptr<ImageFilter> ImageFilter::CreateBlendImageFilter(BlendMode mode,
159 std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground, const Rect& cropRect)
160 {
161 return std::make_shared<ImageFilter>(ImageFilter::FilterType::BLEND, mode, background, foreground, cropRect);
162 }
163
ImageFilter(FilterType t,BlendMode mode,std::shared_ptr<ImageFilter> background,std::shared_ptr<ImageFilter> foreground,const Rect & cropRect)164 ImageFilter::ImageFilter(FilterType t, BlendMode mode, std::shared_ptr<ImageFilter> background,
165 std::shared_ptr<ImageFilter> foreground, const Rect& cropRect) noexcept
166 : ImageFilter()
167 {
168 type_ = t;
169 impl_->InitWithBlend(mode, cropRect, background, foreground);
170 }
171
CreateShaderImageFilter(std::shared_ptr<ShaderEffect> shader,const Rect & cropRect)172 std::shared_ptr<ImageFilter> ImageFilter::CreateShaderImageFilter(
173 std::shared_ptr<ShaderEffect> shader, const Rect& cropRect)
174 {
175 return std::make_shared<ImageFilter>(ImageFilter::FilterType::SHADER, shader, cropRect);
176 }
177
ImageFilter(FilterType t,std::shared_ptr<ShaderEffect> shader,const Rect & cropRect)178 ImageFilter::ImageFilter(FilterType t, std::shared_ptr<ShaderEffect> shader, const Rect& cropRect) noexcept
179 : ImageFilter()
180 {
181 type_ = t;
182 impl_->InitWithShader(shader, cropRect);
183 }
184
185 } // namespace Drawing
186 } // namespace Rosen
187 } // namespace OHOS