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 bool useNoPadding = false; 73 // Out: output allocator release function. 74 Media::CustomFreePixelMap freeFunc = nullptr; 75 // Out: png nine patch context; 76 NinePatchContext ninePatchContext; 77 // Out: output YUV data info 78 OHOS::Media::YUVDataInfo yuvInfo; 79 // Out: output the final pixelMap Info, only size is used now. 80 PlImageInfo outInfo; 81 // Out: Whether to perform hard decoding 0 is no 1 is yes 82 bool isHardDecode = false; 83 // Out: hard decode error message 84 std::string hardDecodeError; 85 // Out: hdr type 86 Media::ImageHdrType hdrType = Media::ImageHdrType::UNKNOWN; 87 Media::ResolutionQuality resolutionQuality = Media::ResolutionQuality::UNKNOWN; 88 bool isAisr = false; 89 // Out: colorSpace 90 ColorManager::ColorSpaceName grColorSpaceName = ColorManager::NONE; 91 // In: User set allocatorType. 92 bool isAppUseAllocator = false; 93 // In : User set CreateWideGamutSdrPixelMap 94 bool isCreateWideGamutSdrPixelMap = false; 95 }; 96 97 struct ProgDecodeContext { 98 DecodeContext decodeContext; 99 100 static constexpr uint8_t DEFAULT_STEP = 10; 101 static constexpr uint8_t FULL_PROGRESS = 100; 102 // In: step size requesting advancement, in percentage, 1-100. 103 // if it is an incremental data source and the remaining image data does not 104 // reach the required amount, try to decode to the maximum possible number. 105 uint8_t desiredStep = DEFAULT_STEP; 106 107 // InOut: in percentage, 1-100. 108 // input total process progress after last decoding step, 109 // output total process progress after current decoding step. 110 uint8_t totalProcessProgress = 0; 111 }; 112 113 struct PixelDecodeOptions { 114 OHOS::Media::Rect CropRect; 115 OHOS::Media::Size desiredSize; 116 float rotateDegrees = 0; 117 static constexpr uint32_t DEFAULT_SAMPLE_SIZE = 1; 118 uint32_t sampleSize = DEFAULT_SAMPLE_SIZE; 119 OHOS::Media::PixelFormat desiredPixelFormat = OHOS::Media::PixelFormat::RGBA_8888; 120 OHOS::Media::ColorSpace desiredColorSpace = OHOS::Media::ColorSpace::UNKNOWN; 121 OHOS::Media::AlphaType desireAlphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL; 122 bool allowPartialImage = true; 123 bool editable = false; 124 OHOS::Media::FillColor plFillColor; 125 OHOS::Media::FillColor plStrokeColor; 126 OHOS::Media::SVGResize plSVGResize; 127 std::shared_ptr<OHOS::ColorManager::ColorSpace> plDesiredColorSpace = nullptr; 128 std::shared_ptr<Media::PixelMap> plReusePixelmap = nullptr; 129 OHOS::Media::CropAndScaleStrategy cropAndScaleStrategy = OHOS::Media::CropAndScaleStrategy::DEFAULT; 130 }; 131 132 class AbsImageDecoder { 133 public: 134 static constexpr uint32_t DEFAULT_IMAGE_NUM = 1; 135 static constexpr uint32_t E_NO_EXIF = 1; 136 static constexpr uint32_t DEFAULT_GAINMAP_OFFSET = 0; 137 138 AbsImageDecoder() = default; 139 140 virtual ~AbsImageDecoder() = default; 141 142 // set image file source, start a new picture decoding process. 143 // the InputDataStream points to the beginning of the image file. 144 virtual void SetSource(InputDataStream &sourceStream) = 0; 145 146 // reset the decoder, clear all the decoder's status data cache. 147 virtual void Reset() = 0; 148 149 // judge a image source has a property or not. HasProperty(std::string key)150 virtual bool HasProperty(std::string key) 151 { 152 return false; 153 } 154 155 // set decode options before decode and get target decoded image info. 156 virtual uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) = 0; 157 158 // use to init jpeg hardware decoder and notify it to power on InitJpegDecoder()159 virtual void InitJpegDecoder() 160 { 161 return; 162 } 163 164 // One-time decoding. 165 virtual uint32_t Decode(uint32_t index, DecodeContext &context) = 0; 166 167 // incremental decoding. 168 virtual uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) = 0; 169 170 // get the number of top level images in the image file. GetTopLevelImageNum(uint32_t & num)171 virtual uint32_t GetTopLevelImageNum(uint32_t &num) 172 { 173 num = DEFAULT_IMAGE_NUM; 174 return Media::SUCCESS; 175 } 176 177 // get image size without decoding image data. 178 virtual uint32_t GetImageSize(uint32_t index, OHOS::Media::Size &size) = 0; 179 180 // get image property. GetImagePropertyInt(uint32_t index,const std::string & key,int32_t & value)181 virtual uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) 182 { 183 return Media::ERR_MEDIA_INVALID_OPERATION; 184 } 185 186 // get image property. GetImagePropertyString(uint32_t index,const std::string & key,std::string & value)187 virtual uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) 188 { 189 return Media::ERR_MEDIA_INVALID_OPERATION; 190 } 191 192 // modify image property. ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const std::string & path)193 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 194 const std::string &value, const std::string &path) 195 { 196 return Media::ERR_MEDIA_INVALID_OPERATION; 197 } 198 ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const int fd)199 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 200 const std::string &value, const int fd) 201 { 202 return Media::ERR_MEDIA_INVALID_OPERATION; 203 } 204 ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,uint8_t * data,uint32_t size)205 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 206 const std::string &value, uint8_t *data, uint32_t size) 207 { 208 return Media::ERR_MEDIA_INVALID_OPERATION; 209 } 210 211 // get filter area. GetFilterArea(const int & privacyType,std::vector<std::pair<uint32_t,uint32_t>> & ranges)212 virtual uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges) 213 { 214 return E_NO_EXIF; 215 } 216 217 #ifdef IMAGE_COLORSPACE_FLAG 218 // get current source is support icc profile or not. IsSupportICCProfile()219 virtual bool IsSupportICCProfile() 220 { 221 return false; 222 } 223 224 // if current source support icc. get relevant color gamut information by this method. GetPixelMapColorSpace()225 virtual OHOS::ColorManager::ColorSpace GetPixelMapColorSpace() 226 { 227 return OHOS::ColorManager::ColorSpace(OHOS::ColorManager::ColorSpaceName::NONE); 228 } 229 #endif 230 CheckHdrType()231 virtual Media::ImageHdrType CheckHdrType() 232 { 233 return Media::ImageHdrType::SDR; 234 } 235 ValidateAndCorrectMetaData(Media::HdrMetadata & metadata)236 virtual void ValidateAndCorrectMetaData(Media::HdrMetadata& metadata) 237 { 238 return; 239 } 240 GetGainMapOffset()241 virtual uint32_t GetGainMapOffset() 242 { 243 return DEFAULT_GAINMAP_OFFSET; 244 } 245 GetHdrMetadata(Media::ImageHdrType type)246 virtual Media::HdrMetadata GetHdrMetadata(Media::ImageHdrType type) 247 { 248 return {}; 249 } 250 DecodeHeifGainMap(DecodeContext & context)251 virtual bool DecodeHeifGainMap(DecodeContext& context) 252 { 253 return false; 254 } 255 GetHeifRegionGridSize()256 virtual OHOS::Media::Size GetHeifRegionGridSize() 257 { 258 return {0, 0}; 259 } 260 GetHeifHdrColorSpace(ColorManager::ColorSpaceName & gainmap,ColorManager::ColorSpaceName & hdr)261 virtual bool GetHeifHdrColorSpace(ColorManager::ColorSpaceName &gainmap, ColorManager::ColorSpaceName &hdr) 262 { 263 return false; 264 } 265 GetHeifParseErr()266 virtual uint32_t GetHeifParseErr() 267 { 268 return 0; 269 } 270 DecodeHeifAuxiliaryMap(DecodeContext & context,Media::AuxiliaryPictureType type)271 virtual bool DecodeHeifAuxiliaryMap(DecodeContext& context, Media::AuxiliaryPictureType type) 272 { 273 return false; 274 } 275 CheckAuxiliaryMap(Media::AuxiliaryPictureType type)276 virtual bool CheckAuxiliaryMap(Media::AuxiliaryPictureType type) 277 { 278 return false; 279 } 280 GetHeifFragmentMetadata(Media::Rect & metadata)281 virtual bool GetHeifFragmentMetadata(Media::Rect& metadata) 282 { 283 return false; 284 } 285 286 virtual std::string GetPluginType() = 0; 287 GetHeifGridTileSize()288 virtual OHOS::Media::Size GetHeifGridTileSize() 289 { 290 return {0, 0}; 291 } 292 293 // define multiple subservices for this interface 294 static constexpr uint16_t SERVICE_DEFAULT = 0; 295 }; 296 } // namespace ImagePlugin 297 } // namespace OHOS 298 299 DECLARE_INTERFACE(OHOS::ImagePlugin::AbsImageDecoder, IMAGE_DECODER_IID) 300 301 #endif // ABS_IMAGE_DECODER_H 302