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 29 static const Rect noCropRect = { 30 -std::numeric_limits<scalar>::infinity(), -std::numeric_limits<scalar>::infinity(), 31 std::numeric_limits<scalar>::infinity(), std::numeric_limits<scalar>::infinity() 32 }; 33 34 class DRAWING_API ImageFilter { 35 public: 36 enum class FilterType { 37 NO_TYPE, 38 BLUR, 39 COLOR_FILTER, 40 OFFSET, 41 ARITHMETIC, 42 COMPOSE, 43 GRADIENT_BLUR, 44 BLEND, 45 SHADER 46 }; 47 /** 48 * @brief Create a filter that blurs its input by the separate X and Y sinma value. 49 * @param sigmaX The Gaussian sigma value for blurring along the X axis. 50 * @param sigmaY The Gaussian sigma value for blurring along the Y axis. 51 * @param mode The tile mode applied at edges. 52 * @param input The input filter that is blurred, uses source bitmap if this is null. 53 * @param blurType The BlurType of Image, default as GAUSS. 54 * @param cropRect Optional rectangle that crops the input and output 55 * @return A shared pointer to ImageFilter that its type is blur. 56 */ 57 static std::shared_ptr<ImageFilter> CreateBlurImageFilter(scalar sigmaX, scalar sigmaY, TileMode mode, 58 std::shared_ptr<ImageFilter> input, ImageBlurType blurType = ImageBlurType::GAUSS, 59 const Rect& cropRect = noCropRect); 60 /** 61 * @brief Create a filter that applies the color filter to the input filter results. 62 * @param cf The color filter that transforms the input image. 63 * @param input The input filter, or uses the source bitmap if this is null. 64 * @param cropRect Optional rectangle that crops the input and output 65 * @return A shared pointer to ImageFilter that its type is color. 66 */ 67 static std::shared_ptr<ImageFilter> CreateColorFilterImageFilter(const ColorFilter& cf, 68 std::shared_ptr<ImageFilter> input, const Rect& cropRect = noCropRect); 69 70 static std::shared_ptr<ImageFilter> CreateColorBlurImageFilter(const ColorFilter& cf, scalar sigmaX, scalar sigmaY, 71 ImageBlurType blurType = ImageBlurType::GAUSS, const Rect& cropRect = noCropRect); 72 /* 73 * @brief Create a filter that offsets the input filter by the given vector. 74 * @param dx The x offset in local space that the image is shifted. 75 * @param dy The y offset in local space that the image is shifted. 76 * @param input The input that will be moved, if null the source bitmap is used instead. 77 * @param cropRect Optional rectangle that crops the input and output 78 * @return A shared pointer to ImageFilter that its type is offset. 79 */ 80 static std::shared_ptr<ImageFilter> CreateOffsetImageFilter(scalar dx, scalar dy, 81 std::shared_ptr<ImageFilter> input, const Rect& cropRect = noCropRect); 82 /** 83 * @brief Create a filter that implements a custom blend mode. 84 * @param coefficients Get the four coefficients used to combine the foreground and background in the vector. 85 And The vector size must be four, otherwise the call fails. 86 * @param enforcePMColor If true, the RGB channels will be clamped to the Calculated alpha. 87 * @param background The Background content, using the source bitmap when this is null. 88 * @param foreground The foreground content, using the source bitmap when this is null. 89 * @param cropRect Optional rectangle that crops the input and output 90 * @return A shared point to ImageFilter that its type is arithmetic. 91 */ 92 static std::shared_ptr<ImageFilter> CreateArithmeticImageFilter(const std::vector<scalar>& coefficients, 93 bool enforcePMColor, std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground, 94 const Rect& cropRect = noCropRect); 95 /** 96 * @brief Create a filter that composes f1 with f2. 97 * @param f1 The outer filter that evaluates the results of inner. 98 * @param f2 The inner filter that produces the input to outer. 99 * @return A shared pointer to ImageFilter that its type is compose. 100 */ 101 static std::shared_ptr<ImageFilter> CreateComposeImageFilter(std::shared_ptr<ImageFilter> f1, 102 std::shared_ptr<ImageFilter> f2); 103 104 static std::shared_ptr<ImageFilter> CreateGradientBlurImageFilter(float radius, 105 const std::vector<std::pair<float, float>>& fractionStops, GradientDir direction, 106 GradientBlurType blurType, std::shared_ptr<ImageFilter> input); 107 108 /** 109 * @brief This filter takes an BlendMode and uses it to composite the two filters together 110 * @param mode The blend mode that defines the compositing operation 111 * @param background The Dst pixels used in blending, if null the source bitmap is used. 112 * @param foreground The Src pixels used in blending, if null the source bitmap is used. 113 * @param cropRect Optional rectangle that crops the input and output 114 * @return A shared pointer to ImageFilter that its type is blend. 115 */ 116 static std::shared_ptr<ImageFilter> CreateBlendImageFilter(BlendMode mode, 117 std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground = nullptr, 118 const Rect& cropRect = noCropRect); 119 120 /** 121 * @brief Create a filter that fills the output with the per-pixel evaluation of the ShaderEffect. The 122 * shader is defined in the image filter's local coordinate system, so will automatically 123 * be affected by Canvas's transform. 124 * 125 * Like Image() and Picture(), this is a leaf filter that can be used to introduce inputs to 126 * a complex filter graph, but should generally be combined with a filter that as at least 127 * one null input to use the implicit source image. 128 * @param shader The shader that fills the result image 129 * @param cropRect Optional rectangle that crops the input and output 130 * @return A shared pointer to ImageFilter that its type is shader. 131 */ 132 static std::shared_ptr<ImageFilter> CreateShaderImageFilter(std::shared_ptr<ShaderEffect> shader, 133 const Rect& cropRect = noCropRect); 134 135 virtual ~ImageFilter() = default; 136 FilterType GetType() const; GetDrawingType()137 virtual DrawingType GetDrawingType() const 138 { 139 return DrawingType::COMMON; 140 } 141 std::shared_ptr<Data> Serialize() const; 142 bool Deserialize(std::shared_ptr<Data> data); 143 template<typename T> GetImpl()144 T* GetImpl() const 145 { 146 return impl_->DowncastingTo<T>(); 147 } 148 149 ImageFilter(FilterType t, scalar x, scalar y, std::shared_ptr<ImageFilter> input, 150 const Rect& cropRect = noCropRect) noexcept; 151 ImageFilter(FilterType t, scalar x, scalar y, TileMode mode, std::shared_ptr<ImageFilter> input, 152 ImageBlurType blurType, const Rect& cropRect = noCropRect) noexcept; 153 ImageFilter(FilterType t, const ColorFilter& cf, 154 std::shared_ptr<ImageFilter> input, const Rect& cropRect = noCropRect) noexcept; 155 ImageFilter(FilterType t, const ColorFilter& cf, scalar x, scalar y, 156 ImageBlurType blurType, const Rect& cropRect = noCropRect) noexcept; 157 ImageFilter(FilterType t, const std::vector<scalar>& coefficients, bool enforcePMColor, 158 std::shared_ptr<ImageFilter> background, std::shared_ptr<ImageFilter> foreground, 159 const Rect& cropRect = noCropRect) noexcept; 160 ImageFilter(FilterType t, std::shared_ptr<ImageFilter> f1, std::shared_ptr<ImageFilter> f2) noexcept; 161 ImageFilter(FilterType t, float radius, const std::vector<std::pair<float, float>>& fractionStops, 162 GradientDir direction, GradientBlurType blurType, std::shared_ptr<ImageFilter> input) noexcept; 163 ImageFilter(FilterType t) noexcept; 164 void InitWithColorBlur(const ColorFilter& cf, scalar x, scalar y, ImageBlurType blurType, 165 const Rect& cropRect = noCropRect); 166 ImageFilter(FilterType t, BlendMode mode, std::shared_ptr<ImageFilter> background, 167 std::shared_ptr<ImageFilter> foreground = nullptr, 168 const Rect& cropRect = noCropRect) noexcept; 169 ImageFilter(FilterType t, std::shared_ptr<ShaderEffect> shader, const Rect& cropRect = noCropRect) noexcept; 170 protected: 171 ImageFilter() noexcept; 172 173 private: 174 FilterType type_; 175 std::shared_ptr<ImageFilterImpl> impl_; 176 }; 177 } // namespace Drawing 178 } // namespace Rosen 179 } // namespace OHOS 180 #endif