• 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 };
104 
105 class AbsImageDecoder {
106 public:
107     static constexpr uint32_t DEFAULT_IMAGE_NUM = 1;
108     static constexpr uint32_t E_NO_EXIF = 1;
109 
110     AbsImageDecoder() = default;
111 
112     virtual ~AbsImageDecoder() = default;
113 
114     // set image file source, start a new picture decoding process.
115     // the InputDataStream points to the beginning of the image file.
116     virtual void SetSource(InputDataStream &sourceStream) = 0;
117 
118     // reset the decoder, clear all the decoder's status data cache.
119     virtual void Reset() = 0;
120 
121     // judge a image source has a property or not.
HasProperty(std::string key)122     virtual bool HasProperty(std::string key)
123     {
124         return false;
125     }
126 
127     // set decode options before decode and get target decoded image info.
128     virtual uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) = 0;
129 
130     // One-time decoding.
131     virtual uint32_t Decode(uint32_t index, DecodeContext &context) = 0;
132 
133     // incremental decoding.
134     virtual uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) = 0;
135 
136     // get the number of top level images in the image file.
GetTopLevelImageNum(uint32_t & num)137     virtual uint32_t GetTopLevelImageNum(uint32_t &num)
138     {
139         num = DEFAULT_IMAGE_NUM;
140         return Media::SUCCESS;
141     }
142 
143     // get image size without decoding image data.
144     virtual uint32_t GetImageSize(uint32_t index, PlSize &size) = 0;
145 
146     // get image property.
GetImagePropertyInt(uint32_t index,const std::string & key,int32_t & value)147     virtual uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value)
148     {
149         return Media::ERR_MEDIA_INVALID_OPERATION;
150     }
151 
152     // get image property.
GetImagePropertyString(uint32_t index,const std::string & key,std::string & value)153     virtual uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value)
154     {
155         return Media::ERR_MEDIA_INVALID_OPERATION;
156     }
157 
158     // modify image property.
ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const std::string & path)159     virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key,
160         const std::string &value, const std::string &path)
161     {
162         return Media::ERR_MEDIA_INVALID_OPERATION;
163     }
164 
ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,const int fd)165     virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key,
166                                          const std::string &value, const int fd)
167     {
168         return Media::ERR_MEDIA_INVALID_OPERATION;
169     }
170 
ModifyImageProperty(uint32_t index,const std::string & key,const std::string & value,uint8_t * data,uint32_t size)171     virtual uint32_t ModifyImageProperty(uint32_t index, const std::string &key,
172                                          const std::string &value, uint8_t *data, uint32_t size)
173     {
174         return Media::ERR_MEDIA_INVALID_OPERATION;
175     }
176 
177     // get filter area.
GetFilterArea(const int & privacyType,std::vector<std::pair<uint32_t,uint32_t>> & ranges)178     virtual uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges)
179     {
180         return E_NO_EXIF;
181     }
182 
183 #ifdef IMAGE_COLORSPACE_FLAG
184     // get current source is support icc profile or not.
IsSupportICCProfile()185     virtual bool IsSupportICCProfile()
186     {
187         return false;
188     }
189 
190     // if current source support icc. get relevant color gamut information by this method.
getGrColorSpace()191     virtual OHOS::ColorManager::ColorSpace getGrColorSpace()
192     {
193         return OHOS::ColorManager::ColorSpace(OHOS::ColorManager::ColorSpaceName::NONE);
194     }
195 #endif
196 
197     // define multiple subservices for this interface
198     static constexpr uint16_t SERVICE_DEFAULT = 0;
199 };
200 } // namespace ImagePlugin
201 } // namespace OHOS
202 
203 DECLARE_INTERFACE(OHOS::ImagePlugin::AbsImageDecoder, IMAGE_DECODER_IID)
204 
205 #endif // ABS_IMAGE_DECODER_H
206