1 /*
2  * Copyright (c) 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_INFO_H
17 #define IMAGE_INFO_H
18 #include "draw/color.h"
19 #include "effect/color_space.h"
20 #include "utils/rect.h"
21 namespace OHOS {
22 namespace Rosen {
23 namespace Drawing {
24 
25 enum class EncodedImageFormat {
26     JPEG,
27     PNG,
28     WEBP,
29     UNKNOWN,
30 };
31 
32 class ImageInfo {
33 public:
34     ImageInfo() = default;
35     ImageInfo(int width, int height, ColorType colorType, AlphaType alphaType,
36 	          std::shared_ptr<ColorSpace> colorSpace = nullptr)
37         : width_(width), height_(height), colorType_(colorType), alphaType_(alphaType), colorSpace_(colorSpace) {}
38     ~ImageInfo() = default;
39 
MakeN32Premul(int32_t width,int32_t height)40     static ImageInfo MakeN32Premul(int32_t width, int32_t height)
41     {
42         return ImageInfo(width, height, COLORTYPE_N32, ALPHATYPE_PREMUL, nullptr);
43     }
44     /**
45      * @brief Gets the width value of ImageInfo.
46      */
GetWidth()47     int GetWidth() const
48     {
49         return width_;
50     }
51 
52     /**
53      * @brief Gets the height value of ImageInfo.
54      */
GetHeight()55     int GetHeight() const
56     {
57         return height_;
58     }
59 
60     /**
61      * @brief Gets the color type value of ImageInfo.
62      */
GetColorType()63     ColorType GetColorType() const
64     {
65         return colorType_;
66     }
67 
68     /**
69      * @brief Gets the alpha type value of ImageInfo.
70      */
GetAlphaType()71     AlphaType GetAlphaType() const
72     {
73         return alphaType_;
74     }
75 
76     /**
77      * @brief Gets the color space value of ImageInfo.
78      */
GetColorSpace()79     std::shared_ptr<ColorSpace> GetColorSpace() const
80     {
81         return colorSpace_;
82     }
83 
84     /**
85      * @brief Returns number of bytes per pixel.
86      */
GetBytesPerPixel()87     int32_t GetBytesPerPixel() const
88     {
89         // returns the number of bytes per pixel: 1byte, 2bytes, 4bytes
90         switch (colorType_) {
91             case COLORTYPE_ALPHA_8:
92                 return 1;
93             case COLORTYPE_RGB_565:
94             case COLORTYPE_ARGB_4444:
95                 return 2;
96             case COLORTYPE_RGBA_8888:
97             case COLORTYPE_BGRA_8888:
98             case COLORTYPE_RGB_888X:
99             case COLORTYPE_N32:
100                 return 4;
101             default:
102                 return 0;
103         }
104     }
105 
106     /**
107      * @brief Sets the width value of ImageInfo.
108      */
SetWidth(int width)109     void SetWidth(int width)
110     {
111         width_ = width;
112     }
113 
114     /**
115      * @brief Sets the height value of ImageInfo.
116      */
SetHeight(int height)117     void SetHeight(int height)
118     {
119         height_ = height;
120     }
121 
122     /**
123      * @brief Sets the color type value of ImageInfo.
124      */
SetColorType(ColorType colorType)125     void SetColorType(ColorType colorType)
126     {
127         colorType_ = colorType;
128     }
129 
130     /**
131      * @brief Sets the alpha type value of ImageInfo.
132      */
SetAlphaType(AlphaType alphaType)133     void SetAlphaType(AlphaType alphaType)
134     {
135         alphaType_ = alphaType;
136     }
137 
138     /**
139      * @brief Sets the color space value of ImageInfo.
140      */
SetColorSpace(std::shared_ptr<ColorSpace> colorSpace)141     void SetColorSpace(std::shared_ptr<ColorSpace> colorSpace)
142     {
143         colorSpace_ = colorSpace;
144     }
145 
146     /**
147      * @brief Gets the bounds of ImageInfo.
148      */
GetBound()149     RectI GetBound() const
150     {
151         return RectI(0, 0, width_, height_);
152     }
153 
154 private:
155     int width_ = 0;
156     int height_ = 0;
157     ColorType colorType_ = COLORTYPE_UNKNOWN;
158     AlphaType alphaType_ = ALPHATYPE_UNKNOWN;
159     std::shared_ptr<ColorSpace> colorSpace_ = nullptr;
160 };
161 } // namespace Drawing
162 } // namespace Rosen
163 } // namespace OHOS
164 #endif // IMAGE_INFO_H
165