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