1 /* 2 * Copyright (C) 2021 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 ABS_IMAGE_DECODER_H 17 #define ABS_IMAGE_DECODER_H 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 #include <cmath> 23 #include <unistd.h> 24 #if !defined(_WIN32) && !defined(_APPLE) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 25 #include <sys/mman.h> 26 #include "ashmem.h" 27 #include "auxiliary_picture.h" 28 #endif 29 #ifdef IMAGE_COLORSPACE_FLAG 30 #include "color_space.h" 31 #endif 32 #include "image_plugin_type.h" 33 #include "input_data_stream.h" 34 #include "media_errors.h" 35 #include "pixel_map.h" 36 #include "plugin_service.h" 37 #include "hdr_type.h" 38 39 namespace OHOS { 40 namespace ImagePlugin { 41 const std::string ACTUAL_IMAGE_ENCODED_FORMAT = "actual_encoded_format"; 42 43 struct NinePatchContext { 44 // png nine patch info 45 void *ninePatch = nullptr; 46 // png nine patch info size; 47 size_t patchSize = 0; 48 }; 49 struct DecodeContext { 50 // In: input the image head info. 51 PlImageInfo info; 52 // In: input the pixelmap uniqueId. 53 uint32_t pixelmapUniqueId_; 54 // InOut: input the buffer and bufferSize, output pixels data and dataSize. 55 PlImageBuffer pixelsBuffer; 56 // In: whether the source data is completed. 57 // data incomplete may occur when it is in incremental data source. 58 // when this state is false, data incomplete is not an exception, 59 // so the decoding cannot be failed because data incomplete, 60 // but should decode as much as possible based on the existing data. 61 bool ifSourceCompleted = true; 62 // Out: output the PixelFormat. 63 OHOS::Media::PixelFormat pixelFormat = OHOS::Media::PixelFormat::RGBA_8888; 64 // Out: output the PixelFormat. 65 OHOS::Media::PixelFormat photoDesiredPixelFormat = OHOS::Media::PixelFormat::RGBA_1010102; 66 // Out: output the ColorSpace. 67 OHOS::Media::ColorSpace colorSpace = OHOS::Media::ColorSpace::UNKNOWN; 68 // Out: output if a partial image output. 69 bool ifPartialOutput = false; 70 // Out: output allocator type. 71 Media::AllocatorType allocatorType = Media::AllocatorType::SHARE_MEM_ALLOC; 72 // Out: output allocator release function. 73 Media::CustomFreePixelMap freeFunc = nullptr; 74 // Out: png nine patch context; 75 NinePatchContext ninePatchContext; 76 // Out: output YUV data info 77 OHOS::Media::YUVDataInfo yuvInfo; 78 // Out: output the final pixelMap Info, only size is used now. 79 PlImageInfo outInfo; 80 // Out: Whether to perform hard decoding 0 is no 1 is yes 81 bool isHardDecode = false; 82 // Out: hard decode error message 83 std::string hardDecodeError; 84 // Out: hdr type 85 Media::ImageHdrType hdrType = Media::ImageHdrType::UNKNOWN; 86 Media::ResolutionQuality resolutionQuality = Media::ResolutionQuality::UNKNOWN; 87 bool isAisr = false; 88 // Out: colorSpace 89 ColorManager::ColorSpaceName grColorSpaceName = ColorManager::NONE; 90 // In: User set allocatorType. 91 bool isAppUseAllocator = false; 92 }; 93 94 struct ProgDecodeContext { 95 DecodeContext decodeContext; 96 97 static constexpr uint8_t DEFAULT_STEP = 10; 98 static constexpr uint8_t FULL_PROGRESS = 100; 99 // In: step size requesting advancement, in percentage, 1-100. 100 // if it is an incremental data source and the remaining image data does not 101 // reach the required amount, try to decode to the maximum possible number. 102 uint8_t desiredStep = DEFAULT_STEP; 103 104 // InOut: in percentage, 1-100. 105 // input total process progress after last decoding step, 106 // output total process progress after current decoding step. 107 uint8_t totalProcessProgress = 0; 108 }; 109 110 struct PixelDecodeOptions { 111 OHOS::Media::Rect CropRect; 112 OHOS::Media::Size desiredSize; 113 float rotateDegrees = 0; 114 static constexpr uint32_t DEFAULT_SAMPLE_SIZE = 1; 115 uint32_t sampleSize = DEFAULT_SAMPLE_SIZE; 116 OHOS::Media::PixelFormat desiredPixelFormat = OHOS::Media::PixelFormat::RGBA_8888; 117 OHOS::Media::ColorSpace desiredColorSpace = OHOS::Media::ColorSpace::UNKNOWN; 118 OHOS::Media::AlphaType desireAlphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL; 119 bool allowPartialImage = true; 120 bool editable = false; 121 OHOS::Media::FillColor plFillColor; 122 OHOS::Media::FillColor plStrokeColor; 123 OHOS::Media::SVGResize plSVGResize; 124 std::shared_ptr<OHOS::ColorManager::ColorSpace> plDesiredColorSpace = nullptr; 125 std::shared_ptr<Media::PixelMap> plReusePixelmap = nullptr; 126 OHOS::Media::CropAndScaleStrategy cropAndScaleStrategy = OHOS::Media::CropAndScaleStrategy::DEFAULT; 127 }; 128 129 class AbsImageDecoder { 130 public: 131 static constexpr uint32_t DEFAULT_IMAGE_NUM = 1; 132 static constexpr uint32_t E_NO_EXIF = 1; 133 static constexpr uint32_t DEFAULT_GAINMAP_OFFSET = 0; 134 135 AbsImageDecoder() = default; 136 137 virtual ~AbsImageDecoder() = default; 138 139 // set image file source, start a new picture decoding process. 140 // the InputDataStream points to the beginning of the image file. 141 virtual void SetSource(InputDataStream &sourceStream) = 0; 142 143 // reset the decoder, clear all the decoder's status data cache. 144 virtual void Reset() = 0; 145 146 // judge a image source has a property or not. HasProperty(std::string key)147 virtual bool HasProperty(std::string key) 148 { 149 return false; 150 } 151 152 // set decode options before decode and get target decoded image info. 153 virtual uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) = 0; 154 155 // One-time decoding. 156 virtual uint32_t Decode(uint32_t index, DecodeContext &context) = 0; 157 158 // incremental decoding. 159 virtual uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) = 0; 160 161 // get the number of top level images in the image file. GetTopLevelImageNum(uint32_t & num)162 virtual uint32_t GetTopLevelImageNum(uint32_t &num) 163 { 164 num = DEFAULT_IMAGE_NUM; 165 return Media::SUCCESS; 166 } 167 168 // get image size without decoding image data. 169 virtual uint32_t GetImageSize(uint32_t index, OHOS::Media::Size &size) = 0; 170 171 // get image property. GetImagePropertyInt(uint32_t index,const std::string & key,int32_t & value)172 virtual uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) 173 { 174 return Media::ERR_MEDIA_INVALID_OPERATION; 175 } 176 177 // get image property. GetImagePropertyString(uint32_t index,const std::string & key,std::string & value)178 virtual uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) 179 { 180 return Media::ERR_MEDIA_INVALID_OPERATION; 181 } 182 183 // modify image property. ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const std::string & path)184 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 185 const std::string &value, const std::string &path) 186 { 187 return Media::ERR_MEDIA_INVALID_OPERATION; 188 } 189 ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const int fd)190 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 191 const std::string &value, const int fd) 192 { 193 return Media::ERR_MEDIA_INVALID_OPERATION; 194 } 195 ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,uint8_t * data,uint32_t size)196 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 197 const std::string &value, uint8_t *data, uint32_t size) 198 { 199 return Media::ERR_MEDIA_INVALID_OPERATION; 200 } 201 202 // get filter area. GetFilterArea(const int & privacyType,std::vector<std::pair<uint32_t,uint32_t>> & ranges)203 virtual uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges) 204 { 205 return E_NO_EXIF; 206 } 207 208 #ifdef IMAGE_COLORSPACE_FLAG 209 // get current source is support icc profile or not. IsSupportICCProfile()210 virtual bool IsSupportICCProfile() 211 { 212 return false; 213 } 214 215 // if current source support icc. get relevant color gamut information by this method. GetPixelMapColorSpace()216 virtual OHOS::ColorManager::ColorSpace GetPixelMapColorSpace() 217 { 218 return OHOS::ColorManager::ColorSpace(OHOS::ColorManager::ColorSpaceName::NONE); 219 } 220 #endif 221 CheckHdrType()222 virtual Media::ImageHdrType CheckHdrType() 223 { 224 return Media::ImageHdrType::SDR; 225 } 226 GetGainMapOffset()227 virtual uint32_t GetGainMapOffset() 228 { 229 return DEFAULT_GAINMAP_OFFSET; 230 } 231 GetHdrMetadata(Media::ImageHdrType type)232 virtual Media::HdrMetadata GetHdrMetadata(Media::ImageHdrType type) 233 { 234 return {}; 235 } 236 DecodeHeifGainMap(DecodeContext & context)237 virtual bool DecodeHeifGainMap(DecodeContext& context) 238 { 239 return false; 240 } 241 GetHeifHdrColorSpace(ColorManager::ColorSpaceName & gainmap,ColorManager::ColorSpaceName & hdr)242 virtual bool GetHeifHdrColorSpace(ColorManager::ColorSpaceName &gainmap, ColorManager::ColorSpaceName &hdr) 243 { 244 return false; 245 } 246 GetHeifParseErr()247 virtual uint32_t GetHeifParseErr() 248 { 249 return 0; 250 } 251 DecodeHeifAuxiliaryMap(DecodeContext & context,Media::AuxiliaryPictureType type)252 virtual bool DecodeHeifAuxiliaryMap(DecodeContext& context, Media::AuxiliaryPictureType type) 253 { 254 return false; 255 } 256 CheckAuxiliaryMap(Media::AuxiliaryPictureType type)257 virtual bool CheckAuxiliaryMap(Media::AuxiliaryPictureType type) 258 { 259 return false; 260 } 261 GetHeifFragmentMetadata(Media::Rect & metadata)262 virtual bool GetHeifFragmentMetadata(Media::Rect& metadata) 263 { 264 return false; 265 } 266 267 // define multiple subservices for this interface 268 static constexpr uint16_t SERVICE_DEFAULT = 0; 269 }; 270 } // namespace ImagePlugin 271 } // namespace OHOS 272 273 DECLARE_INTERFACE(OHOS::ImagePlugin::AbsImageDecoder, IMAGE_DECODER_IID) 274 275 #endif // ABS_IMAGE_DECODER_H 276