1 /*
2  * Copyright (C) 2023 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 #include "image_source_mdk.h"
17 
18 #include "common_utils.h"
19 #include "image_source_mdk_kits.h"
20 using namespace OHOS::Media;
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 const size_t SIZE_ZERO = 0;
27 struct ImageSourceNative_ {
28     ImageSourceNapi* napi = nullptr;
29     napi_env env = nullptr;
30 };
31 
32 MIDK_EXPORT
OH_ImageSource_InitNative(napi_env env,napi_value source)33 ImageSourceNative* OH_ImageSource_InitNative(napi_env env, napi_value source)
34 {
35     ImageSourceArgs args;
36     args.inEnv = env;
37     args.inVal = source;
38     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_UNWRAP, &args);
39     if (ret != IMAGE_RESULT_SUCCESS || args.napi == nullptr) {
40         return nullptr;
41     }
42     std::unique_ptr<ImageSourceNative> result = std::make_unique<ImageSourceNative>();
43     result->napi = args.napi;
44     result->env = env;
45     return result.release();
46 }
47 
48 MIDK_EXPORT
OH_ImageSource_Create(napi_env env,struct OhosImageSource * src,struct OhosImageSourceOps * ops,napi_value * res)49 int32_t OH_ImageSource_Create(napi_env env, struct OhosImageSource* src,
50     struct OhosImageSourceOps* ops, napi_value *res)
51 {
52     ImageSourceArgs args;
53     args.inEnv = env;
54     args.source = src;
55     args.sourceOps = ops;
56     args.outVal = res;
57     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE, &args);
58     return ret;
59 }
60 
61 MIDK_EXPORT
OH_ImageSource_CreateFromUri(napi_env env,char * uri,size_t size,struct OhosImageSourceOps * ops,napi_value * res)62 int32_t OH_ImageSource_CreateFromUri(napi_env env, char* uri, size_t size,
63     struct OhosImageSourceOps* ops, napi_value *res)
64 {
65     if (uri == nullptr || size == SIZE_ZERO) {
66         return IMAGE_RESULT_BAD_PARAMETER;
67     }
68     ImageSourceArgs args;
69     args.inEnv = env;
70     args.uri = std::string(uri, size);
71     args.sourceOps = ops;
72     args.outVal = res;
73     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_FROM_URI, &args);
74     return ret;
75 }
76 
77 MIDK_EXPORT
OH_ImageSource_CreateFromFd(napi_env env,int32_t fd,struct OhosImageSourceOps * ops,napi_value * res)78 int32_t OH_ImageSource_CreateFromFd(napi_env env, int32_t fd,
79     struct OhosImageSourceOps* ops, napi_value *res)
80 {
81     ImageSourceArgs args;
82     args.inEnv = env;
83     args.fd = fd;
84     args.sourceOps = ops;
85     args.outVal = res;
86     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_FROM_FD, &args);
87     return ret;
88 }
89 
90 MIDK_EXPORT
OH_ImageSource_CreateFromData(napi_env env,uint8_t * data,size_t dataSize,struct OhosImageSourceOps * ops,napi_value * res)91 int32_t OH_ImageSource_CreateFromData(napi_env env, uint8_t* data, size_t dataSize,
92     struct OhosImageSourceOps* ops, napi_value *res)
93 {
94     ImageSourceArgs args;
95     DataArray dataArray;
96     dataArray.data = data;
97     dataArray.dataSize = dataSize;
98     args.inEnv = env;
99     args.dataArray = dataArray;
100     args.sourceOps = ops;
101     args.outVal = res;
102     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_FROM_DATA, &args);
103     return ret;
104 }
105 
106 MIDK_EXPORT
OH_ImageSource_CreateFromRawFile(napi_env env,RawFileDescriptor rawFile,struct OhosImageSourceOps * ops,napi_value * res)107 int32_t OH_ImageSource_CreateFromRawFile(napi_env env, RawFileDescriptor rawFile,
108     struct OhosImageSourceOps* ops, napi_value *res)
109 {
110     ImageSourceArgs args;
111     args.inEnv = env;
112     args.rawFile = rawFile;
113     args.sourceOps = ops;
114     args.outVal = res;
115     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_FROM_RAW_FILE, &args);
116     return ret;
117 }
118 
119 MIDK_EXPORT
OH_ImageSource_CreateIncremental(napi_env env,struct OhosImageSource * source,struct OhosImageSourceOps * ops,napi_value * res)120 int32_t OH_ImageSource_CreateIncremental(napi_env env,
121     struct OhosImageSource* source, struct OhosImageSourceOps* ops, napi_value *res)
122 {
123     ImageSourceArgs args;
124     args.inEnv = env;
125     args.source = source;
126     args.sourceOps = ops;
127     args.outVal = res;
128     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_INCREMENTAL, &args);
129     return ret;
130 }
131 
132 MIDK_EXPORT
OH_ImageSource_CreateIncrementalFromData(napi_env env,uint8_t * data,size_t dataSize,struct OhosImageSourceOps * ops,napi_value * res)133 int32_t OH_ImageSource_CreateIncrementalFromData(napi_env env, uint8_t* data, size_t dataSize,
134     struct OhosImageSourceOps* ops, napi_value *res)
135 {
136     ImageSourceArgs args;
137     DataArray dataArray;
138     dataArray.data = data;
139     dataArray.dataSize = dataSize;
140     args.inEnv = env;
141     args.dataArray = dataArray;
142     args.sourceOps = ops;
143     args.outVal = res;
144     auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_INCREMENTAL, &args);
145     return ret;
146 }
147 
148 MIDK_EXPORT
OH_ImageSource_GetSupportedFormats(struct OhosImageSourceSupportedFormatList * res)149 int32_t OH_ImageSource_GetSupportedFormats(struct OhosImageSourceSupportedFormatList* res)
150 {
151     ImageSourceArgs args;
152     args.outFormats = res;
153     auto ret = ImageSourceNativeCall(STA_FUNC_IMAGE_SOURCE_GET_SUPPORTED_FORMATS, &args);
154     return ret;
155 }
156 
157 MIDK_EXPORT
OH_ImageSource_CreatePixelMap(const ImageSourceNative * native,struct OhosImageDecodingOps * ops,napi_value * res)158 int32_t OH_ImageSource_CreatePixelMap(const ImageSourceNative* native,
159     struct OhosImageDecodingOps* ops, napi_value *res)
160 {
161     if (native == nullptr || native->napi == nullptr) {
162         return IMAGE_RESULT_BAD_PARAMETER;
163     }
164     ImageSourceArgs args;
165     args.napi = native->napi;
166     args.inEnv = native->env;
167     args.decodingOps = ops;
168     args.outVal = res;
169     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_CREATE_PIXELMAP, &args);
170     return ret;
171 }
172 
173 MIDK_EXPORT
OH_ImageSource_CreatePixelMapList(const ImageSourceNative * native,struct OhosImageDecodingOps * ops,napi_value * res)174 int32_t OH_ImageSource_CreatePixelMapList(const ImageSourceNative* native,
175     struct OhosImageDecodingOps* ops, napi_value* res)
176 {
177     if (native == nullptr || native->napi == nullptr) {
178         return IMAGE_RESULT_BAD_PARAMETER;
179     }
180     ImageSourceArgs args;
181     args.napi = native->napi;
182     args.inEnv = native->env;
183     args.decodingOps = ops;
184     args.outVal = res;
185     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_CREATE_PIXELMAP_LIST, &args);
186     return ret;
187 }
188 
189 MIDK_EXPORT
OH_ImageSource_GetDelayTime(const ImageSourceNative * native,struct OhosImageSourceDelayTimeList * res)190 int32_t OH_ImageSource_GetDelayTime(const ImageSourceNative* native,
191     struct OhosImageSourceDelayTimeList* res)
192 {
193     if (native == nullptr || native->napi == nullptr) {
194         return IMAGE_RESULT_BAD_PARAMETER;
195     }
196     ImageSourceArgs args;
197     args.napi = native->napi;
198     args.outDelayTimes = res;
199     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_DELAY_TIME, &args);
200     return ret;
201 }
202 
203 MIDK_EXPORT
OH_ImageSource_GetFrameCount(const ImageSourceNative * native,uint32_t * res)204 int32_t OH_ImageSource_GetFrameCount(const ImageSourceNative* native, uint32_t *res)
205 {
206     if (native == nullptr || native->napi == nullptr) {
207         return IMAGE_RESULT_BAD_PARAMETER;
208     }
209     ImageSourceArgs args;
210     args.napi = native->napi;
211     args.outUint32 = res;
212     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_FRAME_COUNT, &args);
213     return ret;
214 }
215 
216 MIDK_EXPORT
OH_ImageSource_GetImageInfo(const ImageSourceNative * native,int32_t index,struct OhosImageSourceInfo * info)217 int32_t OH_ImageSource_GetImageInfo(const ImageSourceNative* native, int32_t index,
218     struct OhosImageSourceInfo* info)
219 {
220     if (native == nullptr || native->napi == nullptr) {
221         return IMAGE_RESULT_BAD_PARAMETER;
222     }
223     ImageSourceArgs args;
224     args.napi = native->napi;
225     args.inInt32 = index;
226     args.outInfo = info;
227     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_IMAGE_INFO, &args);
228     return ret;
229 }
230 
231 MIDK_EXPORT
OH_ImageSource_GetImageProperty(const ImageSourceNative * native,struct OhosImageSourceProperty * key,struct OhosImageSourceProperty * value)232 int32_t OH_ImageSource_GetImageProperty(const ImageSourceNative* native,
233     struct OhosImageSourceProperty* key, struct OhosImageSourceProperty* value)
234 {
235     if (native == nullptr || native->napi == nullptr) {
236         return IMAGE_RESULT_BAD_PARAMETER;
237     }
238     ImageSourceArgs args;
239     args.napi = native->napi;
240     args.inPropertyKey = key;
241     args.propertyVal = value;
242     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_IMAGE_PROPERTY, &args);
243     return ret;
244 }
245 
246 MIDK_EXPORT
OH_ImageSource_ModifyImageProperty(const ImageSourceNative * native,struct OhosImageSourceProperty * key,struct OhosImageSourceProperty * value)247 int32_t OH_ImageSource_ModifyImageProperty(const ImageSourceNative* native,
248     struct OhosImageSourceProperty* key, struct OhosImageSourceProperty* value)
249 {
250     if (native == nullptr || native->napi == nullptr) {
251         return IMAGE_RESULT_BAD_PARAMETER;
252     }
253     ImageSourceArgs args;
254     args.napi = native->napi;
255     args.inPropertyKey = key;
256     args.propertyVal = value;
257     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_MODIFY_IMAGE_PROPERTY, &args);
258     return ret;
259 }
260 
261 MIDK_EXPORT
OH_ImageSource_UpdateData(const ImageSourceNative * native,struct OhosImageSourceUpdateData * data)262 int32_t OH_ImageSource_UpdateData(const ImageSourceNative* native,
263     struct OhosImageSourceUpdateData* data)
264 {
265     if (native == nullptr || native->napi == nullptr) {
266         return IMAGE_RESULT_BAD_PARAMETER;
267     }
268     ImageSourceArgs args;
269     args.napi = native->napi;
270     args.inUpdateData = data;
271     auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_UPDATE_DATA, &args);
272     return ret;
273 }
274 
275 MIDK_EXPORT
OH_ImageSource_Release(ImageSourceNative * native)276 int32_t OH_ImageSource_Release(ImageSourceNative* native)
277 {
278     if (native != nullptr) {
279         delete native;
280     }
281     return IMAGE_RESULT_SUCCESS;
282 }
283 
284 #ifdef __cplusplus
285 };
286 #endif