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 <math.h> 23 #include <unistd.h> 24 #if !defined(_WIN32) && !defined(_APPLE) 25 #include <sys/mman.h> 26 #include "ashmem.h" 27 #endif 28 #include "image_plugin_type.h" 29 #include "input_data_stream.h" 30 #include "media_errors.h" 31 #include "pixel_map.h" 32 #include "plugin_service.h" 33 34 namespace OHOS { 35 namespace ImagePlugin { 36 const std::string ACTUAL_IMAGE_ENCODED_FORMAT = "actual_encoded_format"; 37 38 struct NinePatchContext { 39 // png nine patch info 40 void *ninePatch = nullptr; 41 // png nine patch info size; 42 size_t patchSize = 0; 43 }; 44 struct DecodeContext { 45 // In: input the image head info. 46 PlImageInfo info; 47 // InOut: input the buffer and bufferSize, output pixels data and dataSize. 48 PlImageBuffer pixelsBuffer; 49 // In: whether the source data is completed. 50 // data incomplete may occur when it is in incremental data source. 51 // when this state is false, data incomplete is not an exception, 52 // so the decoding cannot be failed because data incomplete, 53 // but should decode as much as possible based on the existing data. 54 bool ifSourceCompleted = true; 55 // Out: output the PixelFormat. 56 PlPixelFormat pixelFormat = PlPixelFormat::RGBA_8888; 57 // Out: output the ColorSpace. 58 PlColorSpace colorSpace = PlColorSpace::UNKNOWN; 59 // Out: output if a partial image output. 60 bool ifPartialOutput = false; 61 // Out: output allocator type. 62 Media::AllocatorType allocatorType = Media::AllocatorType::HEAP_ALLOC; 63 // Out: output allocator release function. 64 Media::CustomFreePixelMap freeFunc = nullptr; 65 // Out: png nine patch context; 66 NinePatchContext ninePatchContext; 67 }; 68 69 struct ProgDecodeContext { 70 DecodeContext decodeContext; 71 72 static constexpr uint8_t DEFAULT_STEP = 10; 73 static constexpr uint8_t FULL_PROGRESS = 100; 74 // In: step size requesting advancement, in percentage, 1-100. 75 // if it is an incremental data source and the remaining image data does not 76 // reach the required amount, try to decode to the maximum possible number. 77 uint8_t desiredStep = DEFAULT_STEP; 78 79 // InOut: in percentage, 1-100. 80 // input total process progress after last decoding step, 81 // output total process progress after current decoding step. 82 uint8_t totalProcessProgress = 0; 83 }; 84 85 struct PixelDecodeOptions { 86 PlRect CropRect; 87 PlSize desiredSize; 88 float rotateDegrees = 0; 89 static constexpr uint32_t DEFAULT_SAMPLE_SIZE = 1; 90 uint32_t sampleSize = DEFAULT_SAMPLE_SIZE; 91 PlPixelFormat desiredPixelFormat = PlPixelFormat::RGBA_8888; 92 PlColorSpace desiredColorSpace = PlColorSpace::UNKNOWN; 93 PlAlphaType desireAlphaType = PlAlphaType::IMAGE_ALPHA_TYPE_PREMUL; 94 bool allowPartialImage = true; 95 bool editable = false; 96 }; 97 98 class AbsImageDecoder { 99 public: 100 static constexpr uint32_t DEFAULT_IMAGE_NUM = 1; 101 102 AbsImageDecoder() = default; 103 104 virtual ~AbsImageDecoder() = default; 105 106 // set image file source, start a new picture decoding process. 107 // the InputDataStream points to the beginning of the image file. 108 virtual void SetSource(InputDataStream &sourceStream) = 0; 109 110 // reset the decoder, clear all the decoder's status data cache. 111 virtual void Reset() = 0; 112 113 // judge a image source has a property or not. HasProperty(std::string key)114 virtual bool HasProperty(std::string key) 115 { 116 return false; 117 } 118 119 // set decode options before decode and get target decoded image info. 120 virtual uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) = 0; 121 122 // One-time decoding. 123 virtual uint32_t Decode(uint32_t index, DecodeContext &context) = 0; 124 125 // incremental decoding. 126 virtual uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) = 0; 127 128 // get the number of top level images in the image file. GetTopLevelImageNum(uint32_t & num)129 virtual uint32_t GetTopLevelImageNum(uint32_t &num) 130 { 131 num = DEFAULT_IMAGE_NUM; 132 return Media::SUCCESS; 133 } 134 135 // get image size without decoding image data. 136 virtual uint32_t GetImageSize(uint32_t index, PlSize &size) = 0; 137 138 // get image property. GetImagePropertyInt(uint32_t index,const std::string & key,int32_t & value)139 virtual uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) 140 { 141 return Media::ERR_MEDIA_INVALID_OPERATION; 142 } 143 144 // get image property. GetImagePropertyString(uint32_t index,const std::string & key,std::string & value)145 virtual uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) 146 { 147 return Media::ERR_MEDIA_INVALID_OPERATION; 148 } 149 150 // modify image property. ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const std::string & path)151 virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key, 152 const std::string &value, const std::string &path) 153 { 154 return Media::ERR_MEDIA_INVALID_OPERATION; 155 } 156 157 // define multiple subservices for this interface 158 static constexpr uint16_t SERVICE_DEFAULT = 0; 159 }; 160 } // namespace ImagePlugin 161 } // namespace OHOS 162 163 DECLARE_INTERFACE(OHOS::ImagePlugin::AbsImageDecoder, IMAGE_DECODER_IID) 164 165 #endif // ABS_IMAGE_DECODER_H 166