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