• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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/scalar.h"
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class ImageFilterImpl;
28 class DRAWING_API ImageFilter {
29 public:
30     enum class FilterType {
31         NO_TYPE,
32         BLUR,
33         COLOR_FILTER,
34         OFFSET,
35         ARITHMETIC,
36         COMPOSE,
37     };
38     /*
39      * @brief         Create a filter that blurs its input by the separate X and Y sinma value.
40      * @param sigmaX  The Gaussian sigma value for blurring along the X axis.
41      * @param sigmaY  The Gaussian sigma value for blurring along the Y axis.
42      * @param mode    The tile mode applied at edges.
43      * @param input   The input filter that is blurred, uses source bitmap if this is null.
44      * @return        A shared pointer to ImageFilter that its type is blur.
45      */
46     static std::shared_ptr<ImageFilter> CreateBlurImageFilter(scalar sigmaX, scalar sigmaY, TileMode mode,
47         std::shared_ptr<ImageFilter> input);
48     /*
49      * @brief        Create a filter that applies the color filter to the input filter results.
50      * @param cf     The color filter that transforms the input image.
51      * @param input  The input filter, or uses the source bitmap if this is null.
52      * @return       A shared pointer to ImageFilter that its type is color.
53      */
54     static std::shared_ptr<ImageFilter> CreateColorFilterImageFilter(const ColorFilter& cf,
55         std::shared_ptr<ImageFilter> input);
56 
57     static std::shared_ptr<ImageFilter> CreateColorBlurImageFilter(const ColorFilter& cf,
58         scalar sigmaX, scalar sigmaY);
59     /*
60      * @brief        Create a filter that offsets the input filter by the given vector.
61      * @param dx     The x offset in local space that the image is shifted.
62      * @param dy     The y offset in local space that the image is shifted.
63      * @param input  The input that will be moved, if null the source bitmap is used instead.
64      * @return       A shared pointer to ImageFilter that its type is offset.
65      */
66     static std::shared_ptr<ImageFilter> CreateOffsetImageFilter(scalar dx, scalar dy,
67         std::shared_ptr<ImageFilter> input);
68     /*
69      * @brief                 Create a filter that implements a custom blend mode.
70      * @param coefficients    Get the four coefficients used to combine the foreground and background in the vector.
71                               And The vector size must be four, otherwise the call fails.
72      * @param enforcePMColor  If true, the RGB channels will be clamped to the Calculated alpha.
73      * @param background      The Background content, using the source bitmap when this is null.
74      * @param foreground      The foreground content, using the source bitmap when this is null.
75      * @return                A shared point to ImageFilter that its type is arithmetic.
76      */
77     static std::shared_ptr<ImageFilter> CreateArithmeticImageFilter(const std::vector<scalar>& coefficients,
78         bool enforcePMColor, std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground);
79     /*
80      * @brief     Create a filter that composes f1 with f2.
81      * @param f1  The outer filter that evaluates the results of inner.
82      * @param f2  The inner filter that produces the input to outer.
83      * @return    A shared pointer to ImageFilter that its type is compose.
84      */
85     static std::shared_ptr<ImageFilter> CreateComposeImageFilter(std::shared_ptr<ImageFilter> f1,
86         std::shared_ptr<ImageFilter> f2);
87 
88     virtual ~ImageFilter() = default;
89     FilterType GetType() const;
GetDrawingType()90     virtual DrawingType GetDrawingType() const
91     {
92         return DrawingType::COMMON;
93     }
94 
95     template<typename T>
GetImpl()96     T* GetImpl() const
97     {
98         return impl_->DowncastingTo<T>();
99     }
100 
101     ImageFilter(FilterType t, scalar x, scalar y, std::shared_ptr<ImageFilter> input) noexcept;
102     ImageFilter(FilterType t, scalar x, scalar y, TileMode mode, std::shared_ptr<ImageFilter> input) noexcept;
103     ImageFilter(FilterType t, const ColorFilter& cf, std::shared_ptr<ImageFilter> input) noexcept;
104     ImageFilter(FilterType t, const ColorFilter& cf, scalar x, scalar y) noexcept;
105     ImageFilter(FilterType t, const std::vector<scalar>& coefficients, bool enforcePMColor,
106         std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground) noexcept;
107     ImageFilter(FilterType t, std::shared_ptr<ImageFilter> f1, std::shared_ptr<ImageFilter> f2) noexcept;
108     ImageFilter(FilterType t) noexcept;
109 
110     void InitWithColorBlur(const ColorFilter& cf, scalar x, scalar y);
111 
112     std::shared_ptr<Data> Serialize() const;
113     bool Deserialize(std::shared_ptr<Data> data);
114 protected:
115     ImageFilter() noexcept;
116 
117 private:
118     FilterType type_;
119     std::shared_ptr<ImageFilterImpl> impl_;
120 };
121 } // namespace Drawing
122 } // namespace Rosen
123 } // namespace OHOS
124 #endif