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 struct ImageSourceNative_ {
27 ImageSourceNapi* napi = nullptr;
28 napi_env env = nullptr;
29 };
30
31 MIDK_EXPORT
OH_ImageSource_InitNative(napi_env env,napi_value source)32 ImageSourceNative* OH_ImageSource_InitNative(napi_env env, napi_value source)
33 {
34 ImageSourceArgs args;
35 args.inEnv = env;
36 args.inVal = source;
37 auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_UNWRAP, &args);
38 if (ret != IMAGE_RESULT_SUCCESS || args.napi == nullptr) {
39 return nullptr;
40 }
41 std::unique_ptr<ImageSourceNative> result = std::make_unique<ImageSourceNative>();
42 result->napi = args.napi;
43 result->env = env;
44 return result.release();
45 }
46
47 MIDK_EXPORT
OH_ImageSource_Create(napi_env env,struct OhosImageSource * src,struct OhosImageSourceOps * ops,napi_value * res)48 int32_t OH_ImageSource_Create(napi_env env, struct OhosImageSource* src,
49 struct OhosImageSourceOps* ops, napi_value *res)
50 {
51 ImageSourceArgs args;
52 args.inEnv = env;
53 args.source = src;
54 args.sourceOps = ops;
55 args.outVal = res;
56 auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE, &args);
57 return ret;
58 }
59
60 MIDK_EXPORT
OH_ImageSource_CreateIncremental(napi_env env,struct OhosImageSource * source,struct OhosImageSourceOps * ops,napi_value * res)61 int32_t OH_ImageSource_CreateIncremental(napi_env env,
62 struct OhosImageSource* source, struct OhosImageSourceOps* ops, napi_value *res)
63 {
64 ImageSourceArgs args;
65 args.inEnv = env;
66 args.source = source;
67 args.sourceOps = ops;
68 args.outVal = res;
69 auto ret = ImageSourceNativeCall(ENV_FUNC_IMAGE_SOURCE_CREATE_INCREMENTAL, &args);
70 return ret;
71 }
72
73 MIDK_EXPORT
OH_ImageSource_GetSupportedFormats(struct OhosImageSourceSupportedFormatList * res)74 int32_t OH_ImageSource_GetSupportedFormats(struct OhosImageSourceSupportedFormatList* res)
75 {
76 ImageSourceArgs args;
77 args.outFormats = res;
78 auto ret = ImageSourceNativeCall(STA_FUNC_IMAGE_SOURCE_GET_SUPPORTED_FORMATS, &args);
79 return ret;
80 }
81
82 MIDK_EXPORT
OH_ImageSource_CreatePixelMap(const ImageSourceNative * native,struct OhosImageDecodingOps * ops,napi_value * res)83 int32_t OH_ImageSource_CreatePixelMap(const ImageSourceNative* native,
84 struct OhosImageDecodingOps* ops, napi_value *res)
85 {
86 if (native == nullptr || native->napi == nullptr) {
87 return IMAGE_RESULT_BAD_PARAMETER;
88 }
89 ImageSourceArgs args;
90 args.napi = native->napi;
91 args.inEnv = native->env;
92 args.decodingOps = ops;
93 args.outVal = res;
94 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_CREATE_PIXELMAP, &args);
95 return ret;
96 }
97
98 MIDK_EXPORT
OH_ImageSource_CreatePixelMapList(const ImageSourceNative * native,struct OhosImageDecodingOps * ops,napi_value * res)99 int32_t OH_ImageSource_CreatePixelMapList(const ImageSourceNative* native,
100 struct OhosImageDecodingOps* ops, napi_value* res)
101 {
102 if (native == nullptr || native->napi == nullptr) {
103 return IMAGE_RESULT_BAD_PARAMETER;
104 }
105 ImageSourceArgs args;
106 args.napi = native->napi;
107 args.inEnv = native->env;
108 args.decodingOps = ops;
109 args.outVal = res;
110 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_CREATE_PIXELMAP_LIST, &args);
111 return ret;
112 }
113
114 MIDK_EXPORT
OH_ImageSource_GetDelayTime(const ImageSourceNative * native,struct OhosImageSourceDelayTimeList * res)115 int32_t OH_ImageSource_GetDelayTime(const ImageSourceNative* native,
116 struct OhosImageSourceDelayTimeList* res)
117 {
118 if (native == nullptr || native->napi == nullptr) {
119 return IMAGE_RESULT_BAD_PARAMETER;
120 }
121 ImageSourceArgs args;
122 args.napi = native->napi;
123 args.outDelayTimes = res;
124 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_DELAY_TIME, &args);
125 return ret;
126 }
127
128 MIDK_EXPORT
OH_ImageSource_GetFrameCount(const ImageSourceNative * native,uint32_t * res)129 int32_t OH_ImageSource_GetFrameCount(const ImageSourceNative* native, uint32_t *res)
130 {
131 if (native == nullptr || native->napi == nullptr) {
132 return IMAGE_RESULT_BAD_PARAMETER;
133 }
134 ImageSourceArgs args;
135 args.napi = native->napi;
136 args.outUint32 = res;
137 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_FRAME_COUNT, &args);
138 return ret;
139 }
140
141 MIDK_EXPORT
OH_ImageSource_GetImageInfo(const ImageSourceNative * native,int32_t index,struct OhosImageSourceInfo * info)142 int32_t OH_ImageSource_GetImageInfo(const ImageSourceNative* native, int32_t index,
143 struct OhosImageSourceInfo* info)
144 {
145 if (native == nullptr || native->napi == nullptr) {
146 return IMAGE_RESULT_BAD_PARAMETER;
147 }
148 ImageSourceArgs args;
149 args.napi = native->napi;
150 args.inInt32 = index;
151 args.outInfo = info;
152 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_IMAGE_INFO, &args);
153 return ret;
154 }
155
156 MIDK_EXPORT
OH_ImageSource_GetImageProperty(const ImageSourceNative * native,struct OhosImageSourceProperty * key,struct OhosImageSourceProperty * value)157 int32_t OH_ImageSource_GetImageProperty(const ImageSourceNative* native,
158 struct OhosImageSourceProperty* key, struct OhosImageSourceProperty* value)
159 {
160 if (native == nullptr || native->napi == nullptr) {
161 return IMAGE_RESULT_BAD_PARAMETER;
162 }
163 ImageSourceArgs args;
164 args.napi = native->napi;
165 args.inPropertyKey = key;
166 args.propertyVal = value;
167 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_GET_IMAGE_PROPERTY, &args);
168 return ret;
169 }
170
171 MIDK_EXPORT
OH_ImageSource_ModifyImageProperty(const ImageSourceNative * native,struct OhosImageSourceProperty * key,struct OhosImageSourceProperty * value)172 int32_t OH_ImageSource_ModifyImageProperty(const ImageSourceNative* native,
173 struct OhosImageSourceProperty* key, struct OhosImageSourceProperty* value)
174 {
175 if (native == nullptr || native->napi == nullptr) {
176 return IMAGE_RESULT_BAD_PARAMETER;
177 }
178 ImageSourceArgs args;
179 args.napi = native->napi;
180 args.inPropertyKey = key;
181 args.propertyVal = value;
182 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_MODIFY_IMAGE_PROPERTY, &args);
183 return ret;
184 }
185
186 MIDK_EXPORT
OH_ImageSource_UpdateData(const ImageSourceNative * native,struct OhosImageSourceUpdateData * data)187 int32_t OH_ImageSource_UpdateData(const ImageSourceNative* native,
188 struct OhosImageSourceUpdateData* data)
189 {
190 if (native == nullptr || native->napi == nullptr) {
191 return IMAGE_RESULT_BAD_PARAMETER;
192 }
193 ImageSourceArgs args;
194 args.napi = native->napi;
195 args.inUpdateData = data;
196 auto ret = ImageSourceNativeCall(CTX_FUNC_IMAGE_SOURCE_UPDATE_DATA, &args);
197 return ret;
198 }
199
200 MIDK_EXPORT
OH_ImageSource_Release(ImageSourceNative * native)201 int32_t OH_ImageSource_Release(ImageSourceNative* native)
202 {
203 if (native != nullptr) {
204 delete native;
205 }
206 return IMAGE_RESULT_SUCCESS;
207 }
208
209 #ifdef __cplusplus
210 };
211 #endif