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