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