1 /*
2  * Copyright (c) 2020-2022 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 /**
17  * @addtogroup UI_Common
18  * @{
19  *
20  * @brief Defines common UI capabilities, such as image and text processing.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file image.h
28  *
29  * @brief Declares basic image attributes, including the image type and path.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_IMAGE_H
36 #define GRAPHIC_LITE_IMAGE_H
37 
38 #include "gfx_utils/geometry2d.h"
39 #include "gfx_utils/graphic_buffer.h"
40 #include "gfx_utils/heap_base.h"
41 #include "gfx_utils/image_info.h"
42 #include "gfx_utils/style.h"
43 
44 namespace OHOS {
45 /**
46  * @brief Represents basic image attributes, including the image type and path.
47  *
48  * @since 1.0
49  * @version 1.0
50  */
51 class Image : public HeapBase {
52 public:
53     /**
54      * @brief A constructor used to create an <b>Image</b> instance. You can use this constructor when a component
55      * requires a map.
56      *
57      * @since 1.0
58      * @version 1.0
59      */
60     Image();
61 
62     /**
63      * @brief A destructor used to delete the <b>Image</b> instance.
64      *
65      * @since 1.0
66      * @version 1.0
67      */
68     virtual ~Image();
69 
70     /**
71      * @brief Obtains the image information in an array.
72      *
73      * @return Returns the pointer to the image information.
74      * @since 1.0
75      * @version 1.0
76      */
GetImageInfo()77     const ImageInfo* GetImageInfo() const
78     {
79         return imageInfo_;
80     }
81 
82     /**
83      * @brief Obtains the image path in binary.
84      *
85      * @return Returns the pointer to the image path.
86      * @since 1.0
87      * @version 1.0
88      */
GetPath()89     const char* GetPath() const
90     {
91         return path_;
92     }
93 
94     /**
95      * @brief Obtains the basic image information, including the image format, width, and height.
96      *
97      * @param header Indicates the basic image information.
98      * @since 1.0
99      * @version 1.0
100      */
101     void GetHeader(ImageHeader& header) const;
102 
103     /**
104      * @brief Obtains the image type.
105      *
106      * @return Returns <b>IMG_SRC_VARIABLE</b> for image information in an array; returns <b>IMG_SRC_FILE</b> for an
107      * image path in binary.
108      * @since 1.0
109      * @version 1.0
110      */
GetSrcType()111     uint8_t GetSrcType() const
112     {
113         return srcType_;
114     }
115 
116     /**
117      * @brief Sets the image path.
118      *
119      * @param src Indicates the pointer to image path in the format of <b>..\\xxx\\xxx\\xxx.bin</b>.
120      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
121      * @since 1.0
122      * @version 1.0
123      */
124     bool SetSrc(const char* src);
125 
126     /**
127      * @brief Sets the image information.
128      *
129      * @param src Indicates the pointer to the image information.
130      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
131      * @since 1.0
132      * @version 1.0
133      */
134     bool SetSrc(const ImageInfo* src);
135 
136     /**
137      * @brief Parse file path
138      *
139      * @param src Indicates the pointer to the image information.
140      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
141      * @since 1.0
142      * @version 1.0
143      */
144     bool PreParse(const char* src);
145 
146     void DrawImage(BufferInfo& gfxDstBuffer,
147                    const Rect& coords,
148                    const Rect& mask,
149                    const Style& style,
150                    uint8_t opaScale) const;
151 
152 protected:
153     const ImageInfo* imageInfo_;
154     char* path_;
155 
156 private:
157 #if ENABLE_JPEG || ENABLE_PNG
158     enum ImageType {
159         IMG_PNG,
160         IMG_JPEG,
161         IMG_GIF,
162         IMG_UNKNOWN,
163     };
164     const static uint8_t IMG_BYTES_TO_CHECK = 4; // 4: check 4 bytes of image file
165 #endif
166     uint8_t srcType_;
167     bool mallocFlag_;
168     bool SetLiteSrc(const char* src);
169     bool SetStandardSrc(const char* src);
170 #if ENABLE_JPEG
171     bool SetJPEGSrc(const char* src);
172 #endif
173 #if ENABLE_PNG
174     bool SetPNGSrc(const char* src);
175 #endif
176 #if ENABLE_JPEG || ENABLE_PNG
177     ImageType CheckImgType(const char* src);
178 #endif
IsImgValid(const char * suffix)179     bool IsImgValid(const char* suffix)
180     {
181         return (!strcmp(suffix, ".png") || !strcmp(suffix, ".PNG") || !strcmp(suffix, ".jpg") ||
182             !strcmp(suffix, ".JPG") || !strcmp(suffix, ".jpeg") || !strcmp(suffix, ".JPEG") ||
183             !strcmp(suffix, ".BMP") || !strcmp(suffix, ".bmp") || !strcmp(suffix, ".GIF") || !strcmp(suffix, ".gif"));
184     }
185     void ReInitImageInfo(ImageInfo* imgInfo, bool mallocFlag);
186 };
187 } // namespace OHOS
188 #endif // GRAPHIC_LITE_IMAGE_H
189