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 API_CORE_IMAGE_IIMAGE_LOADER_MANAGER_H 17 #define API_CORE_IMAGE_IIMAGE_LOADER_MANAGER_H 18 19 #include <base/containers/array_view.h> 20 #include <base/containers/string_view.h> 21 #include <base/containers/unique_ptr.h> 22 #include <core/image/intf_animated_image.h> 23 #include <core/image/intf_image_container.h> 24 #include <core/namespace.h> 25 26 CORE_BEGIN_NAMESPACE() 27 class IFile; 28 const int MAX_ERR_MSG_LEN = 128; 29 30 // NOTE: Nicer way to return error strings. 31 /** @ingroup group_image_iimagemanager */ 32 /** IImage manager */ 33 class IImageLoaderManager { 34 public: 35 /** Describes result of the loading operation. */ 36 struct LoadResult { 37 /** Load result was successful? */ 38 bool success { false }; 39 /** Load result error message if load was not successful. */ 40 char error[MAX_ERR_MSG_LEN]; 41 42 /** Pointer to image container */ 43 IImageContainer::Ptr image; 44 }; 45 46 /** Describes result of the animation loading operation. */ 47 struct LoadAnimatedResult { 48 /** Load result was successful? */ 49 bool success { false }; 50 /** Load result error message if load was not successful. */ 51 char error[MAX_ERR_MSG_LEN]; 52 53 /** Pointer to image container */ 54 IAnimatedImage::Ptr image; 55 }; 56 57 /** Image loader flags */ 58 enum ImageLoaderFlags : uint32_t { 59 /** Generate mipmaps if not already included in the image file. */ 60 IMAGE_LOADER_GENERATE_MIPS = 0x00000001, 61 /** Force linear RGB. */ 62 IMAGE_LOADER_FORCE_LINEAR_RGB_BIT = 0x00000002, 63 /** Force SRGB. */ 64 IMAGE_LOADER_FORCE_SRGB_BIT = 0x00000004, 65 /** Force grayscale. */ 66 IMAGE_LOADER_FORCE_GRAYSCALE_BIT = 0x00000008, 67 /** Flip image vertically when loaded. */ 68 IMAGE_LOADER_FLIP_VERTICALLY_BIT = 0x00000010, 69 /** Premultiply color values with the alpha value if an alpha channel is present. */ 70 IMAGE_LOADER_PREMULTIPLY_ALPHA = 0x00000020, 71 /** Only load image metadata (size and format) */ 72 IMAGE_LOADER_METADATA_ONLY = 0x00000040, 73 }; 74 75 /** Interface for defining loaders for different image formats. */ 76 class IImageLoader { 77 public: 78 /** Detect whether current loader can load given image 79 * @param imageFileBytes Image data. 80 */ 81 virtual bool CanLoad(BASE_NS::array_view<const uint8_t> imageFileBytes) const = 0; 82 83 /** Load Image file from provided file with passed flags 84 * @param file File where to load from 85 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 86 */ 87 virtual LoadResult Load(IFile& file, uint32_t loadFlags) const = 0; 88 89 /** Load image file from given data bytes 90 * @param imageFileBytes Image data. 91 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 92 */ 93 virtual LoadResult Load(BASE_NS::array_view<const uint8_t> imageFileBytes, uint32_t loadFlags) const = 0; 94 95 /** Load animated image with given parameters 96 * @param file File to load image from 97 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 98 */ 99 virtual LoadAnimatedResult LoadAnimatedImage(IFile& file, uint32_t loadFlags) = 0; 100 101 /** Load animated image with given parameters 102 * @param imageFileBytes Image data 103 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 104 */ 105 virtual LoadAnimatedResult LoadAnimatedImage( 106 BASE_NS::array_view<const uint8_t> imageFileBytes, uint32_t loadFlags) = 0; 107 108 struct Deleter { 109 constexpr Deleter() noexcept = default; operatorDeleter110 void operator()(IImageLoader* ptr) const 111 { 112 ptr->Destroy(); 113 } 114 }; 115 using Ptr = BASE_NS::unique_ptr<IImageLoader, Deleter>; 116 117 protected: 118 IImageLoader() = default; 119 virtual ~IImageLoader() = default; 120 virtual void Destroy() = 0; 121 }; 122 123 // Prevent copy construction and assign. 124 IImageLoaderManager(IImageLoaderManager const&) = delete; 125 IImageLoaderManager& operator=(IImageLoaderManager const&) = delete; 126 127 /** Register image loader for image manager 128 * @param imageLoader Image loader to be registered 129 */ 130 virtual void RegisterImageLoader(IImageLoader::Ptr imageLoader) = 0; 131 132 /** Load image with given parameters 133 * @param uri Uri to image 134 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 135 */ 136 virtual LoadResult LoadImage(const BASE_NS::string_view uri, uint32_t loadFlags) = 0; 137 138 /** Load image with given parameters 139 * @param file File to load image from 140 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 141 */ 142 virtual LoadResult LoadImage(IFile& file, uint32_t loadFlags) = 0; 143 144 /** Load image with given parameters 145 * @param imageFileBytes Image data 146 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 147 */ 148 virtual LoadResult LoadImage(BASE_NS::array_view<const uint8_t> imageFileBytes, uint32_t loadFlags) = 0; 149 150 /** Load animated image with given parameters 151 * @param uri Uri to image 152 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 153 */ 154 virtual LoadAnimatedResult LoadAnimatedImage(const BASE_NS::string_view uri, uint32_t loadFlags) = 0; 155 156 /** Load animated image with given parameters 157 * @param file File to load image from 158 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 159 */ 160 virtual LoadAnimatedResult LoadAnimatedImage(IFile& file, uint32_t loadFlags) = 0; 161 162 /** Load animated image with given parameters 163 * @param imageFileBytes Image data 164 * @param loadFlags Load flags. Combination of #ImageLoaderFlags 165 */ 166 virtual LoadAnimatedResult LoadAnimatedImage( 167 BASE_NS::array_view<const uint8_t> imageFileBytes, uint32_t loadFlags) = 0; 168 169 protected: 170 IImageLoaderManager() = default; 171 virtual ~IImageLoaderManager() = default; 172 }; 173 CORE_END_NAMESPACE() 174 175 #endif // API_CORE_IMAGE_IIMAGE_LOADER_MANAGER_H 176