• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 #ifndef IMAGE_FILTER_H
17 #define IMAGE_FILTER_H
18 
19 #include "effect/color_filter.h"
20 #include "drawing/engine_adapter/impl_interface/image_filter_impl.h"
21 #include "utils/drawing_macros.h"
22 #include "utils/sampling_options.h"
23 #include "utils/scalar.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class ImageFilterImpl;
29 
30 static const Rect noCropRect = {
31     -std::numeric_limits<scalar>::infinity(), -std::numeric_limits<scalar>::infinity(),
32     std::numeric_limits<scalar>::infinity(), std::numeric_limits<scalar>::infinity()
33 };
34 
35 class DRAWING_API ImageFilter {
36 public:
37     enum class FilterType {
38         NO_TYPE,
39         BLUR,
40         COLOR_FILTER,
41         OFFSET,
42         ARITHMETIC,
43         COMPOSE,
44         GRADIENT_BLUR,
45         BLEND,
46         SHADER,
47         IMAGE,
48         HD_SAMPLE, // high definition sample
49         LAZY_IMAGE_FILTER
50     };
51     /**
52      * @brief Create a filter that blurs its input by the separate X and Y sinma value.
53      * @param sigmaX     The Gaussian sigma value for blurring along the X axis.
54      * @param sigmaY     The Gaussian sigma value for blurring along the Y axis.
55      * @param mode       The tile mode applied at edges.
56      * @param input      The input filter that is blurred, uses source bitmap if this is null.
57      * @param blurType   The BlurType of Image, default as GAUSS.
58      * @param cropRect   Optional rectangle that crops the input and output
59      * @return           A shared pointer to ImageFilter that its type is blur.
60      */
61     static std::shared_ptr<ImageFilter> CreateBlurImageFilter(scalar sigmaX, scalar sigmaY, TileMode mode,
62         std::shared_ptr<ImageFilter> input, ImageBlurType blurType = ImageBlurType::GAUSS,
63         const Rect& cropRect = noCropRect);
64     /**
65      * @brief Create a filter that applies the color filter to the input filter results.
66      * @param cf     The color filter that transforms the input image.
67      * @param input  The input filter, or uses the source bitmap if this is null.
68      * @param cropRect   Optional rectangle that crops the input and output
69      * @return       A shared pointer to ImageFilter that its type is color.
70      */
71     static std::shared_ptr<ImageFilter> CreateColorFilterImageFilter(const ColorFilter& cf,
72         std::shared_ptr<ImageFilter> input, const Rect& cropRect = noCropRect);
73 
74     static std::shared_ptr<ImageFilter> CreateColorBlurImageFilter(const ColorFilter& cf, scalar sigmaX, scalar sigmaY,
75         ImageBlurType blurType = ImageBlurType::GAUSS, const Rect& cropRect = noCropRect);
76     /*
77      * @brief        Create a filter that offsets the input filter by the given vector.
78      * @param dx     The x offset in local space that the image is shifted.
79      * @param dy     The y offset in local space that the image is shifted.
80      * @param input  The input that will be moved, if null the source bitmap is used instead.
81      * @param cropRect   Optional rectangle that crops the input and output
82      * @return       A shared pointer to ImageFilter that its type is offset.
83      */
84     static std::shared_ptr<ImageFilter> CreateOffsetImageFilter(scalar dx, scalar dy,
85         std::shared_ptr<ImageFilter> input, const Rect& cropRect = noCropRect);
86     /**
87      * @brief Create a filter that implements a custom blend mode.
88      * @param coefficients    Get the four coefficients used to combine the foreground and background in the vector.
89                               And The vector size must be four, otherwise the call fails.
90      * @param enforcePMColor  If true, the RGB channels will be clamped to the Calculated alpha.
91      * @param background      The Background content, using the source bitmap when this is null.
92      * @param foreground      The foreground content, using the source bitmap when this is null.
93      * @param cropRect   Optional rectangle that crops the input and output
94      * @return                A shared point to ImageFilter that its type is arithmetic.
95      */
96     static std::shared_ptr<ImageFilter> CreateArithmeticImageFilter(const std::vector<scalar>& coefficients,
97         bool enforcePMColor, std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground,
98         const Rect& cropRect = noCropRect);
99     /**
100      * @brief Create a filter that composes f1 with f2.
101      * @param f1  The outer filter that evaluates the results of inner.
102      * @param f2  The inner filter that produces the input to outer.
103      * @return    A shared pointer to ImageFilter that its type is compose.
104      */
105     static std::shared_ptr<ImageFilter> CreateComposeImageFilter(std::shared_ptr<ImageFilter> f1,
106         std::shared_ptr<ImageFilter> f2);
107 
108     static std::shared_ptr<ImageFilter> CreateGradientBlurImageFilter(float radius,
109         const std::vector<std::pair<float, float>>& fractionStops, GradientDir direction,
110         GradientBlurType blurType, std::shared_ptr<ImageFilter> input);
111 
112     /**
113      * @brief This filter takes an BlendMode and uses it to composite the two filters together
114      * @param mode  The blend mode that defines the compositing operation
115      * @param background  The Dst pixels used in blending, if null the source bitmap is used.
116      * @param foreground  The Src pixels used in blending, if null the source bitmap is used.
117      * @param cropRect   Optional rectangle that crops the input and output
118      * @return    A shared pointer to ImageFilter that its type is blend.
119      */
120     static std::shared_ptr<ImageFilter> CreateBlendImageFilter(BlendMode mode,
121         std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground = nullptr,
122         const Rect& cropRect = noCropRect);
123 
124     /**
125      * @brief Create a filter that fills the output with the per-pixel evaluation of the ShaderEffect. The
126      *        shader is defined in the image filter's local coordinate system, so will automatically
127      *        be affected by Canvas's transform.
128      *
129      *        Like Image() and Picture(), this is a leaf filter that can be used to introduce inputs to
130      *        a complex filter graph, but should generally be combined with a filter that as at least
131      *        one null input to use the implicit source image.
132      * @param shader  The shader that fills the result image
133      * @param cropRect   Optional rectangle that crops the input and output
134      * @return    A shared pointer to ImageFilter that its type is shader.
135      */
136     static std::shared_ptr<ImageFilter> CreateShaderImageFilter(std::shared_ptr<ShaderEffect> shader,
137         const Rect& cropRect = noCropRect);
138 
139     /**
140      * @brief Create a filter to render the contents of the input Image with rect.
141      *
142      * @param srcRect Indicates the pointer to a src rect object.
143      * @param dstRect Indicates the pointer to a dst rect object.
144      * @return    A shared pointer to ImageFilter that its type is bitmap.
145      */
146     static std::shared_ptr<ImageFilter> CreateImageImageFilter(
147         const std::shared_ptr<Image>& image, const Rect& srcRect, const Rect& dstRect,
148         const SamplingOptions& options = SamplingOptions());
149 
150      /**
151      * @brief Create a filter that draws the 'srcRect' portion of image into 'dstRect' using HD Sampling.
152      *
153      * @param image The image that the filter will process.
154      * @param src   The source pixels sampled from the image, subset of image rect.
155      * @param dst   The local rectangle to draw the image into.
156      * @return      A shared pointer to ImageFilter that its type is hd sample.
157      */
158     static std::shared_ptr<ImageFilter> CreateHDSampleImageFilter(
159         const std::shared_ptr<Image>& image, const Rect& src, const Rect& dst, const HDSampleInfo& info);
160 
161     virtual ~ImageFilter() = default;
162     FilterType GetType() const;
IsLazy()163     virtual bool IsLazy() const { return false; }
GetDrawingType()164     virtual DrawingType GetDrawingType() const
165     {
166         return DrawingType::COMMON;
167     }
168     virtual std::shared_ptr<Data> Serialize() const;
169     virtual bool Deserialize(std::shared_ptr<Data> data);
170 #ifdef ROSEN_OHOS
171     virtual bool Marshalling(Parcel& parcel);
172     static std::shared_ptr<ImageFilter> Unmarshalling(Parcel& parcel, bool& isValid);
173 #endif
174     template<typename T>
GetImpl()175     T* GetImpl() const
176     {
177         return impl_->DowncastingTo<T>();
178     }
179 
180     ImageFilter(FilterType t, scalar x, scalar y, std::shared_ptr<ImageFilter> input,
181         const Rect& cropRect = noCropRect) noexcept;
182     ImageFilter(FilterType t, scalar x, scalar y, TileMode mode, std::shared_ptr<ImageFilter> input,
183         ImageBlurType blurType, const Rect& cropRect = noCropRect) noexcept;
184     ImageFilter(FilterType t, const ColorFilter& cf,
185         std::shared_ptr<ImageFilter> input, const Rect& cropRect = noCropRect) noexcept;
186     ImageFilter(FilterType t, const ColorFilter& cf, scalar x, scalar y,
187         ImageBlurType blurType, const Rect& cropRect = noCropRect) noexcept;
188     ImageFilter(FilterType t, const std::vector<scalar>& coefficients, bool enforcePMColor,
189         std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground,
190         const Rect& cropRect = noCropRect) noexcept;
191     ImageFilter(FilterType t, std::shared_ptr<ImageFilter> f1, std::shared_ptr<ImageFilter> f2) noexcept;
192     ImageFilter(FilterType t, float radius, const std::vector<std::pair<float, float>>& fractionStops,
193         GradientDir direction, GradientBlurType blurType, std::shared_ptr<ImageFilter> input) noexcept;
194     ImageFilter(FilterType t) noexcept;
195     void InitWithColorBlur(const ColorFilter& cf, scalar x, scalar y, ImageBlurType blurType,
196         const Rect& cropRect = noCropRect);
197     ImageFilter(FilterType t, BlendMode mode, std::shared_ptr<ImageFilter> background,
198         std::shared_ptr<ImageFilter> foreground = nullptr,
199         const Rect& cropRect = noCropRect) noexcept;
200     ImageFilter(FilterType t, std::shared_ptr<ShaderEffect> shader, const Rect& cropRect = noCropRect) noexcept;
201     ImageFilter(FilterType t, const std::shared_ptr<Image>& image, const RectF& srcRect,
202         const RectF& dstRect, const SamplingOptions& options = SamplingOptions()) noexcept;
203     ImageFilter(FilterType t, const std::shared_ptr<Image>& image,
204         const Rect& src, const Rect& dst, const HDSampleInfo& info) noexcept;
205 
206 protected:
207     ImageFilter() noexcept;
208 
209 private:
210     FilterType type_;
211     std::shared_ptr<ImageFilterImpl> impl_;
212 };
213 } // namespace Drawing
214 } // namespace Rosen
215 } // namespace OHOS
216 #endif