• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_test.h"
17 #include "node_api.h"
18 #include "image_pixel_map_napi.h"
19 #include <cstdlib>
20 #include "hilog/log.h"
21 #include "raw_file.h"
22 
23 namespace {
24     constexpr size_t SIZE_ZERO = 0;
25     constexpr size_t SIZE_ONE = 1;
26     constexpr size_t SIZE_TWO = 2;
27     constexpr size_t SIZE_THREE = 3;
28     constexpr size_t DEFAULT_STRING_SIZE = 64;
29     constexpr uint32_t ARGS_FIRST = 0;
30     constexpr uint32_t ARGS_SECOND = 1;
31     constexpr uint32_t ARGS_THIRD = 2;
32     constexpr uint32_t INVALID_FRAME_COUNT = 0;
33     constexpr int8_t INT8_FALSE = 0;
34     constexpr int8_t INT8_TRUE = 1;
35     constexpr int32_t DEFAULT_INDEX = 0;
36 }
37 
38 namespace OHOS {
39 namespace Media {
40 
41 const unsigned int LOG_ID = 0xD002B05;
42 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_ID, "ImageSourceNDKTest"};
43 #define MY_HILOG(op, fmt, args...) \
44     do {                                  \
45         op(LABEL, "{%{public}s:%{public}d} " fmt, __FUNCTION__, __LINE__, ##args);  \
46     } while (0)
47 #define DEBUG_LOG(fmt, ...) MY_HILOG(OHOS::HiviewDFX::HiLog::Info, fmt, ##__VA_ARGS__)
48 #define DEBUG_PTR(p) (((p) == nullptr)?"nullptr":"not nullptr")
49 
50 #define STATIC_FUNCTION(n, f) { (n), nullptr, (f), nullptr, nullptr, nullptr, napi_static, nullptr }
Init(napi_env env,napi_value exports)51 napi_value ImageSourceNDKTest::Init(napi_env env, napi_value exports)
52 {
53     napi_property_descriptor props[] = {
54         STATIC_FUNCTION("create", Create),
55         STATIC_FUNCTION("createFromFd", CreateFromFd),
56         STATIC_FUNCTION("createFromUri", CreateFromUri),
57         STATIC_FUNCTION("createFromData", CreateFromData),
58         STATIC_FUNCTION("createFromRawFile", CreateFromRawFile),
59         STATIC_FUNCTION("createIncremental", CreateIncremental),
60         STATIC_FUNCTION("createIncrementalFromData", CreateIncrementalFromData),
61         STATIC_FUNCTION("initNative", InitNative),
62         STATIC_FUNCTION("createPixelMap", CreatePixelMap),
63         STATIC_FUNCTION("createPixelMapList", CreatePixelMapList),
64         STATIC_FUNCTION("getDelayTime", GetDelayTime),
65         STATIC_FUNCTION("getFrameCount", GetFrameCount),
66         STATIC_FUNCTION("getSupportedFormats", GetSupportedFormats),
67         STATIC_FUNCTION("getImageInfo", GetImageInfo),
68         STATIC_FUNCTION("getImageProperty", GetImageProperty),
69         STATIC_FUNCTION("modifyImageProperty", ModifyImageProperty),
70         STATIC_FUNCTION("updateData", UpdateData),
71         STATIC_FUNCTION("release", Release),
72     };
73     napi_define_properties(env, exports, sizeof(props) / sizeof(props[ARGS_FIRST]), props);
74     return exports;
75 }
76 
GetBoolProperty(napi_env env,napi_value root,const char * utf8name,int8_t * res)77 static bool GetBoolProperty(napi_env env, napi_value root, const char* utf8name, int8_t* res)
78 {
79     napi_value property = nullptr;
80     auto status = napi_get_named_property(env, root, utf8name, &property);
81     if (status != napi_ok || property == nullptr) {
82         DEBUG_LOG("Get property error %{public}s", utf8name);
83         return false;
84     }
85     bool tmp = false;
86     status = napi_get_value_bool(env, property, &tmp);
87     *res = tmp ? INT8_TRUE : INT8_FALSE;
88     return (status == napi_ok);
89 }
90 
GetInt32Property(napi_env env,napi_value root,const char * utf8name,int32_t * res)91 static bool GetInt32Property(napi_env env, napi_value root, const char* utf8name, int32_t* res)
92 {
93     napi_value property = nullptr;
94     auto status = napi_get_named_property(env, root, utf8name, &property);
95     if (status != napi_ok || property == nullptr) {
96         DEBUG_LOG("Get property error %{public}s", utf8name);
97         return false;
98     }
99     return (napi_get_value_int32(env, property, res) == napi_ok);
100 }
101 
GetUint32Property(napi_env env,napi_value root,const char * utf8name,uint32_t * res)102 static bool GetUint32Property(napi_env env, napi_value root, const char* utf8name, uint32_t* res)
103 {
104     napi_value property = nullptr;
105     auto status = napi_get_named_property(env, root, utf8name, &property);
106     if (status != napi_ok || property == nullptr) {
107         DEBUG_LOG("Get property error %{public}s", utf8name);
108         return false;
109     }
110     return (napi_get_value_uint32(env, property, res) == napi_ok);
111 }
112 
GetArrayBufferProperty(napi_env env,napi_value root,const char * utf8name,uint8_t ** buffer,size_t * bufferSize)113 static bool GetArrayBufferProperty(napi_env env, napi_value root, const char* utf8name,
114     uint8_t** buffer, size_t* bufferSize)
115 {
116     napi_value property = nullptr;
117     auto status = napi_get_named_property(env, root, utf8name, &property);
118     if (status != napi_ok || property == nullptr) {
119         DEBUG_LOG("Get property error %{public}s", utf8name);
120         return false;
121     }
122     void* tmp;
123     status = napi_get_arraybuffer_info(env, property, &tmp, bufferSize);
124     *buffer = static_cast<uint8_t*>(tmp);
125     return (status == napi_ok);
126 }
127 
GetStringValue(napi_env env,napi_value value,char ** buffer,size_t * bufferSize)128 static bool GetStringValue(napi_env env, napi_value value, char** buffer, size_t *bufferSize)
129 {
130     if (napi_ok != napi_get_value_string_utf8(env, value, nullptr, SIZE_ZERO, bufferSize)
131         && *bufferSize == SIZE_ZERO) {
132         DEBUG_LOG("Get napi string length error");
133         return false;
134     }
135     *buffer = (char*)malloc((*bufferSize) + 1);
136     if (napi_ok != napi_get_value_string_utf8(env, value, *buffer, (*bufferSize) + 1, bufferSize)) {
137         DEBUG_LOG("Get napi string error");
138         return false;
139     }
140     return (*bufferSize > SIZE_ZERO);
141 }
142 
setInt32NamedProperty(napi_env env,napi_value object,const char * utf8name,uint32_t value)143 static void setInt32NamedProperty(napi_env env, napi_value object, const char* utf8name, uint32_t value)
144 {
145     napi_value tmp;
146     napi_create_int32(env, value, &tmp);
147     napi_set_named_property(env, object, utf8name, tmp);
148 }
149 
checkType(napi_env env,napi_value arg,napi_valuetype type)150 static bool checkType(napi_env env, napi_value arg, napi_valuetype type)
151 {
152     napi_valuetype argType = napi_undefined;
153     napi_typeof(env, arg, &argType);
154     return (type == argType);
155 }
checkArgs(const napi_value * argValue,size_t argCount,size_t want)156 static bool checkArgs(const napi_value* argValue, size_t argCount, size_t want)
157 {
158     if (argCount < want) {
159         DEBUG_LOG("argCount %{public}zu < want %{public}zu", argCount, want);
160         return false;
161     }
162     for (size_t i = SIZE_ZERO; i < want; i++) {
163         if (argValue[i] == nullptr) {
164             DEBUG_LOG("argValue[%{public}zu] is nullptr", i);
165             return false;
166         }
167     }
168     return true;
169 }
parseImageSource(napi_env env,napi_value arg,struct OhosImageSource & src)170 static bool parseImageSource(napi_env env, napi_value arg, struct OhosImageSource &src)
171 {
172     if (env == nullptr || arg == nullptr) {
173         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
174         return false;
175     }
176     bool isArrayBuffer = false;
177     if (checkType(env, arg, napi_string)) {
178         DEBUG_LOG("Uri arg In");
179         if (!GetStringValue(env, arg, &src.uri, &src.uriSize)) {
180             DEBUG_LOG("Uri arg failed");
181             return false;
182         }
183         DEBUG_LOG("Uri arg %{public}s", src.uri);
184         return true;
185     } else if (checkType(env, arg, napi_number)) {
186         DEBUG_LOG("Fd arg In");
187         if (napi_ok != napi_get_value_int32(env, arg, &(src.fd))) {
188             DEBUG_LOG("Fd arg failed");
189             return false;
190         }
191         return true;
192     } else if (napi_is_arraybuffer(env, arg, &isArrayBuffer) == napi_ok && isArrayBuffer) {
193         DEBUG_LOG("Buffer arg In");
194         void* buf = nullptr;
195         if (napi_ok != napi_get_arraybuffer_info(env, arg, &buf, &(src.bufferSize)) || buf == nullptr ||
196             src.bufferSize == SIZE_ZERO) {
197             DEBUG_LOG("Fd arg failed");
198             return false;
199         }
200         src.buffer = static_cast<uint8_t*>(buf);
201         return true;
202     }
203     DEBUG_LOG("Invaild arg type");
204     return false;
205 }
OhosImageSourceRelease(struct OhosImageSource & src)206 static void OhosImageSourceRelease(struct OhosImageSource &src)
207 {
208     if (src.uri != nullptr) {
209         free(src.uri);
210         src.uri = nullptr;
211     }
212 }
parseImageSourceOpt(napi_env env,napi_value arg,struct OhosImageSourceOps & src)213 static bool parseImageSourceOpt(napi_env env, napi_value arg, struct OhosImageSourceOps &src)
214 {
215     if (env == nullptr || arg == nullptr) {
216         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
217         return false;
218     }
219     // Optional parameters, no need check error.
220     GetInt32Property(env, arg, "density", &(src.density));
221     GetInt32Property(env, arg, "pixelFormat", &(src.pixelFormat));
222     napi_value nSize = nullptr;
223     auto status = napi_get_named_property(env, arg, "size", &nSize);
224     if (status == napi_ok && nSize != nullptr) {
225         GetInt32Property(env, nSize, "width", &(src.size.width));
226         GetInt32Property(env, nSize, "height", &(src.size.height));
227     }
228     return true;
229 }
parseImageDecodingOps(napi_env env,napi_value arg,struct OhosImageDecodingOps & ops)230 static bool parseImageDecodingOps(napi_env env, napi_value arg, struct OhosImageDecodingOps &ops)
231 {
232     if (env == nullptr || arg == nullptr) {
233         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
234         return false;
235     }
236 
237     // Optional parameters, no need check error.
238     GetBoolProperty(env, arg, "editable", &(ops.editable));
239     GetInt32Property(env, arg, "pixelFormat", &(ops.pixelFormat));
240     GetInt32Property(env, arg, "fitDensity", &(ops.fitDensity));
241     GetUint32Property(env, arg, "index", &(ops.index));
242     GetUint32Property(env, arg, "sampleSize", &(ops.sampleSize));
243     GetUint32Property(env, arg, "rotate", &(ops.rotate));
244     napi_value nSize = nullptr;
245     auto status = napi_get_named_property(env, arg, "size", &nSize);
246     if (status == napi_ok && nSize != nullptr) {
247         GetInt32Property(env, nSize, "width", &(ops.size.width));
248         GetInt32Property(env, nSize, "height", &(ops.size.height));
249     }
250     napi_value nRegion = nullptr;
251     status = napi_get_named_property(env, arg, "region", &nRegion);
252     if (status == napi_ok && nRegion != nullptr) {
253         GetInt32Property(env, nRegion, "x", &(ops.region.x));
254         GetInt32Property(env, nRegion, "y", &(ops.region.y));
255         GetInt32Property(env, nRegion, "width", &(ops.region.width));
256         GetInt32Property(env, nRegion, "height", &(ops.region.height));
257     }
258     return true;
259 }
createUndefine(napi_env env)260 static napi_value createUndefine(napi_env env)
261 {
262     napi_value udfVal = nullptr;
263     napi_get_undefined(env, &udfVal);
264     return udfVal;
265 }
createResultValue(napi_env env,int32_t resCode,napi_value res=nullptr)266 static napi_value createResultValue(napi_env env, int32_t resCode, napi_value res = nullptr)
267 {
268     napi_value result = nullptr;
269     // JS result like
270     // {code: <error code>, result: <result value>}
271     napi_value nRes = nullptr;
272     napi_create_int32(env, resCode, &nRes);
273     napi_create_object(env, &result);
274     napi_set_named_property(env, result, "code", nRes);
275     if (res != nullptr) {
276         napi_set_named_property(env, result, "result", res);
277     }
278     return result;
279 }
Create(napi_env env,napi_callback_info info)280 napi_value ImageSourceNDKTest::Create(napi_env env, napi_callback_info info)
281 {
282     napi_value thisVar = nullptr;
283     napi_value argValue[SIZE_TWO] = {0};
284     size_t argCount = SIZE_TWO;
285     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
286         !checkArgs(argValue, argCount, SIZE_TWO)) {
287         return createUndefine(env);
288     }
289     struct OhosImageSource src;
290     if (!parseImageSource(env, argValue[ARGS_FIRST], src)) {
291         DEBUG_LOG("parseImageSource failed!!!");
292         OhosImageSourceRelease(src);
293         return createUndefine(env);
294     }
295     struct OhosImageSourceOps ops;
296     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
297         DEBUG_LOG("parseImageSourceOpt failed!!!");
298         OhosImageSourceRelease(src);
299         return createUndefine(env);
300     }
301     napi_value imageSource = nullptr;
302     int32_t res = OH_ImageSource_Create(env, &src, &ops, &imageSource);
303     OhosImageSourceRelease(src);
304     return createResultValue(env, res, imageSource);
305 }
306 
CreateFromFd(napi_env env,napi_callback_info info)307 napi_value ImageSourceNDKTest::CreateFromFd(napi_env env, napi_callback_info info)
308 {
309     napi_value thisVar = nullptr;
310     napi_value argValue[SIZE_TWO] = {0};
311     size_t argCount = SIZE_TWO;
312     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
313         !checkArgs(argValue, argCount, SIZE_TWO)) {
314         return createUndefine(env);
315     }
316     if (!checkType(env, argValue[ARGS_FIRST], napi_number)) {
317         DEBUG_LOG("Fd type is not number");
318         return createUndefine(env);
319     }
320     int32_t fd;
321     if (napi_ok != napi_get_value_int32(env, argValue[ARGS_FIRST], &(fd))) {
322         DEBUG_LOG("Fd arg failed");
323         return createUndefine(env);
324     }
325     struct OhosImageSourceOps ops;
326     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
327         DEBUG_LOG("parseImageSourceOpt failed!!!");
328         return createUndefine(env);
329     }
330     napi_value imageSource = nullptr;
331     int32_t res = OH_ImageSource_CreateFromFd(env, fd, &ops, &imageSource);
332     return createResultValue(env, res, imageSource);
333 }
334 
CreateFromData(napi_env env,napi_callback_info info)335 napi_value ImageSourceNDKTest::CreateFromData(napi_env env, napi_callback_info info)
336 {
337     napi_value thisVar = nullptr;
338     napi_value argValue[SIZE_TWO] = {0};
339     size_t argCount = SIZE_TWO;
340     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
341         !checkArgs(argValue, argCount, SIZE_TWO)) {
342         return createUndefine(env);
343     }
344     bool isArrayBuffer = false;
345     if (napi_is_arraybuffer(env, argValue[ARGS_FIRST], &isArrayBuffer) != napi_ok || !isArrayBuffer) {
346         DEBUG_LOG("Buffer type is not arraybuffer");
347         return createUndefine(env);
348     }
349     void* buf = nullptr;
350     size_t bufferSize = 0;
351     if (napi_ok != napi_get_arraybuffer_info(env, argValue[ARGS_FIRST], &buf, &(bufferSize)) ||
352         buf == nullptr || bufferSize == SIZE_ZERO) {
353         DEBUG_LOG("buf arg failed");
354         return createUndefine(env);
355     }
356     struct OhosImageSourceOps ops;
357     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
358         DEBUG_LOG("parseImageSourceOpt failed!!!");
359         return createUndefine(env);
360     }
361     napi_value imageSource = nullptr;
362     uint8_t* data = reinterpret_cast<uint8_t*>(buf);
363     int32_t res = OH_ImageSource_CreateFromData(env, data, bufferSize, &ops, &imageSource);
364     return createResultValue(env, res, imageSource);
365 }
366 
CreateFromUri(napi_env env,napi_callback_info info)367 napi_value ImageSourceNDKTest::CreateFromUri(napi_env env, napi_callback_info info)
368 {
369     napi_value thisVar = nullptr;
370     napi_value argValue[SIZE_TWO] = {0};
371     size_t argCount = SIZE_TWO;
372     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
373         !checkArgs(argValue, argCount, SIZE_TWO)) {
374         return createUndefine(env);
375     }
376     size_t uriSize;
377     char uri[2048];
378     napi_get_value_string_utf8(env, argValue[ARGS_FIRST], uri, sizeof(uri), &uriSize);
379 
380     struct OhosImageSourceOps ops;
381     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
382         DEBUG_LOG("parseImageSourceOpt failed!!!");
383         return createUndefine(env);
384     }
385     napi_value imageSource = nullptr;
386     int32_t res = OH_ImageSource_CreateFromUri(env, uri, uriSize, &ops, &imageSource);
387     return createResultValue(env, res, imageSource);
388 }
389 
CreateFromRawFile(napi_env env,napi_callback_info info)390 napi_value ImageSourceNDKTest::CreateFromRawFile(napi_env env, napi_callback_info info)
391 {
392     napi_value thisVar = nullptr;
393     napi_value argValue[SIZE_TWO] = {0};
394     size_t argCount = SIZE_TWO;
395     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
396         !checkArgs(argValue, argCount, SIZE_TWO)) {
397         return createUndefine(env);
398     }
399     RawFileDescriptor rawFileDescriptor = {};
400     int32_t fd, offset, length;
401     if (!GetInt32Property(env, argValue[ARGS_FIRST], "fd", &fd)) {
402         DEBUG_LOG("get fd failed");
403         return createUndefine(env);
404     }
405     rawFileDescriptor.fd = fd;
406     if (!GetInt32Property(env, argValue[ARGS_FIRST], "offset", &offset)) {
407         DEBUG_LOG("get offset failed");
408         return createUndefine(env);
409     }
410     rawFileDescriptor.start = offset;
411     if (!GetInt32Property(env, argValue[ARGS_FIRST], "length", &length)) {
412         DEBUG_LOG("get length failed");
413         return createUndefine(env);
414     }
415     rawFileDescriptor.length = length;
416 
417     struct OhosImageSourceOps ops;
418     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
419         DEBUG_LOG("parseImageSourceOpt failed!!!");
420         return createUndefine(env);
421     }
422     napi_value imageSource = nullptr;
423     int32_t res = OH_ImageSource_CreateFromRawFile(env, rawFileDescriptor, &ops, &imageSource);
424     return createResultValue(env, res, imageSource);
425 }
426 
CreateIncremental(napi_env env,napi_callback_info info)427 napi_value ImageSourceNDKTest::CreateIncremental(napi_env env, napi_callback_info info)
428 {
429     napi_value thisVar = nullptr;
430     napi_value argValue[SIZE_TWO] = {0};
431     size_t argCount = SIZE_TWO;
432     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
433         !checkArgs(argValue, argCount, SIZE_TWO)) {
434         return createUndefine(env);
435     }
436     // No source check. There is no source input.
437     struct OhosImageSource src;
438     parseImageSource(env, argValue[ARGS_FIRST], src);
439     struct OhosImageSourceOps ops;
440     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
441         DEBUG_LOG("parseImageSourceOpt failed!!!");
442         OhosImageSourceRelease(src);
443         return createUndefine(env);
444     }
445     napi_value imageSource = nullptr;
446     int32_t res = OH_ImageSource_CreateIncremental(env, &src, &ops, &imageSource);
447     if (res != OHOS_IMAGE_RESULT_SUCCESS || imageSource == nullptr) {
448         OhosImageSourceRelease(src);
449         return createUndefine(env);
450     }
451     OhosImageSourceRelease(src);
452     return createResultValue(env, res, imageSource);
453 }
454 
CreateIncrementalFromData(napi_env env,napi_callback_info info)455 napi_value ImageSourceNDKTest::CreateIncrementalFromData(napi_env env, napi_callback_info info)
456 {
457     napi_value thisVar = nullptr;
458     napi_value argValue[SIZE_TWO] = {0};
459     size_t argCount = SIZE_TWO;
460     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
461         !checkArgs(argValue, argCount, SIZE_TWO)) {
462         return createUndefine(env);
463     }
464     bool isArrayBuffer = false;
465     if (napi_is_arraybuffer(env, argValue[ARGS_FIRST], &isArrayBuffer) != napi_ok || !isArrayBuffer) {
466         DEBUG_LOG("Buffer type is not arraybuffer");
467         return createUndefine(env);
468     }
469     void* buf = nullptr;
470     size_t bufferSize = 0;
471     if (napi_ok != napi_get_arraybuffer_info(env, argValue[ARGS_FIRST], &buf, &(bufferSize)) ||
472         buf == nullptr || bufferSize == SIZE_ZERO) {
473         DEBUG_LOG("buf arg failed");
474         return createUndefine(env);
475     }
476     struct OhosImageSourceOps ops;
477     if (!parseImageSourceOpt(env, argValue[ARGS_SECOND], ops)) {
478         DEBUG_LOG("parseImageSourceOpt failed!!!");
479         return createUndefine(env);
480     }
481     napi_value imageSource = nullptr;
482     uint8_t* data = reinterpret_cast<uint8_t*>(buf);
483     int32_t res = OH_ImageSource_CreateIncrementalFromData(env, data, bufferSize, &ops, &imageSource);
484     return createResultValue(env, res, imageSource);
485 }
486 
getNativeImageSource(napi_env env,napi_callback_info info,napi_value * argValue,size_t & argCount)487 static ImageSourceNative* getNativeImageSource(napi_env env, napi_callback_info info,
488     napi_value* argValue, size_t &argCount)
489 {
490     napi_value thisVar = nullptr;
491     if (argValue == nullptr || argCount == SIZE_ZERO) {
492         DEBUG_LOG("Invaild input!");
493         return nullptr;
494     }
495     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok) {
496         return nullptr;
497     }
498     return OH_ImageSource_InitNative(env, argValue[ARGS_FIRST]);
499 }
500 
InitNative(napi_env env,napi_callback_info info)501 napi_value ImageSourceNDKTest::InitNative(napi_env env, napi_callback_info info)
502 {
503     napi_value argValue[SIZE_ONE] = {0};
504     size_t argCount = SIZE_ONE;
505 
506     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
507     if (native == nullptr) {
508         return createUndefine(env);
509     }
510     return createResultValue(env, OHOS_IMAGE_RESULT_SUCCESS);
511 }
512 
CreatePixelMap(napi_env env,napi_callback_info info)513 napi_value ImageSourceNDKTest::CreatePixelMap(napi_env env, napi_callback_info info)
514 {
515     napi_value argValue[SIZE_TWO] = {0};
516     size_t argCount = SIZE_TWO;
517 
518     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
519     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
520         DEBUG_LOG("argValue check failed");
521         return createUndefine(env);
522     }
523     struct OhosImageDecodingOps ops;
524     if (!parseImageDecodingOps(env, argValue[ARGS_SECOND], ops)) {
525         DEBUG_LOG("parseImageDecodingOps failed");
526         return createUndefine(env);
527     }
528     napi_value pixelmap = nullptr;
529     int32_t res = OH_ImageSource_CreatePixelMap(native, &ops, &pixelmap);
530     return createResultValue(env, res, pixelmap);
531 }
532 
CreatePixelMapList(napi_env env,napi_callback_info info)533 napi_value ImageSourceNDKTest::CreatePixelMapList(napi_env env, napi_callback_info info)
534 {
535     napi_value argValue[SIZE_TWO] = {0};
536     size_t argCount = SIZE_TWO;
537 
538     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
539     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
540         DEBUG_LOG("argValue check failed");
541         return createUndefine(env);
542     }
543     struct OhosImageDecodingOps ops;
544     if (!parseImageDecodingOps(env, argValue[ARGS_SECOND], ops)) {
545         DEBUG_LOG("parseImageDecodingOps failed");
546         return createUndefine(env);
547     }
548     napi_value pixelmapList = nullptr;
549     int32_t res = OH_ImageSource_CreatePixelMapList(native, &ops, &pixelmapList);
550     return createResultValue(env, res, pixelmapList);
551 }
552 
ReleaseDelayTimeList(struct OhosImageSourceDelayTimeList & timeList)553 static void ReleaseDelayTimeList(struct OhosImageSourceDelayTimeList &timeList)
554 {
555     if (timeList.delayTimeList != nullptr) {
556         timeList.size = SIZE_ZERO;
557         free(timeList.delayTimeList);
558         timeList.delayTimeList = nullptr;
559     }
560 }
561 
GetDelayTime(napi_env env,napi_callback_info info)562 napi_value ImageSourceNDKTest::GetDelayTime(napi_env env, napi_callback_info info)  __attribute__((no_sanitize("cfi")))
563 {
564     napi_value argValue[SIZE_ONE] = {0};
565     size_t argCount = SIZE_ONE;
566 
567     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
568     if (native == nullptr) {
569         DEBUG_LOG("argValue check failed");
570         return createUndefine(env);
571     }
572 
573     struct OhosImageSourceDelayTimeList timeList;
574     int32_t res = OH_ImageSource_GetDelayTime(native, &timeList);
575     if (timeList.size == SIZE_ZERO || res != OHOS_IMAGE_RESULT_SUCCESS) {
576         DEBUG_LOG("Delay time list get failed");
577         return createUndefine(env);
578     }
579     timeList.delayTimeList = (int32_t*)malloc(sizeof(int32_t) * timeList.size);
580     res = OH_ImageSource_GetDelayTime(native, &timeList);
581 
582     napi_value result;
583     napi_create_array(env, &result);
584     for (size_t i = SIZE_ZERO; i < timeList.size; i++) {
585         napi_value nDelayTime = nullptr;
586         napi_create_int32(env, timeList.delayTimeList[i], &nDelayTime);
587         napi_set_element(env, result, i, nDelayTime);
588     }
589     ReleaseDelayTimeList(timeList);
590     return createResultValue(env, res, result);
591 }
592 
GetFrameCount(napi_env env,napi_callback_info info)593 napi_value ImageSourceNDKTest::GetFrameCount(napi_env env, napi_callback_info info)
594 {
595     napi_value argValue[SIZE_ONE] = {0};
596     size_t argCount = SIZE_ONE;
597 
598     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
599     if (native == nullptr) {
600         DEBUG_LOG("argValue check failed");
601         return createUndefine(env);
602     }
603     uint32_t frameCount = INVALID_FRAME_COUNT;
604     int32_t res = OH_ImageSource_GetFrameCount(native, &frameCount);
605 
606     napi_value nFrameCount = nullptr;
607     napi_create_int32(env, frameCount, &nFrameCount);
608     return createResultValue(env, res, nFrameCount);
609 }
ReleaseSupportedFormatList(struct OhosImageSourceSupportedFormatList & formatList)610 static void ReleaseSupportedFormatList(struct OhosImageSourceSupportedFormatList &formatList)
611 {
612     if (formatList.supportedFormatList == nullptr) {
613         return;
614     }
615     for (size_t i = SIZE_ZERO; i < formatList.size; i++) {
616         auto format = formatList.supportedFormatList[i];
617         if (format != nullptr) {
618             if (format->format != nullptr) {
619                 free(format->format);
620                 format->format = nullptr;
621             }
622             free(format);
623         }
624     }
625     free(formatList.supportedFormatList);
626     formatList.supportedFormatList = nullptr;
627 }
GetSupportedFormats(napi_env env,napi_callback_info info)628 napi_value ImageSourceNDKTest::GetSupportedFormats(napi_env env, napi_callback_info info)  __attribute__((no_sanitize("cfi")))
629 {
630     struct OhosImageSourceSupportedFormatList formatList;
631     int32_t res = OH_ImageSource_GetSupportedFormats(&formatList);
632     if (formatList.size == SIZE_ZERO || res != OHOS_IMAGE_RESULT_SUCCESS) {
633         DEBUG_LOG("Supported format list get failed");
634         return createUndefine(env);
635     }
636     formatList.supportedFormatList = (struct OhosImageSourceSupportedFormat**)malloc(
637         sizeof(struct OhosImageSourceSupportedFormat*) * formatList.size);
638     for (size_t i = 0; i < formatList.size; i++) {
639         formatList.supportedFormatList[i] = (struct OhosImageSourceSupportedFormat*)malloc(
640             sizeof(struct OhosImageSourceSupportedFormat));
641         formatList.supportedFormatList[i]->format = (char*)malloc(DEFAULT_STRING_SIZE);
642         formatList.supportedFormatList[i]->size = DEFAULT_STRING_SIZE;
643     }
644 
645     res = OH_ImageSource_GetSupportedFormats(&formatList);
646     napi_value result;
647     napi_create_array(env, &result);
648     for (size_t i = SIZE_ZERO; i < formatList.size; i++) {
649         napi_value nFormat = nullptr;
650         auto format = formatList.supportedFormatList[i];
651         napi_create_string_utf8(env, format->format, format->size, &nFormat);
652         napi_set_element(env, result, i, nFormat);
653     }
654     ReleaseSupportedFormatList(formatList);
655     return createResultValue(env, res, result);
656 }
657 
createImageInfoNVal(napi_env env,const struct OhosImageSourceInfo & imageInfo)658 static napi_value createImageInfoNVal(napi_env env, const struct OhosImageSourceInfo &imageInfo)
659 {
660     napi_value result = nullptr;
661     napi_create_object(env, &result);
662     setInt32NamedProperty(env, result, "pixelFormat", imageInfo.pixelFormat);
663     setInt32NamedProperty(env, result, "colorSpace", imageInfo.colorSpace);
664     setInt32NamedProperty(env, result, "alphaType", imageInfo.alphaType);
665     setInt32NamedProperty(env, result, "density", imageInfo.density);
666     napi_value nSize = nullptr;
667     napi_create_object(env, &nSize);
668     setInt32NamedProperty(env, nSize, "width", imageInfo.size.width);
669     setInt32NamedProperty(env, nSize, "height", imageInfo.size.height);
670     napi_set_named_property(env, result, "size", nSize);
671     return result;
672 }
673 
GetImageInfo(napi_env env,napi_callback_info info)674 napi_value ImageSourceNDKTest::GetImageInfo(napi_env env, napi_callback_info info)
675 {
676     napi_value argValue[SIZE_TWO] = {0};
677     size_t argCount = SIZE_TWO;
678     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
679     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
680         DEBUG_LOG("argValue check failed");
681         return createUndefine(env);
682     }
683     int32_t index = DEFAULT_INDEX;
684     napi_get_value_int32(env, argValue[ARGS_SECOND], &index);
685     struct OhosImageSourceInfo imageInfo;
686     int32_t res = OH_ImageSource_GetImageInfo(native, index, &imageInfo);
687     napi_value nImageInfo = createImageInfoNVal(env, imageInfo);
688     return createResultValue(env, res, nImageInfo);
689 }
690 
GetImageProperty(napi_env env,napi_callback_info info)691 napi_value ImageSourceNDKTest::GetImageProperty(napi_env env, napi_callback_info info)  __attribute__((no_sanitize("cfi")))
692 {
693     napi_value argValue[SIZE_TWO] = {0};
694     size_t argCount = SIZE_TWO;
695     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
696     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
697         DEBUG_LOG("argValue check failed");
698         return createUndefine(env);
699     }
700     struct OhosImageSourceProperty key;
701     if (!GetStringValue(env, argValue[ARGS_SECOND], &key.value, &key.size) ||
702         key.value == nullptr || key.size == SIZE_ZERO) {
703         DEBUG_LOG("Get key failed");
704         return createUndefine(env);
705     }
706     struct OhosImageSourceProperty val;
707     int32_t res = OH_ImageSource_GetImageProperty(native, &key, &val);
708     if (val.size == SIZE_ZERO) {
709         DEBUG_LOG("Get val size failed");
710         return createResultValue(env, res, createUndefine(env));
711     }
712     val.value = (char*)malloc(val.size);
713 
714     res = OH_ImageSource_GetImageProperty(native, &key, &val);
715     napi_value nValue = nullptr;
716     if (val.value != nullptr && val.size != SIZE_ZERO) {
717         napi_create_string_utf8(env, val.value, val.size, &nValue);
718     } else {
719         DEBUG_LOG("Get val is empty");
720     }
721     if (val.value != nullptr) {
722         free(val.value);
723     }
724     return createResultValue(env, res, nValue);
725 }
726 
ModifyImageProperty(napi_env env,napi_callback_info info)727 napi_value ImageSourceNDKTest::ModifyImageProperty(napi_env env, napi_callback_info info)
728 {
729     napi_value argValue[SIZE_THREE] = {0};
730     size_t argCount = SIZE_THREE;
731     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
732     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_THREE)) {
733         DEBUG_LOG("argValue check failed");
734         return createUndefine(env);
735     }
736     struct OhosImageSourceProperty key;
737     if (!GetStringValue(env, argValue[ARGS_SECOND], &key.value, &key.size) ||
738         key.value == nullptr || key.size == SIZE_ZERO) {
739         DEBUG_LOG("Get key failed");
740         return createUndefine(env);
741     }
742     struct OhosImageSourceProperty val;
743     if (!GetStringValue(env, argValue[ARGS_THIRD], &val.value, &val.size) ||
744         val.value == nullptr || val.size == SIZE_ZERO) {
745         DEBUG_LOG("Get val failed");
746         return createUndefine(env);
747     }
748     int32_t res = OH_ImageSource_ModifyImageProperty(native, &key, &val);
749     return createResultValue(env, res);
750 }
parseImageSourceUpdateData(napi_env env,napi_value arg,struct OhosImageSourceUpdateData & data)751 static bool parseImageSourceUpdateData(napi_env env, napi_value arg, struct OhosImageSourceUpdateData &data)
752 {
753     if (env == nullptr || arg == nullptr) {
754         DEBUG_LOG("env is %{public}s || arg is %{public}s", DEBUG_PTR(env), DEBUG_PTR(arg));
755         return false;
756     }
757     GetArrayBufferProperty(env, arg, "buffer", &(data.buffer), &(data.bufferSize));
758     GetUint32Property(env, arg, "offset", &(data.offset));
759     GetUint32Property(env, arg, "updateLength", &(data.updateLength));
760     GetBoolProperty(env, arg, "isCompleted", &(data.isCompleted));
761     return true;
762 }
UpdateData(napi_env env,napi_callback_info info)763 napi_value ImageSourceNDKTest::UpdateData(napi_env env, napi_callback_info info)
764 {
765     napi_value argValue[SIZE_TWO] = {0};
766     size_t argCount = SIZE_TWO;
767     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
768     if (native == nullptr || !checkArgs(argValue, argCount, SIZE_TWO)) {
769         DEBUG_LOG("argValue check failed");
770         return createUndefine(env);
771     }
772     struct OhosImageSourceUpdateData data;
773     parseImageSourceUpdateData(env, argValue[ARGS_SECOND], data);
774     int32_t res = OH_ImageSource_UpdateData(native, &data);
775     return createResultValue(env, res);
776 }
777 
Release(napi_env env,napi_callback_info info)778 napi_value ImageSourceNDKTest::Release(napi_env env, napi_callback_info info)
779 {
780     napi_value argValue[SIZE_ONE] = {0};
781     size_t argCount = SIZE_ONE;
782     ImageSourceNative* native = getNativeImageSource(env, info, argValue, argCount);
783     if (native == nullptr) {
784         DEBUG_LOG("argValue check failed");
785         return createUndefine(env);
786     }
787     int32_t res = OH_ImageSource_Release(native);
788     return createResultValue(env, res);
789 }
790 
791 EXTERN_C_START
ModuleRegister(napi_env env,napi_value exports)792 static napi_value ModuleRegister(napi_env env, napi_value exports)
793 {
794     ImageSourceNDKTest::Init(env, exports);
795     return exports;
796 }
797 
798 static napi_module demoModule = {
799     .nm_version =1,
800     .nm_flags = 0,
801     .nm_filename = nullptr,
802     .nm_register_func = ModuleRegister,
803     .nm_modname = "ImageSourceNDKTest",
804     .nm_priv = nullptr,
805     .reserved = { 0 },
806 };
807 
RegisterModule(void)808 __attribute__((constructor)) void RegisterModule(void)
809 {
810     napi_module_register(&demoModule);
811 }
812 EXTERN_C_END
813 }
814 }