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