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