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 COLOR_SPACE_H
17 #define COLOR_SPACE_H
18 
19 #include <string>
20 
21 #include "include/core/SkColorSpace.h"
22 
23 #include "drawing/engine_adapter/impl_interface/color_space_impl.h"
24 #include "utils/drawing_macros.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 enum class CMSTransferFuncType {
30     SRGB,
31     DOT2,
32     LINEAR,
33     REC2020,
34 };
35 
36 enum class CMSMatrixType {
37     SRGB,
38     ADOBE_RGB,
39     DCIP3,
40     REC2020,
41     XYZ,
42 };
43 
44 // CMSMatrix3x3 is a 3x3 float type matrix.
45 constexpr static int MATRIX3_SIZE = 3;
46 
47 struct CMSMatrix3x3 {
48     float vals[MATRIX3_SIZE][MATRIX3_SIZE];
49 };
50 
51 struct CMSTransferFunction {
52     float g, a, b, c, d, e, f;
53 };
54 
55 class DRAWING_API ColorSpace {
56 public:
57     enum class ColorSpaceType {
58         NO_TYPE,
59         SRGB,
60         SRGB_LINEAR,
61         REF_IMAGE,
62         RGB,
63     };
64 
65     /**
66      * @brief Create the sRGB color space.
67      *
68      * @return A shared ptr to ColorSpace
69      */
70     static std::shared_ptr<ColorSpace> CreateSRGB();
71 
72     /**
73      * @brief Colorspace with the sRGB primaries, but a linear (1.0) gamma.
74      * @return A shared ptr to ColorSpace
75      */
76     static std::shared_ptr<ColorSpace> CreateSRGBLinear();
77     static std::shared_ptr<ColorSpace> CreateRefImage(const Image& image);
78     /**
79      * @brief Create a ColorSpace form a transfer function and a row-major 3x3 transformation to XYZ.
80      * @param func    A transfer function type
81      * @param matrix  A row-major 3x3 transformation type to XYZ
82      * @return        A shared pointer to ColorSpace that its type is RGB.
83      */
84     static std::shared_ptr<ColorSpace> CreateRGB(const CMSTransferFuncType& func, const CMSMatrixType& matrix);
85     static std::shared_ptr<ColorSpace> CreateCustomRGB(const CMSTransferFunction& func, const CMSMatrix3x3& matrix);
86     /**
87      * @brief Create a ColorSpace form a adaptro impl, only used by ImageInfo to ccreate from adaptor image info
88      * @param impl    A adaptor impl of color space
89      * @return        A shared pointer to ColorSpace that its type is RGB.
90      */
91     static std::shared_ptr<ColorSpace> CreateFromImpl(std::shared_ptr<ColorSpaceImpl> impl);
92 
93     ColorSpace() noexcept;
94     virtual ~ColorSpace() = default;
95     ColorSpaceType GetType() const;
GetDrawingType()96     virtual DrawingType GetDrawingType() const
97     {
98         return DrawingType::COMMON;
99     }
100 
101     template<typename T>
GetImpl()102     T* GetImpl() const
103     {
104         return impl_->DowncastingTo<T>();
105     }
106 
107     ColorSpace(std::shared_ptr<ColorSpaceImpl> impl) noexcept;
108     ColorSpace(ColorSpaceType t) noexcept;
109     ColorSpace(ColorSpaceType t, const Image& image) noexcept;
110     ColorSpace(ColorSpaceType t, const CMSTransferFuncType& func, const CMSMatrixType& matrix) noexcept;
111     ColorSpace(ColorSpaceType t, const CMSTransferFunction& func, const CMSMatrix3x3& matrix) noexcept;
112 
113     /**
114      * @brief Caller use method toProfile of SkColorSpace, the parameter is type skcms_ICCProfile.
115      * In order not to encapsulate this method. Drawing ColorSpace needs to be converted to SkColorSpace.
116      * @return     A shared pointer to SkColorSpace.
117      */
118     sk_sp<SkColorSpace> GetSkColorSpace() const;
119     std::shared_ptr<Data> Serialize() const;
120     bool Deserialize(std::shared_ptr<Data> data);
121     bool IsSRGB() const;
122     bool Equals(const std::shared_ptr<ColorSpace>& colorSpace) const;
123 
124 private:
125     ColorSpaceType type_;
126     std::shared_ptr<ColorSpaceImpl> impl_;
127 };
128 } // namespace Drawing
129 } // namespace Rosen
130 } // namespace OHOS
131 #endif
132