• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 <linux/kd.h>
17 #include <string>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <dlfcn.h>
23 
24 #include "napi/native_api.h"
25 #include <multimedia/image_framework/image/pixelmap_native.h>
26 #include <multimedia/image_framework/image/image_packer_native.h>
27 #include <multimedia/image_framework/image/image_source_native.h>
28 
29 #undef LOG_DOMAIN
30 #undef LOG_TAG
31 #define LOG_DOMAIN 0x3200
32 #define LOG_TAG "MY_TAG"
33 
34 #define NUM_0 0
35 #define NUM_1 1
36 #define NUM_2 2
37 #define NUM_3 3
38 #define NUM_4 4
39 #define NUM_5 5
40 #define NUM_6 6
41 #define MAX_BUFFER_SIZE 512
42 #define MAX_COLOR_SIZE 96
43 #define MAX_QUALITY_SIZE 98
44 
45 OH_PixelmapNative *TEST_PIXELMAP = nullptr;
46 const char *LOG_APP = "ImageNDK";
47 const char* VPE_SO_NAME = "/sys_prod/lib64/VideoProcessingEngine/libaihdr_engine.so";
48 
DataCopy(void * dest,int32_t dest_size,const void * src,int32_t n)49 static void DataCopy(void *dest, int32_t dest_size, const void *src, int32_t n) {
50     if (dest == nullptr || src == nullptr) {
51         return;
52     }
53 
54     if (n > dest_size) {
55         return;
56     }
57 
58     char *d = static_cast<char *>(dest);
59     const char *s = static_cast<const char *>(src);
60 
61     while (n--) {
62         *d++ = *s++;
63     }
64 }
65 
OHLog(const char * module,const char * format,...)66 static void OHLog(const char *module, const char *format, ...) {
67 
68     char buffer[MAX_BUFFER_SIZE];
69 
70     va_list args;
71     va_start(args, format);
72     vsnprintf(buffer, sizeof(buffer), format, args);
73     va_end(args);
74 
75     printf("[%s] %s\n", module, buffer);
76 }
77 
78 #define OH_LOG_ERROR(module, format, ...)                                                                              \
79     do {                                                                                                               \
80         OHLog(module, format, ##__VA_ARGS__);                                                                          \
81     } while (false)
82 
83 
84 #define OH_LOG_INFO(module, format, ...)                                                                               \
85     do {                                                                                                               \
86         OHLog(module, format, ##__VA_ARGS__);                                                                          \
87     } while (false)
88 
getJsResult(napi_env env,int result)89 napi_value getJsResult(napi_env env, int result) {
90     napi_value resultNapi = nullptr;
91     napi_create_int32(env, result, &resultNapi);
92     return resultNapi;
93 }
94 
accessInitializationOptions(OH_Pixelmap_InitializationOptions * opts,uint32_t width,uint32_t height,int32_t pixelFormat,int32_t alphaType)95 Image_ErrorCode accessInitializationOptions(OH_Pixelmap_InitializationOptions *opts, uint32_t width, uint32_t height,
96                                             int32_t pixelFormat, int32_t alphaType) {
97     Image_ErrorCode errCode = OH_PixelmapInitializationOptions_SetWidth(opts, width);
98     if (errCode != IMAGE_SUCCESS) {
99         OH_LOG_ERROR(LOG_APP,
100                      "ImagePixelmapNativeCTest accessInitializationOptions OH_PixelmapInitializationOptions_SetWidth "
101                      "failed, errCode: %{public}d.",
102                      errCode);
103         return errCode;
104     }
105     uint32_t widthGet;
106     errCode = OH_PixelmapInitializationOptions_GetWidth(opts, &widthGet);
107     if (errCode != IMAGE_SUCCESS) {
108         OH_LOG_ERROR(LOG_APP,
109                      "ImagePixelmapNativeCTest accessInitializationOptions OH_PixelmapInitializationOptions_GetWidth "
110                      "failed, errCode: %{public}d.",
111                      errCode);
112         return errCode;
113     }
114     errCode = OH_PixelmapInitializationOptions_SetHeight(opts, height);
115     if (errCode != IMAGE_SUCCESS) {
116         OH_LOG_ERROR(LOG_APP,
117                      "ImagePixelmapNativeCTest accessInitializationOptions OH_PixelmapInitializationOptions_SetHeight "
118                      "failed, errCode: %{public}d.",
119                      errCode);
120         return errCode;
121     }
122     uint32_t heightGet;
123     errCode = OH_PixelmapInitializationOptions_GetHeight(opts, &heightGet);
124     if (errCode != IMAGE_SUCCESS) {
125         OH_LOG_ERROR(LOG_APP,
126                      "ImagePixelmapNativeCTest accessInitializationOptions OH_PixelmapInitializationOptions_GetHeight "
127                      "failed, errCode: %{public}d.",
128                      errCode);
129         return errCode;
130     }
131     errCode = OH_PixelmapInitializationOptions_SetPixelFormat(opts, pixelFormat);
132     if (errCode != IMAGE_SUCCESS) {
133         OH_LOG_ERROR(LOG_APP,
134                      "ImagePixelmapNativeCTest accessInitializationOptions "
135                      "OH_PixelmapInitializationOptions_SetPixelFormat failed, errCode: %{public}d.",
136                      errCode);
137         return errCode;
138     }
139     int32_t pixelFormatGet;
140     errCode = OH_PixelmapInitializationOptions_GetPixelFormat(opts, &pixelFormatGet);
141     if (errCode != IMAGE_SUCCESS) {
142         OH_LOG_ERROR(LOG_APP,
143                      "ImagePixelmapNativeCTest accessInitializationOptions "
144                      "OH_PixelmapInitializationOptions_GetPixelFormat failed, errCode: %{public}d.",
145                      errCode);
146         return errCode;
147     }
148     errCode = OH_PixelmapInitializationOptions_SetAlphaType(opts, alphaType);
149     if (errCode != IMAGE_SUCCESS) {
150         OH_LOG_ERROR(LOG_APP,
151                      "ImagePixelmapNativeCTest accessInitializationOptions "
152                      "OH_PixelmapInitializationOptions_SetAlphaType failed, errCode: %{public}d.",
153                      errCode);
154         return errCode;
155     }
156     int32_t alphaTypeGet;
157     errCode = OH_PixelmapInitializationOptions_GetAlphaType(opts, &alphaTypeGet);
158     if (errCode != IMAGE_SUCCESS) {
159         OH_LOG_ERROR(LOG_APP,
160                      "ImagePixelmapNativeCTest accessInitializationOptions "
161                      "OH_PixelmapInitializationOptions_GetAlphaType failed, errCode: %{public}d.",
162                      errCode);
163         return errCode;
164     }
165     return IMAGE_SUCCESS;
166 }
167 
TestInitializationOptions(napi_env env,napi_callback_info info)168 static napi_value TestInitializationOptions(napi_env env, napi_callback_info info) {
169     napi_value argValue[NUM_4] = {0};
170     size_t argCount = NUM_4;
171     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4 ||
172         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr ||
173         argValue[NUM_3] == nullptr) {
174         OH_LOG_ERROR(
175             LOG_APP,
176             "ImagePixelmapNativeCTest TestInitializationOptions napi_get_cb_info failed, argCount: %{public}zu.",
177             argCount);
178         return getJsResult(env, IMAGE_BAD_PARAMETER);
179     }
180     uint32_t width;
181     napi_get_value_uint32(env, argValue[NUM_0], &width);
182     uint32_t height;
183     napi_get_value_uint32(env, argValue[NUM_1], &height);
184     int32_t pixelFormat;
185     napi_get_value_int32(env, argValue[NUM_2], &pixelFormat);
186     int32_t alphaType;
187     napi_get_value_int32(env, argValue[NUM_3], &alphaType);
188     OH_Pixelmap_InitializationOptions *opts = nullptr;
189     Image_ErrorCode errCode = OH_PixelmapInitializationOptions_Create(&opts);
190     if (errCode != IMAGE_SUCCESS) {
191         OH_LOG_ERROR(LOG_APP,
192                      "ImagePixelmapNativeCTest TestInitializationOptions OH_PixelmapInitializationOptions_Create "
193                      "failed, errCode: %{public}d.",
194                      errCode);
195         return getJsResult(env, errCode);
196     }
197     errCode = accessInitializationOptions(opts, width, height, pixelFormat, alphaType);
198     if (errCode != IMAGE_SUCCESS) {
199         return getJsResult(env, errCode);
200     }
201     errCode = OH_PixelmapInitializationOptions_Release(opts);
202     if (errCode != IMAGE_SUCCESS) {
203         OH_LOG_ERROR(LOG_APP,
204                      "ImagePixelmapNativeCTest TestInitializationOptions OH_PixelmapInitializationOptions_Release "
205                      "failed, errCode: %{public}d.",
206                      errCode);
207         return getJsResult(env, errCode);
208     }
209     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestInitializationOptions success.");
210     return getJsResult(env, IMAGE_SUCCESS);
211 }
212 
releaseTestPixelmap()213 Image_ErrorCode releaseTestPixelmap() {
214     Image_ErrorCode errCode = OH_PixelmapNative_Release(TEST_PIXELMAP);
215     if (errCode != IMAGE_SUCCESS) {
216         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest release OH_PixelmapNative_Release failed, errCode: %{public}d.",
217                      errCode);
218         return errCode;
219     }
220     TEST_PIXELMAP = nullptr;
221     return IMAGE_SUCCESS;
222 }
223 
createPixelMap(uint32_t width,uint32_t height,int32_t pixelFormat,int32_t alphaType)224 Image_ErrorCode createPixelMap(uint32_t width, uint32_t height, int32_t pixelFormat, int32_t alphaType) {
225     OH_Pixelmap_InitializationOptions *createOpts;
226     Image_ErrorCode errCode = OH_PixelmapInitializationOptions_Create(&createOpts);
227     if (errCode != IMAGE_SUCCESS) {
228         OH_LOG_ERROR(LOG_APP,
229                      "ImagePixelmapNativeCTest createPixelMap OH_PixelmapInitializationOptions_Create failed, errCode: "
230                      "%{public}d.",
231                      errCode);
232         return errCode;
233     }
234     errCode = OH_PixelmapInitializationOptions_SetWidth(createOpts, width);
235     if (errCode != IMAGE_SUCCESS) {
236         OH_LOG_ERROR(LOG_APP,
237                      "ImagePixelmapNativeCTest createPixelMap OH_PixelmapInitializationOptions_SetWidth failed, "
238                      "errCode: %{public}d.",
239                      errCode);
240         return errCode;
241     }
242     errCode = OH_PixelmapInitializationOptions_SetHeight(createOpts, height);
243     if (errCode != IMAGE_SUCCESS) {
244         OH_LOG_ERROR(LOG_APP,
245                      "ImagePixelmapNativeCTest createPixelMap OH_PixelmapInitializationOptions_SetHeight failed, "
246                      "errCode: %{public}d.",
247                      errCode);
248         return errCode;
249     }
250     errCode = OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, pixelFormat);
251     if (errCode != IMAGE_SUCCESS) {
252         OH_LOG_ERROR(LOG_APP,
253                      "ImagePixelmapNativeCTest createPixelMap OH_PixelmapInitializationOptions_SetPixelFormat failed, "
254                      "errCode: %{public}d.",
255                      errCode);
256         return errCode;
257     }
258     errCode = OH_PixelmapInitializationOptions_SetAlphaType(createOpts, alphaType);
259     if (errCode != IMAGE_SUCCESS) {
260         OH_LOG_ERROR(LOG_APP,
261                      "ImagePixelmapNativeCTest createPixelMap OH_PixelmapInitializationOptions_SetAlphaType failed, "
262                      "errCode: %{public}d.",
263                      errCode);
264         return errCode;
265     }
266     if (TEST_PIXELMAP != nullptr) {
267         errCode = releaseTestPixelmap();
268         if (errCode != IMAGE_SUCCESS) {
269             OH_LOG_ERROR(LOG_APP,
270                          "ImagePixelmapNativeCTest createPixelMap releaseTestPixelmap failed, errCode: %{public}d.",
271                          errCode);
272             return errCode;
273         }
274     }
275     uint8_t colors[MAX_COLOR_SIZE];
276     size_t colorLength = MAX_COLOR_SIZE;
277     for (int i = 0; i < MAX_COLOR_SIZE; i++) {
278         colors[i] = i + 1;
279     }
280     errCode = OH_PixelmapNative_CreatePixelmap(colors, colorLength, createOpts, &TEST_PIXELMAP);
281     if (errCode != IMAGE_SUCCESS) {
282         OH_LOG_ERROR(
283             LOG_APP,
284             "ImagePixelmapNativeCTest createPixelMap OH_PixelmapNative_CreatePixelMap failed, errCode: %{public}d.",
285             errCode);
286         return errCode;
287     }
288     errCode = OH_PixelmapInitializationOptions_Release(createOpts);
289     if (errCode != IMAGE_SUCCESS) {
290         OH_LOG_ERROR(LOG_APP,
291                      "ImagePixelmapNativeCTest createPixelMap OH_PixelmapInitializationOptions_Release failed, "
292                      "errCode: %{public}d.",
293                      errCode);
294         return errCode;
295     }
296     return IMAGE_SUCCESS;
297 }
298 
createPixelMapWithData(uint8_t * data,size_t bufferSize,uint32_t width,uint32_t height,int32_t pixelFormat,int32_t alphaType)299 Image_ErrorCode createPixelMapWithData(uint8_t *data, size_t bufferSize, uint32_t width, uint32_t height,
300                                        int32_t pixelFormat, int32_t alphaType) {
301     OH_Pixelmap_InitializationOptions *createOpts;
302     Image_ErrorCode errCode = OH_PixelmapInitializationOptions_Create(&createOpts);
303     if (errCode != IMAGE_SUCCESS) {
304         OH_LOG_ERROR(LOG_APP,
305                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapInitializationOptions_Create failed, "
306                      "errCode: %{public}d.",
307                      errCode);
308         return errCode;
309     }
310     errCode = OH_PixelmapInitializationOptions_SetWidth(createOpts, width);
311     if (errCode != IMAGE_SUCCESS) {
312         OH_LOG_ERROR(LOG_APP,
313                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapInitializationOptions_SetWidth "
314                      "failed, errCode: %{public}d.",
315                      errCode);
316         return errCode;
317     }
318     errCode = OH_PixelmapInitializationOptions_SetHeight(createOpts, height);
319     if (errCode != IMAGE_SUCCESS) {
320         OH_LOG_ERROR(LOG_APP,
321                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapInitializationOptions_SetHeight "
322                      "failed, errCode: %{public}d.",
323                      errCode);
324         return errCode;
325     }
326     errCode = OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, pixelFormat);
327     if (errCode != IMAGE_SUCCESS) {
328         OH_LOG_ERROR(LOG_APP,
329                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapInitializationOptions_SetPixelFormat "
330                      "failed, errCode: %{public}d.",
331                      errCode);
332         return errCode;
333     }
334     errCode = OH_PixelmapInitializationOptions_SetAlphaType(createOpts, alphaType);
335     if (errCode != IMAGE_SUCCESS) {
336         OH_LOG_ERROR(LOG_APP,
337                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapInitializationOptions_SetAlphaType "
338                      "failed, errCode: %{public}d.",
339                      errCode);
340         return errCode;
341     }
342     if (TEST_PIXELMAP != nullptr) {
343         errCode = releaseTestPixelmap();
344         if (errCode != IMAGE_SUCCESS) {
345             OH_LOG_ERROR(
346                 LOG_APP,
347                 "ImagePixelmapNativeCTest createPixelMapWithData releaseTestPixelmap failed, errCode: %{public}d.",
348                 errCode);
349             return errCode;
350         }
351     }
352     errCode = OH_PixelmapNative_CreatePixelmap(data, bufferSize, createOpts, &TEST_PIXELMAP);
353     if (errCode != IMAGE_SUCCESS) {
354         OH_LOG_ERROR(LOG_APP,
355                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapNative_CreatePixelMap failed, "
356                      "errCode: %{public}d.",
357                      errCode);
358         return errCode;
359     }
360     errCode = OH_PixelmapInitializationOptions_Release(createOpts);
361     if (errCode != IMAGE_SUCCESS) {
362         OH_LOG_ERROR(LOG_APP,
363                      "ImagePixelmapNativeCTest createPixelMapWithData OH_PixelmapInitializationOptions_Release failed, "
364                      "errCode: %{public}d.",
365                      errCode);
366         return errCode;
367     }
368     return IMAGE_SUCCESS;
369 }
370 
TestSavePixelmap(napi_env env,napi_callback_info info)371 static napi_value TestSavePixelmap(napi_env env, napi_callback_info info) {
372     napi_value argValue[NUM_1] = {0};
373     size_t argCount = NUM_1;
374     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
375         argValue[NUM_0] == nullptr) {
376         OH_LOG_ERROR(LOG_APP,
377                      "ImagePixelmapNativeCTest TestSavePixelmap napi_get_cb_info failed, argCount: %{public}zu.",
378                      argCount);
379         return getJsResult(env, IMAGE_BAD_PARAMETER);
380     }
381     char name[MAX_BUFFER_SIZE];
382     size_t nameSize = MAX_BUFFER_SIZE;
383     napi_get_value_string_utf8(env, argValue[NUM_0], name, MAX_BUFFER_SIZE, &nameSize);
384     int fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0666);
385     if (fd == -1) {
386         OH_LOG_ERROR(LOG_APP,
387                      "ImagePixelmapNativeCTest TestSavePixelmap open failed, errno: %{public}d, name: %{public}s.",
388                      errno, name);
389         return getJsResult(env, -1);
390     }
391     fd = dup(fd);
392     OH_PackingOptions *opts = nullptr;
393     OH_PackingOptions_Create(&opts);
394     Image_MimeType mimeType;
395     mimeType.data = (char *)MIME_TYPE_JPEG;
396     mimeType.size = strlen(MIME_TYPE_JPEG);
397     OH_PackingOptions_SetMimeType(opts, &mimeType);
398     OH_PackingOptions_SetQuality(opts, MAX_QUALITY_SIZE);
399     OH_ImagePackerNative *packer = nullptr;
400     OH_ImagePackerNative_Create(&packer);
401     Image_ErrorCode errCode = OH_ImagePackerNative_PackToFileFromPixelmap(packer, opts, TEST_PIXELMAP, fd);
402     close(fd);
403     OH_PackingOptions_Release(opts);
404     OH_ImagePackerNative_Release(packer);
405     if (errCode != IMAGE_SUCCESS) {
406         OH_LOG_ERROR(LOG_APP,
407                      "ImagePixelmapNativeCTest TestSavePixelmap OH_ImagePackerNative_PackToFileFromPixelmap failed, "
408                      "errCode: %{public}d.",
409                      errCode);
410         return getJsResult(env, errCode);
411     }
412     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestSavePixelmap success, name: %{public}s.", name);
413     return getJsResult(env, IMAGE_SUCCESS);
414 }
415 
TestCreatePixelmap(napi_env env,napi_callback_info info)416 static napi_value TestCreatePixelmap(napi_env env, napi_callback_info info) {
417     napi_value argValue[NUM_4] = {0};
418     size_t argCount = NUM_4;
419     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4 ||
420         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr ||
421         argValue[NUM_3] == nullptr) {
422         OH_LOG_ERROR(LOG_APP,
423                      "ImagePixelmapNativeCTest TestCreatePixelmap napi_get_cb_info failed, argCount: %{public}zu.",
424                      argCount);
425         return getJsResult(env, IMAGE_BAD_PARAMETER);
426     }
427     uint32_t width;
428     napi_get_value_uint32(env, argValue[NUM_0], &width);
429     uint32_t height;
430     napi_get_value_uint32(env, argValue[NUM_1], &height);
431     int32_t pixelFormat;
432     napi_get_value_int32(env, argValue[NUM_2], &pixelFormat);
433     int32_t alphaType;
434     napi_get_value_int32(env, argValue[NUM_3], &alphaType);
435     Image_ErrorCode errCode = createPixelMap(width, height, pixelFormat, alphaType);
436     if (errCode != IMAGE_SUCCESS) {
437         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestCreatePixelmap createPixelMap failed.");
438         return getJsResult(env, errCode);
439     }
440     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestCreatePixelmap success.");
441     return getJsResult(env, IMAGE_SUCCESS);
442 }
443 
TestCreatePixelmapWithData(napi_env env,napi_callback_info info)444 static napi_value TestCreatePixelmapWithData(napi_env env, napi_callback_info info) {
445     napi_value argValue[NUM_5] = {0};
446     size_t argCount = NUM_5;
447     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_5 ||
448         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr ||
449         argValue[NUM_3] == nullptr || argValue[NUM_4] == nullptr) {
450         OH_LOG_ERROR(
451             LOG_APP,
452             "ImagePixelmapNativeCTest TestCreatePixelmapWithData napi_get_cb_info failed, argCount: %{public}zu.",
453             argCount);
454         return getJsResult(env, IMAGE_BAD_PARAMETER);
455     }
456     uint8_t *pixelmapData = nullptr;
457     size_t pixelmapDataSize = 0;
458     napi_get_arraybuffer_info(env, argValue[NUM_0], (void **)&pixelmapData, &pixelmapDataSize);
459     uint32_t width;
460     napi_get_value_uint32(env, argValue[NUM_1], &width);
461     uint32_t height;
462     napi_get_value_uint32(env, argValue[NUM_2], &height);
463     int32_t pixelFormat;
464     napi_get_value_int32(env, argValue[NUM_3], &pixelFormat);
465     int32_t alphaType;
466     napi_get_value_int32(env, argValue[NUM_4], &alphaType);
467     Image_ErrorCode errCode =
468         createPixelMapWithData(pixelmapData, pixelmapDataSize, width, height, pixelFormat, alphaType);
469     if (errCode != IMAGE_SUCCESS) {
470         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestCreatePixelmapWithData createPixelMapWithData failed.");
471         return getJsResult(env, errCode);
472     }
473     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestCreatePixelmapWithData success.");
474     return getJsResult(env, IMAGE_SUCCESS);
475 }
476 
TestReadPixels(napi_env env,napi_callback_info info)477 static napi_value TestReadPixels(napi_env env, napi_callback_info info) {
478     napi_value argValue[NUM_1] = {0};
479     size_t argCount = NUM_1;
480     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
481         argValue[NUM_0] == nullptr) {
482         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestReadPixels napi_get_cb_info failed, argCount: %{public}zu.",
483                      argCount);
484         return getJsResult(env, IMAGE_BAD_PARAMETER);
485     }
486     uint8_t *destination;
487     size_t bufferSize;
488     napi_get_arraybuffer_info(env, argValue[NUM_0], (void **)&destination, &bufferSize);
489     Image_ErrorCode errCode = OH_PixelmapNative_ReadPixels(TEST_PIXELMAP, destination, &bufferSize);
490     if (errCode != IMAGE_SUCCESS) {
491         OH_LOG_ERROR(
492             LOG_APP,
493             "ImagePixelmapNativeCTest TestReadPixels OH_PixelmapNative_ReadPixels failed, errCode: %{public}d.",
494             errCode);
495         return getJsResult(env, errCode);
496     }
497     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestReadPixels success.");
498     return getJsResult(env, IMAGE_SUCCESS);
499 }
500 
TestWritePixels(napi_env env,napi_callback_info info)501 static napi_value TestWritePixels(napi_env env, napi_callback_info info) {
502     napi_value argValue[NUM_1] = {0};
503     size_t argCount = NUM_1;
504     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
505         argValue[NUM_0] == nullptr) {
506         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestWritePixels napi_get_cb_info failed, argCount: %{public}d.",
507                      argCount);
508         return getJsResult(env, IMAGE_BAD_PARAMETER);
509     }
510     uint8_t *source;
511     size_t bufferSize;
512     napi_get_arraybuffer_info(env, argValue[NUM_0], (void **)&source, &bufferSize);
513     Image_ErrorCode errCode = OH_PixelmapNative_WritePixels(TEST_PIXELMAP, source, bufferSize);
514     if (errCode != IMAGE_SUCCESS) {
515         OH_LOG_ERROR(
516             LOG_APP,
517             "ImagePixelmapNativeCTest TestWritePixels OH_PixelmapNative_WritePixels failed, errCode: %{public}d.",
518             errCode);
519         return getJsResult(env, errCode);
520     }
521     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestWritePixels success.");
522     return getJsResult(env, IMAGE_SUCCESS);
523 }
524 
525 struct ImageInfo {
526     uint32_t width;
527     uint32_t height;
528     uint32_t rowStride;
529     int32_t pixelFormat;
530     int32_t alphaType;
531 };
buildJsImageInfo(napi_env env,ImageInfo imageInfo)532 napi_value buildJsImageInfo(napi_env env, ImageInfo imageInfo) {
533     napi_value object = nullptr;
534     napi_create_object(env, &object);
535     if (object == nullptr) {
536         return nullptr;
537     }
538     napi_value alphaType = nullptr;
539     napi_create_int32(env, imageInfo.alphaType, &alphaType);
540     napi_set_named_property(env, object, "alphaType", alphaType);
541     napi_value pixelFormat = nullptr;
542     napi_create_int32(env, imageInfo.pixelFormat, &pixelFormat);
543     napi_set_named_property(env, object, "pixelFormat", pixelFormat);
544     napi_value rowStride = nullptr;
545     napi_create_uint32(env, imageInfo.rowStride, &rowStride);
546     napi_set_named_property(env, object, "rowStride", rowStride);
547     napi_value width = nullptr;
548     napi_create_uint32(env, imageInfo.width, &width);
549     napi_set_named_property(env, object, "width", width);
550     napi_value height = nullptr;
551     napi_create_uint32(env, imageInfo.height, &height);
552     napi_set_named_property(env, object, "height", height);
553     return object;
554 }
555 
TestGetImageInfo(napi_env env,napi_callback_info info)556 static napi_value TestGetImageInfo(napi_env env, napi_callback_info info) {
557     OH_Pixelmap_ImageInfo *imageInfo;
558     Image_ErrorCode errCode = OH_PixelmapImageInfo_Create(&imageInfo);
559     if (errCode != IMAGE_SUCCESS) {
560         OH_LOG_ERROR(
561             LOG_APP,
562             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_Create failed, errCode: %{public}d.",
563             errCode);
564         return nullptr;
565     }
566     errCode = OH_PixelmapNative_GetImageInfo(TEST_PIXELMAP, imageInfo);
567     if (errCode != IMAGE_SUCCESS) {
568         OH_LOG_ERROR(
569             LOG_APP,
570             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapNative_GetImageInfo failed, errCode: %{public}d.",
571             errCode);
572         return nullptr;
573     }
574     ImageInfo imageInfoJs;
575     errCode = OH_PixelmapImageInfo_GetWidth(imageInfo, &(imageInfoJs.width));
576     if (errCode != IMAGE_SUCCESS) {
577         OH_LOG_ERROR(
578             LOG_APP,
579             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_GetWidth failed, errCode: %{public}d.",
580             errCode);
581         return nullptr;
582     }
583     errCode = OH_PixelmapImageInfo_GetHeight(imageInfo, &(imageInfoJs.height));
584     if (errCode != IMAGE_SUCCESS) {
585         OH_LOG_ERROR(
586             LOG_APP,
587             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_GetHeight failed, errCode: %{public}d.",
588             errCode);
589         return nullptr;
590     }
591     errCode = OH_PixelmapImageInfo_GetRowStride(imageInfo, &(imageInfoJs.rowStride));
592     if (errCode != IMAGE_SUCCESS) {
593         OH_LOG_ERROR(
594             LOG_APP,
595             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_GetRowStride failed, errCode: %{public}d.",
596             errCode);
597         return nullptr;
598     }
599     errCode = OH_PixelmapImageInfo_GetPixelFormat(imageInfo, &(imageInfoJs.pixelFormat));
600     if (errCode != IMAGE_SUCCESS) {
601         OH_LOG_ERROR(LOG_APP,
602                      "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_GetPixelFormat failed, errCode: "
603                      "%{public}d.",
604                      errCode);
605         return nullptr;
606     }
607     errCode = OH_PixelmapImageInfo_GetAlphaType(imageInfo, &(imageInfoJs.alphaType));
608     if (errCode != IMAGE_SUCCESS) {
609         OH_LOG_ERROR(
610             LOG_APP,
611             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_GetAlphaType failed, errCode: %{public}d.",
612             errCode);
613         return nullptr;
614     }
615     errCode = OH_PixelmapImageInfo_Release(imageInfo);
616     if (errCode != IMAGE_SUCCESS) {
617         OH_LOG_ERROR(
618             LOG_APP,
619             "ImagePixelmapNativeCTest TestGetImageInfo OH_PixelmapImageInfo_Release failed, errCode: %{public}d.",
620             errCode);
621         return nullptr;
622     }
623     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestGetImageInfo success.");
624     return buildJsImageInfo(env, imageInfoJs);
625 }
626 
TestOpacity(napi_env env,napi_callback_info info)627 static napi_value TestOpacity(napi_env env, napi_callback_info info) {
628     napi_value argValue[NUM_1] = {0};
629     size_t argCount = NUM_1;
630     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
631         argValue[NUM_0] == nullptr) {
632         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestOpacity napi_get_cb_info failed, argCount: %{public}zu.",
633                      argCount);
634         return getJsResult(env, IMAGE_BAD_PARAMETER);
635     }
636     double rate;
637     napi_get_value_double(env, argValue[NUM_0], &rate);
638     Image_ErrorCode errCode = OH_PixelmapNative_Opacity(TEST_PIXELMAP, (float)rate);
639     if (errCode != IMAGE_SUCCESS) {
640         OH_LOG_ERROR(LOG_APP,
641                      "ImagePixelmapNativeCTest TestOpacity OH_PixelmapNative_Opacity failed, errCode: %{public}d.",
642                      errCode);
643         return getJsResult(env, errCode);
644     }
645     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestOpacity success.");
646     return getJsResult(env, IMAGE_SUCCESS);
647 }
648 
TestScale(napi_env env,napi_callback_info info)649 static napi_value TestScale(napi_env env, napi_callback_info info) {
650     napi_value argValue[NUM_2] = {0};
651     size_t argCount = NUM_2;
652     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2 ||
653         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
654         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestScale napi_get_cb_info failed, argCount: %{public}zu.",
655                      argCount);
656         return getJsResult(env, IMAGE_BAD_PARAMETER);
657     }
658     double x;
659     double y;
660     napi_get_value_double(env, argValue[NUM_0], &x);
661     napi_get_value_double(env, argValue[NUM_1], &y);
662     Image_ErrorCode errCode = OH_PixelmapNative_Scale(TEST_PIXELMAP, (float)x, (float)y);
663     if (errCode != IMAGE_SUCCESS) {
664         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestScale OH_PixelmapNative_Scale failed, errCode: %{public}d.",
665                      errCode);
666         return getJsResult(env, errCode);
667     }
668     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestScale success.");
669     return getJsResult(env, IMAGE_SUCCESS);
670 }
671 
TestTranslate(napi_env env,napi_callback_info info)672 static napi_value TestTranslate(napi_env env, napi_callback_info info) {
673     napi_value argValue[NUM_2] = {0};
674     size_t argCount = NUM_2;
675     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2 ||
676         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
677         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestTranslate napi_get_cb_info failed, argCount: %{public}zu.",
678                      argCount);
679         return getJsResult(env, IMAGE_BAD_PARAMETER);
680     }
681     double x, y;
682     napi_get_value_double(env, argValue[NUM_0], &x);
683     napi_get_value_double(env, argValue[NUM_1], &y);
684     Image_ErrorCode errCode = OH_PixelmapNative_Translate(TEST_PIXELMAP, (float)x, (float)y);
685     if (errCode != IMAGE_SUCCESS) {
686         OH_LOG_ERROR(LOG_APP,
687                      "ImagePixelmapNativeCTest TestTranslate OH_PixelmapNative_Translate failed, errCode: %{public}d.",
688                      errCode);
689         return getJsResult(env, errCode);
690     }
691     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestTranslate success.");
692     return getJsResult(env, IMAGE_SUCCESS);
693 }
694 
TestRotate(napi_env env,napi_callback_info info)695 static napi_value TestRotate(napi_env env, napi_callback_info info) {
696     napi_value argValue[NUM_1] = {0};
697     size_t argCount = NUM_1;
698     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
699         argValue[NUM_0] == nullptr) {
700         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestRotate napi_get_cb_info failed, argCount: %{public}zu.",
701                      argCount);
702         return getJsResult(env, IMAGE_BAD_PARAMETER);
703     }
704     double angle;
705     napi_get_value_double(env, argValue[NUM_0], &angle);
706     Image_ErrorCode errCode = OH_PixelmapNative_Rotate(TEST_PIXELMAP, (float)angle);
707     if (errCode != IMAGE_SUCCESS) {
708         OH_LOG_ERROR(LOG_APP,
709                      "ImagePixelmapNativeCTest TestRotate OH_PixelmapNative_Rotate failed, errCode: %{public}d.",
710                      errCode);
711         return getJsResult(env, errCode);
712     }
713     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestRotate success.");
714     return getJsResult(env, IMAGE_SUCCESS);
715 }
716 
TestFlip(napi_env env,napi_callback_info info)717 static napi_value TestFlip(napi_env env, napi_callback_info info) {
718     napi_value argValue[NUM_2] = {0};
719     size_t argCount = NUM_2;
720     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2 ||
721         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
722         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestFlip napi_get_cb_info failed, argCount: %{public}zu.",
723                      argCount);
724         return getJsResult(env, IMAGE_BAD_PARAMETER);
725     }
726     bool horizontal, vertical;
727     napi_get_value_bool(env, argValue[NUM_0], &horizontal);
728     napi_get_value_bool(env, argValue[NUM_1], &vertical);
729     Image_ErrorCode errCode = OH_PixelmapNative_Flip(TEST_PIXELMAP, horizontal, vertical);
730     if (errCode != IMAGE_SUCCESS) {
731         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestFlip OH_PixelmapNative_Flip failed, errCode: %{public}d.",
732                      errCode);
733         return getJsResult(env, errCode);
734     }
735     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestFlip success.");
736     return getJsResult(env, IMAGE_SUCCESS);
737 }
738 
TestCrop(napi_env env,napi_callback_info info)739 static napi_value TestCrop(napi_env env, napi_callback_info info) {
740     napi_value argValue[NUM_4] = {0};
741     size_t argCount = NUM_4;
742     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4 ||
743         argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr ||
744         argValue[NUM_3] == nullptr) {
745         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestCrop napi_get_cb_info failed, argCount: %{public}zu.",
746                      argCount);
747         return getJsResult(env, IMAGE_BAD_PARAMETER);
748     }
749     Image_Region region;
750     napi_get_value_uint32(env, argValue[NUM_0], &region.x);
751     napi_get_value_uint32(env, argValue[NUM_1], &region.y);
752     napi_get_value_uint32(env, argValue[NUM_2], &region.width);
753     napi_get_value_uint32(env, argValue[NUM_3], &region.height);
754     Image_ErrorCode errCode = OH_PixelmapNative_Crop(TEST_PIXELMAP, &region);
755     if (errCode != IMAGE_SUCCESS) {
756         OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest TestCrop OH_PixelmapNative_Crop failed, errCode: %{public}d.",
757                      errCode);
758         return getJsResult(env, errCode);
759     }
760     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestCrop success.");
761     return getJsResult(env, IMAGE_SUCCESS);
762 }
763 
TestReleasePixelmap(napi_env env,napi_callback_info info)764 static napi_value TestReleasePixelmap(napi_env env, napi_callback_info info) {
765     Image_ErrorCode errCode = releaseTestPixelmap();
766     if (errCode != IMAGE_SUCCESS) {
767         OH_LOG_ERROR(LOG_APP,
768                      "ImagePixelmapNativeCTest TestReleasePixelmap releaseTestPixelmap failed, errCode: %{public}d.",
769                      errCode);
770         return getJsResult(env, errCode);
771     }
772     OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest TestReleasePixelmap success.");
773     return getJsResult(env, IMAGE_SUCCESS);
774 }
775 
setInt32NamedProperty(napi_env env,napi_value object,const char * utf8name,uint32_t value)776 static void setInt32NamedProperty(napi_env env, napi_value object, const char *utf8name, uint32_t value) {
777     napi_value tmp;
778     napi_create_int32(env, value, &tmp);
779     napi_set_named_property(env, object, utf8name, tmp);
780 }
781 
CreateImageInfo(napi_env env,napi_callback_info info)782 static napi_value CreateImageInfo(napi_env env, napi_callback_info info) {
783     napi_value result = nullptr;
784 
785     napi_get_undefined(env, &result);
786 
787     OH_ImageSource_Info *imageInfo;
788     Image_ErrorCode errCode = OH_ImageSourceInfo_Create(&imageInfo);
789     if (IMAGE_SUCCESS != errCode) {
790         napi_create_int32(env, errCode, &result);
791         return result;
792     }
793 
794     napi_status status = napi_create_external(env, reinterpret_cast<void *>(imageInfo), nullptr, nullptr, &result);
795     if (status != napi_ok) {
796         napi_throw_error(env, nullptr, "Failed to create external object");
797         return nullptr;
798     }
799     return result;
800 }
801 
ImageInfoGetWidth(napi_env env,napi_callback_info info)802 static napi_value ImageInfoGetWidth(napi_env env, napi_callback_info info) {
803     napi_value result = nullptr;
804 
805     napi_get_undefined(env, &result);
806 
807     napi_value argValue[NUM_1] = {0};
808     size_t argCount = NUM_1;
809 
810 
811     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
812         return result;
813     }
814 
815     void *ptr = nullptr;
816     napi_get_value_external(env, argValue[NUM_0], &ptr);
817 
818     OH_ImageSource_Info *imageInfo = reinterpret_cast<OH_ImageSource_Info *>(ptr);
819 
820     uint32_t width = 0;
821     Image_ErrorCode errCode = OH_ImageSourceInfo_GetWidth(imageInfo, &width);
822     if (IMAGE_SUCCESS != errCode) {
823         napi_create_int32(env, errCode, &result);
824         return result;
825     }
826     napi_create_uint32(env, width, &result);
827 
828     return result;
829 }
830 
ImageInfoGetHeight(napi_env env,napi_callback_info info)831 static napi_value ImageInfoGetHeight(napi_env env, napi_callback_info info) {
832     napi_value result = nullptr;
833 
834     napi_get_undefined(env, &result);
835 
836     napi_value argValue[NUM_1] = {0};
837     size_t argCount = NUM_1;
838 
839 
840     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
841         return result;
842     }
843 
844     void *ptr = nullptr;
845     napi_get_value_external(env, argValue[NUM_0], &ptr);
846 
847     OH_ImageSource_Info *imageInfo = reinterpret_cast<OH_ImageSource_Info *>(ptr);
848 
849     uint32_t height = 0;
850     Image_ErrorCode errCode = OH_ImageSourceInfo_GetHeight(imageInfo, &height);
851     if (IMAGE_SUCCESS != errCode) {
852         napi_create_int32(env, errCode, &result);
853         return result;
854     }
855     napi_create_uint32(env, height, &result);
856 
857     return result;
858 }
859 
ReleaseImageInfo(napi_env env,napi_callback_info info)860 static napi_value ReleaseImageInfo(napi_env env, napi_callback_info info) {
861     napi_value result = nullptr;
862 
863 
864     napi_get_undefined(env, &result);
865 
866     napi_value argValue[NUM_1] = {0};
867     size_t argCount = NUM_1;
868 
869 
870     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
871         return result;
872     }
873 
874     void *ptr = nullptr;
875     napi_get_value_external(env, argValue[NUM_0], &ptr);
876 
877     OH_ImageSource_Info *imageInfo = reinterpret_cast<OH_ImageSource_Info *>(ptr);
878 
879     Image_ErrorCode errCode = OH_ImageSourceInfo_Release(imageInfo);
880 
881     napi_create_int32(env, errCode, &result);
882 
883     return result;
884 }
885 
CreateDecodingOptions(napi_env env,napi_callback_info info)886 static napi_value CreateDecodingOptions(napi_env env, napi_callback_info info) {
887     napi_value result = nullptr;
888 
889 
890     napi_get_undefined(env, &result);
891 
892     OH_DecodingOptions *deops;
893     Image_ErrorCode errCode = OH_DecodingOptions_Create(&deops);
894     if (IMAGE_SUCCESS != errCode) {
895         napi_create_int32(env, errCode, &result);
896         return result;
897     }
898 
899     napi_status status = napi_create_external(env, reinterpret_cast<void *>(deops), nullptr, nullptr, &result);
900     if (status != napi_ok) {
901         napi_throw_error(env, nullptr, "Failed to create external object");
902         return nullptr;
903     }
904     return result;
905 }
906 
DecodingOptionsGetPixelFormat(napi_env env,napi_callback_info info)907 static napi_value DecodingOptionsGetPixelFormat(napi_env env, napi_callback_info info) {
908     napi_value result = nullptr;
909 
910     napi_get_undefined(env, &result);
911 
912     napi_value argValue[NUM_1] = {0};
913     size_t argCount = NUM_1;
914 
915     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
916         return result;
917     }
918 
919     void *ptr = nullptr;
920     napi_get_value_external(env, argValue[NUM_0], &ptr);
921 
922     OH_DecodingOptions *depos = reinterpret_cast<OH_DecodingOptions *>(ptr);
923     int32_t pixelFormat = 0;
924     Image_ErrorCode errCode = OH_DecodingOptions_GetPixelFormat(depos, &pixelFormat);
925     if (IMAGE_SUCCESS != errCode) {
926         napi_create_int32(env, errCode, &result);
927         return result;
928     }
929     napi_create_int32(env, pixelFormat, &result);
930 
931     return result;
932 }
933 
DecodingOptionsSetPixelFormat(napi_env env,napi_callback_info info)934 static napi_value DecodingOptionsSetPixelFormat(napi_env env, napi_callback_info info) {
935     napi_value result = nullptr;
936 
937 
938     napi_get_undefined(env, &result);
939 
940     napi_value argValue[NUM_2] = {0};
941     size_t argCount = NUM_2;
942 
943 
944     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
945         return result;
946     }
947 
948     void *ptr = nullptr;
949     napi_get_value_external(env, argValue[NUM_0], &ptr);
950 
951     uint32_t pixelFormat = 0;
952     napi_get_value_uint32(env, argValue[NUM_1], &pixelFormat);
953 
954     OH_DecodingOptions *depos = reinterpret_cast<OH_DecodingOptions *>(ptr);
955 
956     Image_ErrorCode errCode = OH_DecodingOptions_SetPixelFormat(depos, pixelFormat);
957 
958     napi_create_int32(env, errCode, &result);
959 
960     return result;
961 }
962 
DecodingOptionsGetIndex(napi_env env,napi_callback_info info)963 static napi_value DecodingOptionsGetIndex(napi_env env, napi_callback_info info) {
964     napi_value result = nullptr;
965 
966 
967     napi_get_undefined(env, &result);
968 
969     napi_value argValue[NUM_1] = {0};
970     size_t argCount = NUM_1;
971 
972 
973     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
974         return result;
975     }
976 
977     void *ptr = nullptr;
978     napi_get_value_external(env, argValue[NUM_0], &ptr);
979 
980     OH_DecodingOptions *depos = reinterpret_cast<OH_DecodingOptions *>(ptr);
981     uint32_t index = 0;
982     Image_ErrorCode errCode = OH_DecodingOptions_GetIndex(depos, &index);
983     if (IMAGE_SUCCESS != errCode) {
984         napi_create_int32(env, errCode, &result);
985         return result;
986     }
987     napi_create_int32(env, index, &result);
988 
989     return result;
990 }
991 
DecodingOptionsSetIndex(napi_env env,napi_callback_info info)992 static napi_value DecodingOptionsSetIndex(napi_env env, napi_callback_info info) {
993     napi_value result = nullptr;
994 
995 
996     napi_get_undefined(env, &result);
997 
998     napi_value argValue[NUM_2] = {0};
999     size_t argCount = NUM_2;
1000 
1001     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1002         return result;
1003     }
1004 
1005     void *ptr = nullptr;
1006     napi_get_value_external(env, argValue[NUM_0], &ptr);
1007 
1008     uint32_t index = 0;
1009     napi_get_value_uint32(env, argValue[NUM_1], &index);
1010 
1011     OH_DecodingOptions *depos = reinterpret_cast<OH_DecodingOptions *>(ptr);
1012 
1013     Image_ErrorCode errCode = OH_DecodingOptions_SetIndex(depos, index);
1014 
1015     napi_create_int32(env, errCode, &result);
1016 
1017     return result;
1018 }
1019 
DecodingOptionsGetRotate(napi_env env,napi_callback_info info)1020 static napi_value DecodingOptionsGetRotate(napi_env env, napi_callback_info info) {
1021     napi_value result = nullptr;
1022     napi_value argValue[NUM_1] = {0};
1023     size_t argCount = NUM_1;
1024 
1025     napi_get_undefined(env, &result);
1026 
1027     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1028         return result;
1029     }
1030 
1031     void *ptr = nullptr;
1032     napi_get_value_external(env, argValue[NUM_0], &ptr);
1033     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1034 
1035     float rotate;
1036     Image_ErrorCode ret = OH_DecodingOptions_GetRotate(decodeOpts, &rotate);
1037     if (IMAGE_SUCCESS != ret) {
1038         napi_create_int32(env, ret, &result);
1039 
1040         return result;
1041     }
1042     napi_create_double(env, static_cast<double>(rotate), &result);
1043     return result;
1044 }
1045 
DecodingOptionsSetRotate(napi_env env,napi_callback_info info)1046 static napi_value DecodingOptionsSetRotate(napi_env env, napi_callback_info info) {
1047     napi_value result = nullptr;
1048     napi_value argValue[NUM_2] = {0};
1049     size_t argCount = NUM_2;
1050 
1051     napi_get_undefined(env, &result);
1052 
1053     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1054         return result;
1055     }
1056 
1057     void *ptr = nullptr;
1058     napi_get_value_external(env, argValue[NUM_0], &ptr);
1059     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1060 
1061     double rotate;
1062     napi_get_value_double(env, argValue[NUM_1], &rotate);
1063 
1064 
1065     Image_ErrorCode ret = OH_DecodingOptions_SetRotate(decodeOpts, static_cast<float>(rotate));
1066     napi_create_int32(env, ret, &result);
1067     return result;
1068 }
1069 
DecodingOptionsGetDesiredSize(napi_env env,napi_callback_info info)1070 static napi_value DecodingOptionsGetDesiredSize(napi_env env, napi_callback_info info) {
1071     napi_value result = nullptr;
1072     napi_value argValue[NUM_1] = {0};
1073     size_t argCount = NUM_1;
1074 
1075     napi_get_undefined(env, &result);
1076 
1077     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1078         return result;
1079     }
1080 
1081     void *ptr = nullptr;
1082     napi_get_value_external(env, argValue[NUM_0], &ptr);
1083     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1084 
1085     Image_Size image_Size;
1086 
1087     Image_ErrorCode ret = OH_DecodingOptions_GetDesiredSize(decodeOpts, &image_Size);
1088     if (ret != IMAGE_SUCCESS) {
1089         napi_create_int32(env, ret, &result);
1090         return result;
1091     }
1092     napi_create_object(env, &result);
1093 
1094     setInt32NamedProperty(env, result, "width", image_Size.width);
1095     setInt32NamedProperty(env, result, "height", image_Size.height);
1096 
1097     return result;
1098 }
1099 
DecodingOptionsSetDesiredSize(napi_env env,napi_callback_info info)1100 static napi_value DecodingOptionsSetDesiredSize(napi_env env, napi_callback_info info) {
1101     napi_value result = nullptr;
1102     napi_value argValue[NUM_3] = {0};
1103     size_t argCount = NUM_3;
1104 
1105     napi_get_undefined(env, &result);
1106 
1107     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_3) {
1108         return result;
1109     }
1110 
1111     void *ptr = nullptr;
1112     napi_get_value_external(env, argValue[NUM_0], &ptr);
1113     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1114 
1115     Image_Size imageSize;
1116     napi_get_value_uint32(env, argValue[NUM_1], &imageSize.width);
1117     napi_get_value_uint32(env, argValue[NUM_2], &imageSize.height);
1118 
1119     Image_ErrorCode ret = OH_DecodingOptions_SetDesiredSize(decodeOpts, &imageSize);
1120     if (ret != IMAGE_SUCCESS) {
1121         napi_create_int32(env, ret, &result);
1122         return result;
1123     }
1124 
1125     napi_create_object(env, &result);
1126 
1127     setInt32NamedProperty(env, result, "width", imageSize.width);
1128     setInt32NamedProperty(env, result, "height", imageSize.height);
1129 
1130     return result;
1131 }
1132 
DecodingOptionsGetDesiredRegion(napi_env env,napi_callback_info info)1133 static napi_value DecodingOptionsGetDesiredRegion(napi_env env, napi_callback_info info) {
1134     napi_value result = nullptr;
1135     napi_value argValue[NUM_1] = {0};
1136     size_t argCount = NUM_1;
1137 
1138     napi_get_undefined(env, &result);
1139 
1140     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1141         return result;
1142     }
1143 
1144     void *ptr = nullptr;
1145     napi_get_value_external(env, argValue[NUM_0], &ptr);
1146     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1147 
1148     Image_Region image_region;
1149 
1150     Image_ErrorCode ret = OH_DecodingOptions_GetDesiredRegion(decodeOpts, &image_region);
1151     if (ret != IMAGE_SUCCESS) {
1152         napi_create_int32(env, ret, &result);
1153         return result;
1154     }
1155 
1156     napi_create_object(env, &result);
1157 
1158     setInt32NamedProperty(env, result, "x", image_region.x);
1159     setInt32NamedProperty(env, result, "y", image_region.y);
1160     setInt32NamedProperty(env, result, "width", image_region.width);
1161     setInt32NamedProperty(env, result, "height", image_region.height);
1162 
1163     return result;
1164 }
1165 
DecodingOptionsSetDesiredRegion(napi_env env,napi_callback_info info)1166 static napi_value DecodingOptionsSetDesiredRegion(napi_env env, napi_callback_info info) {
1167     napi_value result = nullptr;
1168     napi_value argValue[NUM_5] = {0};
1169     size_t argCount = NUM_5;
1170 
1171     napi_get_undefined(env, &result);
1172 
1173     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_5) {
1174         return result;
1175     }
1176 
1177     void *ptr = nullptr;
1178     napi_get_value_external(env, argValue[NUM_0], &ptr);
1179     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1180 
1181     Image_Region image_Region;
1182     napi_get_value_uint32(env, argValue[NUM_1], &image_Region.x);
1183     napi_get_value_uint32(env, argValue[NUM_2], &image_Region.y);
1184     napi_get_value_uint32(env, argValue[NUM_3], &image_Region.width);
1185     napi_get_value_uint32(env, argValue[NUM_4], &image_Region.height);
1186 
1187     Image_ErrorCode ret = OH_DecodingOptions_SetDesiredRegion(decodeOpts, &image_Region);
1188     napi_create_int32(env, ret, &result);
1189     return result;
1190 }
1191 
ReleaseDecodingOptions(napi_env env,napi_callback_info info)1192 static napi_value ReleaseDecodingOptions(napi_env env, napi_callback_info info) {
1193     napi_value result = nullptr;
1194     napi_value thisVar = nullptr;
1195     napi_value argValue[NUM_1] = {0};
1196     size_t argCount = NUM_1;
1197 
1198     napi_get_undefined(env, &result);
1199 
1200     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < NUM_1) {
1201         return result;
1202     }
1203     void *ptr = nullptr;
1204     napi_get_value_external(env, argValue[NUM_0], &ptr);
1205     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1206 
1207     Image_ErrorCode ret = OH_DecodingOptions_Release(decodeOpts);
1208 
1209     napi_create_int32(env, ret, &result);
1210     return result;
1211 }
1212 
CreateFromUri(napi_env env,napi_callback_info info)1213 static napi_value CreateFromUri(napi_env env, napi_callback_info info) {
1214     napi_value result = nullptr;
1215     napi_value argValue[NUM_1] = {0};
1216     size_t argCount = NUM_1;
1217 
1218     napi_get_undefined(env, &result);
1219 
1220     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1221         return result;
1222     }
1223 
1224     const size_t maxUrlLen = MAX_BUFFER_SIZE;
1225     char url[maxUrlLen];
1226     size_t urlSize = 0;
1227     napi_get_value_string_utf8(env, argValue[NUM_0], url, maxUrlLen, &urlSize);
1228 
1229     OH_ImageSourceNative *res = nullptr;
1230     Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromUri(url, urlSize, &res);
1231     if (errCode != IMAGE_SUCCESS) {
1232         napi_create_int32(env, errCode, &result);
1233         return result;
1234     }
1235 
1236     napi_status status = napi_create_external(env, reinterpret_cast<void *>(res), nullptr, nullptr, &result);
1237     if (status != napi_ok) {
1238         napi_throw_error(env, nullptr, "Failed to create external object");
1239         return nullptr;
1240     }
1241     return result;
1242 }
1243 
CreateFromFd(napi_env env,napi_callback_info info)1244 static napi_value CreateFromFd(napi_env env, napi_callback_info info) {
1245     napi_value result = nullptr;
1246 
1247     napi_value argValue[NUM_1] = {0};
1248     size_t argCount = NUM_1;
1249 
1250     napi_get_undefined(env, &result);
1251 
1252     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1253         return result;
1254     }
1255     int32_t fd;
1256     napi_get_value_int32(env, argValue[NUM_0], &fd);
1257     if (fd < 0) {
1258         napi_create_int32(env, -1, &result);
1259         return result;
1260     }
1261 
1262 
1263     OH_ImageSourceNative *res = nullptr;
1264     Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromFd(fd, &res);
1265     if (errCode != IMAGE_SUCCESS) {
1266         napi_create_int32(env, errCode, &result);
1267         return result;
1268     }
1269 
1270     napi_status status = napi_create_external(env, reinterpret_cast<void *>(res), nullptr, nullptr, &result);
1271     if (status != napi_ok) {
1272         napi_throw_error(env, nullptr, "Failed to create external object");
1273         return nullptr;
1274     }
1275 
1276     return result;
1277 }
1278 
CreateFromData(napi_env env,napi_callback_info info)1279 static napi_value CreateFromData(napi_env env, napi_callback_info info) {
1280     napi_value result = nullptr;
1281 
1282     napi_value argValue[NUM_1] = {0};
1283     size_t argCount = NUM_1;
1284 
1285     napi_get_undefined(env, &result);
1286 
1287     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1288         return result;
1289     }
1290 
1291     void *data = nullptr;
1292     size_t dataSize = 0;
1293 
1294     napi_status status = napi_get_buffer_info(env, argValue[NUM_0], &data, &dataSize);
1295     if (status != napi_ok) {
1296         return result;
1297     }
1298 
1299     OH_LOG_INFO(LOG_APP, "end OH_ImageSource2_CreatePixelMapList 721aaaa:%{public}zu", dataSize);
1300     OH_ImageSourceNative *res = nullptr;
1301     Image_ErrorCode error = OH_ImageSourceNative_CreateFromData(reinterpret_cast<uint8_t *>(data), dataSize, &res);
1302     if (error != IMAGE_SUCCESS) {
1303         napi_throw_error(env, nullptr, "Failed to OH_ImageSource2_CreateFromData create external object,");
1304         return result;
1305     }
1306 
1307     status = napi_create_external(env, reinterpret_cast<void *>(res), nullptr, nullptr, &result);
1308     if (status != napi_ok) {
1309         napi_throw_error(env, nullptr, "Failed to OH_ImageSource2_CreateFromData create external object");
1310         return result;
1311     }
1312 
1313     return result;
1314 }
1315 
CreatePixelMap(napi_env env,napi_callback_info info)1316 static napi_value CreatePixelMap(napi_env env, napi_callback_info info) {
1317 
1318     napi_value result = nullptr;
1319     napi_value argValue[NUM_2] = {0};
1320     size_t argCount = NUM_2;
1321 
1322     napi_get_undefined(env, &result);
1323 
1324     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok) {
1325         return result;
1326     }
1327 
1328     void *ptr = nullptr;
1329     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
1330     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1331 
1332     OH_DecodingOptions *decodeOpts = nullptr;
1333     if (2 == argCount) {
1334         status = napi_get_value_external(env, argValue[NUM_1], &ptr);
1335         OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1336     }
1337 
1338     OH_PixelmapNative *resPixMap = nullptr;
1339     Image_ErrorCode errCode = OH_ImageSourceNative_CreatePixelmap(imageSource, decodeOpts, &resPixMap);
1340     if (IMAGE_SUCCESS != errCode) {
1341         napi_create_int32(env, errCode, &result);
1342         return result;
1343     }
1344 
1345     if (TEST_PIXELMAP != nullptr) {
1346         errCode = releaseTestPixelmap();
1347         if (errCode != IMAGE_SUCCESS) {
1348             OH_LOG_ERROR(LOG_APP,
1349                          "ImagePixelmapNativeCTest createPixelMap releaseTestPixelmap failed, errCode: %{public}d.",
1350                          errCode);
1351             return result;
1352         }
1353     }
1354     TEST_PIXELMAP = resPixMap;
1355 
1356     status = napi_create_external(env, reinterpret_cast<void *>(resPixMap), nullptr, nullptr, &result);
1357     if (status != napi_ok) {
1358         napi_throw_error(env, nullptr, "Failed to create external object");
1359         return result;
1360     }
1361     return result;
1362 }
1363 
CreatePixelMapList(napi_env env,napi_callback_info info)1364 static napi_value CreatePixelMapList(napi_env env, napi_callback_info info) {
1365     napi_value result = nullptr;
1366     napi_value argValue[NUM_2] = {0};
1367     size_t argCount = NUM_2;
1368 
1369     napi_get_undefined(env, &result);
1370 
1371     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1372         return result;
1373     }
1374 
1375     void *ptr = nullptr;
1376     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
1377     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1378 
1379     status = napi_get_value_external(env, argValue[NUM_1], &ptr);
1380     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
1381 
1382     uint32_t frameCnt = 0;
1383     Image_ErrorCode errCode = OH_ImageSourceNative_GetFrameCount(imageSource, &frameCnt);
1384     if (errCode != IMAGE_SUCCESS) {
1385         napi_create_int32(env, errCode, &result);
1386         return result;
1387     }
1388     if (frameCnt <= 0) {
1389         return result;
1390     }
1391 
1392     OH_PixelmapNative **resPixMapList = new (std::nothrow) OH_PixelmapNative *[frameCnt];
1393 
1394     size_t arrayLength = frameCnt;
1395 
1396     errCode = OH_ImageSourceNative_CreatePixelmapList(imageSource, decodeOpts, resPixMapList, arrayLength);
1397     OH_LOG_INFO(LOG_APP, "end OH_ImageSource2_CreatePixelMapList errCode:%{public}d", errCode);
1398 
1399     if (errCode != IMAGE_SUCCESS) {
1400         delete[] resPixMapList;
1401         return result;
1402     }
1403 
1404     status = napi_create_array_with_length(env, arrayLength, &result);
1405     for (size_t i = 0; i < arrayLength; i++) {
1406         napi_value externalValue;
1407         status = napi_create_external(env, resPixMapList[i], nullptr, nullptr, &externalValue);
1408 
1409         status = napi_set_element(env, result, i, externalValue);
1410         if (status != napi_ok) {
1411             return result;
1412         }
1413     }
1414 
1415     delete[] resPixMapList;
1416     return result;
1417 }
1418 
GetDelayTime(napi_env env,napi_callback_info info)1419 static napi_value GetDelayTime(napi_env env, napi_callback_info info) {
1420     napi_value result = nullptr;
1421     napi_value argValue[NUM_1] = {0};
1422     size_t argCount = NUM_1;
1423 
1424     napi_get_undefined(env, &result);
1425 
1426     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1427         return result;
1428     }
1429 
1430     void *ptr = nullptr;
1431     napi_get_value_external(env, argValue[NUM_0], &ptr);
1432     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1433 
1434     uint32_t frameCnt = 0;
1435     Image_ErrorCode errCode = OH_ImageSourceNative_GetFrameCount(imageSource, &frameCnt);
1436     if (errCode != IMAGE_SUCCESS) {
1437         napi_create_int32(env, errCode, &result);
1438         return result;
1439     }
1440     if (frameCnt <= 0) {
1441         return result;
1442     }
1443 
1444     int32_t *delayTimeList = new (std::nothrow) int32_t[frameCnt];
1445 
1446     size_t size = frameCnt;
1447     errCode = OH_ImageSourceNative_GetDelayTimeList(imageSource, delayTimeList, size);
1448     if (errCode != IMAGE_SUCCESS) {
1449         napi_create_int32(env, errCode, &result);
1450         delete[] delayTimeList;
1451         return result;
1452     }
1453     if (size >= 0) {
1454         napi_create_array_with_length(env, size, &result);
1455         for (size_t i = 0; i < size; i++) {
1456             napi_value externalValue;
1457             napi_create_external(env, &delayTimeList[i], nullptr, nullptr, &externalValue);
1458             napi_set_element(env, result, i, externalValue);
1459         }
1460     }
1461     delete[] delayTimeList;
1462     return result;
1463 }
1464 
GetImageInfo(napi_env env,napi_callback_info info)1465 static napi_value GetImageInfo(napi_env env, napi_callback_info info) {
1466     napi_value result = nullptr;
1467     napi_value argValue[NUM_2] = {0};
1468     size_t argCount = NUM_2;
1469 
1470     napi_get_undefined(env, &result);
1471 
1472     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1473         return result;
1474     }
1475 
1476     void *ptr = nullptr;
1477     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
1478     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1479 
1480     int32_t index = 0;
1481     status = napi_get_value_int32(env, argValue[NUM_1], &index);
1482 
1483 
1484     OH_ImageSource_Info *imageInfo = nullptr;
1485     OH_ImageSourceInfo_Create(&imageInfo);
1486 
1487     Image_ErrorCode errCode = OH_ImageSourceNative_GetImageInfo(imageSource, index, imageInfo);
1488     if (errCode != IMAGE_SUCCESS) {
1489         napi_create_int32(env, errCode, &result);
1490         return result;
1491     }
1492 
1493     status = napi_create_external(env, reinterpret_cast<void *>(imageInfo), nullptr, nullptr, &result);
1494     if (status != napi_ok) {
1495         napi_throw_error(env, nullptr, "Failed to create external object");
1496         return nullptr;
1497     }
1498     return result;
1499 }
1500 
GetImageProperty(napi_env env,napi_callback_info info)1501 static napi_value GetImageProperty(napi_env env, napi_callback_info info) {
1502     napi_value result = nullptr;
1503     napi_value argValue[NUM_2] = {0};
1504     size_t argCount = NUM_2;
1505 
1506     napi_get_undefined(env, &result);
1507 
1508     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1509         return result;
1510     }
1511 
1512     void *ptr = nullptr;
1513     napi_get_value_external(env, argValue[NUM_0], &ptr);
1514     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1515 
1516 
1517     const size_t maxKeyLen = MAX_BUFFER_SIZE;
1518     char key[maxKeyLen];
1519     size_t keySize = 0;
1520     napi_get_value_string_utf8(env, argValue[NUM_1], key, maxKeyLen, &keySize);
1521 
1522 
1523     Image_String imageKey, imageValue = {nullptr, 0};
1524     imageKey.data = key;
1525     imageKey.size = keySize;
1526 
1527     Image_ErrorCode errCode = OH_ImageSourceNative_GetImageProperty(imageSource, &imageKey, &imageValue);
1528     if (errCode != IMAGE_SUCCESS) {
1529         napi_create_int32(env, errCode, &result);
1530         return result;
1531     }
1532 
1533     napi_create_string_utf8(env, (const char *)imageValue.data, imageValue.size, &result);
1534     return result;
1535 }
1536 
1537 
ModifyImageProperty(napi_env env,napi_callback_info info)1538 static napi_value ModifyImageProperty(napi_env env, napi_callback_info info) {
1539     napi_value result = nullptr;
1540     napi_value argValue[NUM_3] = {0};
1541     size_t argCount = NUM_3;
1542 
1543     napi_get_undefined(env, &result);
1544 
1545     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_3) {
1546         return result;
1547     }
1548 
1549     void *ptr = nullptr;
1550     napi_get_value_external(env, argValue[NUM_0], &ptr);
1551     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1552 
1553     const size_t keyLen = MAX_BUFFER_SIZE;
1554     char *key = static_cast<char *>(malloc(keyLen));
1555     size_t keySize = 0;
1556     napi_get_value_string_utf8(env, argValue[NUM_1], key, keyLen, &keySize);
1557 
1558     const size_t maxValLen = MAX_BUFFER_SIZE;
1559     char *val = static_cast<char *>(malloc(maxValLen));
1560 
1561     size_t valSize = 0;
1562     napi_get_value_string_utf8(env, argValue[NUM_2], val, maxValLen, &valSize);
1563 
1564     Image_String imageKey, imageVal;
1565     imageKey.data = key;
1566     imageKey.size = keySize;
1567     imageVal.data = val;
1568     imageVal.size = valSize;
1569 
1570     OH_LOG_INFO(LOG_APP, "end OH_ImageSource2_CreatePixelMapList 1115:%{public}s,%{public}zu,%{public}s,%{public}zu",
1571                 key, keySize, val, valSize);
1572     Image_ErrorCode errCode = OH_ImageSourceNative_ModifyImageProperty(imageSource, &imageKey, &imageVal);
1573     napi_create_int32(env, errCode, &result);
1574     free(key);
1575     free(val);
1576     return result;
1577 }
1578 
1579 
GetFrameCount(napi_env env,napi_callback_info info)1580 static napi_value GetFrameCount(napi_env env, napi_callback_info info) {
1581     napi_value result = nullptr;
1582     napi_value argValue[NUM_1] = {0};
1583     size_t argCount = NUM_1;
1584 
1585     napi_get_undefined(env, &result);
1586 
1587     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1588         return result;
1589     }
1590 
1591     void *ptr = nullptr;
1592     napi_get_value_external(env, argValue[NUM_0], &ptr);
1593     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1594 
1595     uint32_t frameCnt = 0;
1596     Image_ErrorCode errCode = OH_ImageSourceNative_GetFrameCount(imageSource, &frameCnt);
1597 
1598     napi_create_object(env, &result);
1599     setInt32NamedProperty(env, result, "frameCnt", frameCnt);
1600     setInt32NamedProperty(env, result, "errCode", errCode);
1601 
1602     return result;
1603 }
1604 
SourceRelease(napi_env env,napi_callback_info info)1605 static napi_value SourceRelease(napi_env env, napi_callback_info info) {
1606     napi_value result = nullptr;
1607     napi_value argValue[NUM_1] = {0};
1608     size_t argCount = NUM_1;
1609 
1610     napi_get_undefined(env, &result);
1611 
1612     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1613         return result;
1614     }
1615 
1616     void *ptr = nullptr;
1617     napi_get_value_external(env, argValue[NUM_0], &ptr);
1618     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1619 
1620     Image_ErrorCode errCode = OH_ImageSourceNative_Release(imageSource);
1621     napi_create_int32(env, errCode, &result);
1622 
1623     return result;
1624 }
1625 
CreatePackingOptions(napi_env env,napi_callback_info info)1626 static napi_value CreatePackingOptions(napi_env env, napi_callback_info info) {
1627     napi_value result = nullptr;
1628 
1629     napi_get_undefined(env, &result);
1630 
1631     OH_PackingOptions *res = nullptr;
1632     Image_ErrorCode errorCode = OH_PackingOptions_Create(&res);
1633     if (errorCode != IMAGE_SUCCESS) {
1634         napi_create_int32(env, errorCode, &result);
1635         return result;
1636     }
1637 
1638     napi_status status = napi_create_external(env, reinterpret_cast<void *>(res), nullptr, nullptr, &result);
1639     if (status != napi_ok) {
1640         napi_throw_error(env, nullptr, "Failed to create external object");
1641         return nullptr;
1642     }
1643     return result;
1644 }
1645 
PackingOptionsGetMimeType(napi_env env,napi_callback_info info)1646 static napi_value PackingOptionsGetMimeType(napi_env env, napi_callback_info info) {
1647     napi_value result = nullptr;
1648 
1649     napi_value argValue[NUM_1] = {0};
1650     size_t argCount = NUM_1;
1651 
1652     napi_get_undefined(env, &result);
1653 
1654     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1655         return result;
1656     }
1657 
1658     void *ptr = nullptr;
1659     napi_get_value_external(env, argValue[NUM_0], &ptr);
1660     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
1661 
1662     char mineTypeBuf[MAX_BUFFER_SIZE];
1663     Image_MimeType mineType;
1664     mineType.data = mineTypeBuf;
1665 
1666     Image_ErrorCode errorCode = OH_PackingOptions_GetMimeType(packingOptions, &mineType);
1667     if (errorCode != IMAGE_SUCCESS) {
1668         napi_create_int32(env, errorCode, &result);
1669         return result;
1670     }
1671     napi_create_string_utf8(env, mineType.data, mineType.size, &result);
1672 
1673     return result;
1674 }
1675 
PackingOptionsSetMimeType(napi_env env,napi_callback_info info)1676 static napi_value PackingOptionsSetMimeType(napi_env env, napi_callback_info info) {
1677     napi_value result = nullptr;
1678     napi_value argValue[NUM_2] = {0};
1679     size_t argCount = NUM_2;
1680 
1681     napi_get_undefined(env, &result);
1682 
1683     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1684         return result;
1685     }
1686 
1687     void *ptr = nullptr;
1688     napi_get_value_external(env, argValue[NUM_0], &ptr);
1689     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
1690 
1691     const uint32_t maxMineTypeLen = MAX_BUFFER_SIZE;
1692     char mineType[maxMineTypeLen];
1693     size_t mineTypeSize = 0;
1694 
1695     napi_get_value_string_utf8(env, argValue[NUM_1], mineType, maxMineTypeLen, &mineTypeSize);
1696 
1697     Image_MimeType tmpMineType;
1698     tmpMineType.data = new (std::nothrow) char[MAX_BUFFER_SIZE];
1699     DataCopy(tmpMineType.data, MAX_BUFFER_SIZE, mineType, mineTypeSize);
1700     tmpMineType.data[mineTypeSize] = '\0';
1701 
1702     tmpMineType.size = mineTypeSize;
1703     Image_ErrorCode errCode = OH_PackingOptions_SetMimeType(packingOptions, &tmpMineType);
1704 
1705     napi_create_int32(env, errCode, &result);
1706     return result;
1707 }
1708 
PackingOptionsGetQuality(napi_env env,napi_callback_info info)1709 static napi_value PackingOptionsGetQuality(napi_env env, napi_callback_info info) {
1710     napi_value result = nullptr;
1711 
1712     napi_value argValue[NUM_1] = {0};
1713     size_t argCount = NUM_1;
1714 
1715     napi_get_undefined(env, &result);
1716 
1717     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1718         return result;
1719     }
1720 
1721     void *ptr = nullptr;
1722     napi_get_value_external(env, argValue[NUM_0], &ptr);
1723     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
1724 
1725     uint32_t quality;
1726     Image_ErrorCode errCode = OH_PackingOptions_GetQuality(packingOptions, &quality);
1727     if (errCode != IMAGE_SUCCESS) {
1728         napi_create_int32(env, errCode, &result);
1729         return result;
1730     }
1731     napi_create_int32(env, quality, &result);
1732 
1733     return result;
1734 }
1735 
PackingOptionsSetQuality(napi_env env,napi_callback_info info)1736 static napi_value PackingOptionsSetQuality(napi_env env, napi_callback_info info) {
1737     napi_value result = nullptr;
1738 
1739     napi_value argValue[NUM_2] = {0};
1740     size_t argCount = NUM_2;
1741 
1742     napi_get_undefined(env, &result);
1743 
1744     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
1745         return result;
1746     }
1747 
1748     void *ptr = nullptr;
1749     napi_get_value_external(env, argValue[NUM_0], &ptr);
1750     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
1751 
1752     uint32_t quality;
1753     napi_get_value_uint32(env, argValue[NUM_1], &quality);
1754 
1755     Image_ErrorCode errCode = OH_PackingOptions_SetQuality(packingOptions, quality);
1756     napi_create_int32(env, errCode, &result);
1757     return result;
1758 }
1759 
ReleasePackingOptions(napi_env env,napi_callback_info info)1760 static napi_value ReleasePackingOptions(napi_env env, napi_callback_info info) {
1761     napi_value result = nullptr;
1762 
1763     napi_value argValue[NUM_1] = {0};
1764     size_t argCount = NUM_1;
1765 
1766     napi_get_undefined(env, &result);
1767 
1768     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1769         return result;
1770     }
1771 
1772     void *ptr = nullptr;
1773     napi_get_value_external(env, argValue[NUM_0], &ptr);
1774     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
1775 
1776     Image_ErrorCode errCode = OH_PackingOptions_Release(packingOptions);
1777 
1778     napi_create_int32(env, errCode, &result);
1779     return result;
1780 }
1781 
PackerCreate(napi_env env,napi_callback_info info)1782 static napi_value PackerCreate(napi_env env, napi_callback_info info) {
1783     napi_value result = nullptr;
1784     napi_get_undefined(env, &result);
1785 
1786     OH_ImagePackerNative *res = nullptr;
1787     Image_ErrorCode errCode = OH_ImagePackerNative_Create(&res);
1788     if (errCode != IMAGE_SUCCESS) {
1789         napi_create_int32(env, errCode, &result);
1790         return result;
1791     }
1792 
1793     napi_status status = napi_create_external(env, reinterpret_cast<void *>(res), nullptr, nullptr, &result);
1794     if (status != napi_ok) {
1795         napi_throw_error(env, nullptr, "Failed to create external object");
1796         return nullptr;
1797     }
1798 
1799     return result;
1800 }
1801 
PackToDataFromImageSource(napi_env env,napi_callback_info info)1802 static napi_value PackToDataFromImageSource(napi_env env, napi_callback_info info) {
1803     napi_value result = nullptr;
1804 
1805     napi_value argValue[NUM_4] = {0};
1806     size_t argCount = NUM_4;
1807 
1808     napi_get_undefined(env, &result);
1809 
1810     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4) {
1811         return result;
1812     }
1813 
1814     void *ptr = nullptr;
1815     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
1816     OH_ImagePackerNative *packer = reinterpret_cast<OH_ImagePackerNative *>(ptr);
1817 
1818     ptr = nullptr;
1819     status = napi_get_value_external(env, argValue[NUM_1], &ptr);
1820     OH_PackingOptions *opts = reinterpret_cast<OH_PackingOptions *>(ptr);
1821 
1822     ptr = nullptr;
1823     status = napi_get_value_external(env, argValue[NUM_2], &ptr);
1824     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1825 
1826     uint8_t *outBuffer = nullptr;
1827     size_t outBufferSize = 0;
1828 
1829     status = napi_get_buffer_info(env, argValue[NUM_3], (void **)&outBuffer, &outBufferSize);
1830     if (status != napi_ok) {
1831         napi_throw_error(env, nullptr, "Failed to create napi_get_buffer_info object");
1832         return result;
1833     }
1834 
1835     Image_ErrorCode errCode =
1836         OH_ImagePackerNative_PackToDataFromImageSource(packer, opts, imageSource, outBuffer, &outBufferSize);
1837     if (IMAGE_SUCCESS != errCode) {
1838         napi_create_int32(env, errCode, &result);
1839         return result;
1840     }
1841     napi_create_int32(env, outBufferSize, &result);
1842 
1843     return result;
1844 }
1845 
PackToDataFromPixelMap(napi_env env,napi_callback_info info)1846 static napi_value PackToDataFromPixelMap(napi_env env, napi_callback_info info) {
1847     napi_value result = nullptr;
1848 
1849     napi_value argValue[NUM_4] = {0};
1850     size_t argCount = NUM_4;
1851 
1852     napi_get_undefined(env, &result);
1853 
1854     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4) {
1855         return result;
1856     }
1857 
1858     void *ptr = nullptr;
1859     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
1860     OH_ImagePackerNative *packer = reinterpret_cast<OH_ImagePackerNative *>(ptr);
1861 
1862     ptr = nullptr;
1863     status = napi_get_value_external(env, argValue[NUM_1], &ptr);
1864     OH_PackingOptions *opts = reinterpret_cast<OH_PackingOptions *>(ptr);
1865 
1866     ptr = nullptr;
1867     status = napi_get_value_external(env, argValue[NUM_2], &ptr);
1868     OH_PixelmapNative *pixMap = reinterpret_cast<OH_PixelmapNative *>(ptr);
1869 
1870     Image_MimeType mineType;
1871     OH_PackingOptions_GetMimeType(opts, &mineType);
1872 
1873     uint8_t *outBuffer = nullptr;
1874     size_t outBufferSize = 0;
1875 
1876     status = napi_get_buffer_info(env, argValue[NUM_3], (void **)&outBuffer, &outBufferSize);
1877     if (status != napi_ok) {
1878         napi_throw_error(env, nullptr, "Failed to create napi_get_buffer_info object");
1879         return result;
1880     }
1881 
1882     Image_ErrorCode errCode =
1883         OH_ImagePackerNative_PackToDataFromPixelmap(packer, opts, pixMap, outBuffer, &outBufferSize);
1884     if (IMAGE_SUCCESS != errCode) {
1885         napi_create_int32(env, errCode, &result);
1886         return result;
1887     }
1888 
1889     napi_create_int32(env, outBufferSize, &result);
1890 
1891     return result;
1892 }
1893 
PackToFileFromImageSource(napi_env env,napi_callback_info info)1894 static napi_value PackToFileFromImageSource(napi_env env, napi_callback_info info) {
1895     napi_value result = nullptr;
1896 
1897     napi_value argValue[NUM_4] = {0};
1898     size_t argCount = NUM_4;
1899 
1900     napi_get_undefined(env, &result);
1901 
1902     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4) {
1903         return result;
1904     }
1905 
1906     void *ptr = nullptr;
1907     napi_get_value_external(env, argValue[NUM_0], &ptr);
1908     OH_ImagePackerNative *packer = reinterpret_cast<OH_ImagePackerNative *>(ptr);
1909 
1910     ptr = nullptr;
1911     napi_get_value_external(env, argValue[NUM_1], &ptr);
1912     OH_PackingOptions *opts = reinterpret_cast<OH_PackingOptions *>(ptr);
1913 
1914     ptr = nullptr;
1915     napi_get_value_external(env, argValue[NUM_2], &ptr);
1916     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
1917 
1918     int fd;
1919     napi_get_value_int32(env, argValue[NUM_3], &fd);
1920 
1921     Image_ErrorCode errCode = OH_ImagePackerNative_PackToFileFromImageSource(packer, opts, imageSource, fd);
1922 
1923     napi_create_int32(env, errCode, &result);
1924     return result;
1925 }
1926 
PackToFileFromPixelMap(napi_env env,napi_callback_info info)1927 static napi_value PackToFileFromPixelMap(napi_env env, napi_callback_info info) {
1928     napi_value result = nullptr;
1929 
1930     napi_value argValue[NUM_4] = {0};
1931     size_t argCount = NUM_4;
1932 
1933     napi_get_undefined(env, &result);
1934 
1935     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_4) {
1936         return result;
1937     }
1938 
1939     void *ptr = nullptr;
1940     napi_get_value_external(env, argValue[NUM_0], &ptr);
1941     OH_ImagePackerNative *packer = reinterpret_cast<OH_ImagePackerNative *>(ptr);
1942 
1943     ptr = nullptr;
1944     napi_get_value_external(env, argValue[NUM_1], &ptr);
1945     OH_PackingOptions *opts = reinterpret_cast<OH_PackingOptions *>(ptr);
1946 
1947     ptr = nullptr;
1948     napi_get_value_external(env, argValue[NUM_2], &ptr);
1949     OH_PixelmapNative *pixmap = reinterpret_cast<OH_PixelmapNative *>(ptr);
1950 
1951     int fd;
1952     napi_get_value_int32(env, argValue[NUM_3], &fd);
1953 
1954     Image_ErrorCode errCode = OH_ImagePackerNative_PackToFileFromPixelmap(packer, opts, pixmap, fd);
1955 
1956     napi_create_int32(env, errCode, &result);
1957     return result;
1958 }
1959 
PackerRelease(napi_env env,napi_callback_info info)1960 static napi_value PackerRelease(napi_env env, napi_callback_info info) {
1961     napi_value result = nullptr;
1962 
1963     napi_value argValue[NUM_1] = {0};
1964     size_t argCount = NUM_1;
1965 
1966     napi_get_undefined(env, &result);
1967 
1968     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1969         return result;
1970     }
1971 
1972     void *ptr = nullptr;
1973     napi_get_value_external(env, argValue[NUM_0], &ptr);
1974     OH_ImagePackerNative *packer = reinterpret_cast<OH_ImagePackerNative *>(ptr);
1975     Image_ErrorCode errCode = OH_ImagePackerNative_Release(packer);
1976     napi_create_int32(env, errCode, &result);
1977     return result;
1978 }
1979 
PixelMapRelease(napi_env env,napi_callback_info info)1980 static napi_value PixelMapRelease(napi_env env, napi_callback_info info) {
1981     napi_value result = nullptr;
1982     napi_value argValue[NUM_1] = {0};
1983     size_t argCount = NUM_1;
1984 
1985     napi_get_undefined(env, &result);
1986 
1987     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
1988         return result;
1989     }
1990 
1991     void *ptr = nullptr;
1992     napi_get_value_external(env, argValue[NUM_0], &ptr);
1993     OH_PixelmapNative *pixelMap = reinterpret_cast<OH_PixelmapNative *>(ptr);
1994 
1995     Image_ErrorCode errCode = OH_PixelmapNative_Release(pixelMap);
1996     napi_create_int32(env, errCode, &result);
1997 
1998     return result;
1999 }
2000 
CreateFromRawFile(napi_env env,napi_callback_info info)2001 static napi_value CreateFromRawFile(napi_env env, napi_callback_info info) {
2002     napi_value result = nullptr;
2003     napi_value thisVar = nullptr;
2004     napi_value argValue[NUM_3] = {0};
2005     size_t argCount = NUM_3;
2006     napi_status status;
2007 
2008     napi_get_undefined(env, &result);
2009 
2010     OH_LOG_INFO(LOG_APP, "lhb CreateFromRawFile errCode====");
2011 
2012     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_3) {
2013         return result;
2014     }
2015 
2016     RawFileDescriptor rawDesc;
2017     napi_get_value_int32(env, argValue[NUM_0], &rawDesc.fd);
2018     if (rawDesc.fd < 0) {
2019         napi_create_int32(env, -1, &result);
2020         return result;
2021     }
2022     int64_t tmp;
2023     napi_get_value_int64(env, argValue[NUM_1], &tmp);
2024     rawDesc.start = static_cast<long>(tmp);
2025     napi_get_value_int64(env, argValue[NUM_2], &tmp);
2026     rawDesc.length = static_cast<long>(tmp);
2027 
2028     OH_LOG_INFO(LOG_APP, "lhb CreateFromRawFile fd:%{public}d, start:%{public}ld, length:%{public}ld", rawDesc.fd,
2029                 rawDesc.start, rawDesc.length);
2030 
2031     OH_ImageSourceNative *res = nullptr;
2032     Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromRawFile(&rawDesc, &res);
2033     if (errCode != IMAGE_SUCCESS) {
2034         napi_throw_error(env, nullptr, "Failed to OH_ImageSource2_CreateFromData create external object,");
2035         napi_create_int32(env, errCode, &result);
2036         return result;
2037     }
2038     status = napi_create_external(env, reinterpret_cast<void *>(res), nullptr, nullptr, &result);
2039     if (status != napi_ok) {
2040         napi_throw_error(env, nullptr, "Failed to create external object");
2041         return nullptr;
2042     }
2043     return result;
2044 }
2045 
CheckVpe()2046 static bool CheckVpe()
2047 {
2048     void* handle = dlopen(VPE_SO_NAME, RTLD_LAZY);
2049     if (handle == nullptr) {
2050         OH_LOG_INFO(LOG_APP, "CheckVpe failed");
2051         return false;
2052     }
2053     dlclose(handle);
2054     handle = nullptr;
2055     return true;
2056 }
2057 
CheckHasHdr(napi_env env,napi_callback_info info)2058 static napi_value CheckHasHdr(napi_env env, napi_callback_info info)
2059 {
2060     napi_value result = nullptr;
2061     napi_get_undefined(env, &result);
2062     if (CheckVpe()) {
2063         napi_create_int32(env, NUM_0, &result);
2064     } else {
2065         napi_create_int32(env, NUM_1, &result);
2066     }
2067     return result;
2068 }
2069 
DecodingOptionsSetDesiredDynamicRange(napi_env env,napi_callback_info info)2070 static napi_value DecodingOptionsSetDesiredDynamicRange(napi_env env, napi_callback_info info)
2071 {
2072     napi_value result = nullptr;
2073     napi_value argValue[NUM_2] = {0};
2074     size_t argCount = NUM_2;
2075 
2076     napi_get_undefined(env, &result);
2077 
2078     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
2079         OH_LOG_ERROR(LOG_APP, "DecodingOptionsSetDesiredDynamicRange napi_get_cb_info failed");
2080         return result;
2081     }
2082 
2083     void *ptr = nullptr;
2084     napi_get_value_external(env, argValue[NUM_0], &ptr);
2085     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
2086 
2087     int32_t desiredDynamicRange;
2088     napi_get_value_int32(env, argValue[NUM_1], &desiredDynamicRange);
2089 
2090     Image_ErrorCode ret = OH_DecodingOptions_SetDesiredDynamicRange(decodeOpts, desiredDynamicRange);
2091     napi_create_int32(env, ret, &result);
2092     return result;
2093 }
2094 
DecodingOptionsGetDesiredDynamicRange(napi_env env,napi_callback_info info)2095 static napi_value DecodingOptionsGetDesiredDynamicRange(napi_env env, napi_callback_info info)
2096 {
2097     napi_value result = nullptr;
2098     napi_value argValue[NUM_1] = {0};
2099     size_t argCount = NUM_1;
2100 
2101     napi_get_undefined(env, &result);
2102 
2103     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
2104         OH_LOG_ERROR(LOG_APP, "DecodingOptionsGetDesiredDynamicRange napi_get_cb_info failed");
2105         return result;
2106     }
2107 
2108     void *ptr = nullptr;
2109     napi_get_value_external(env, argValue[NUM_0], &ptr);
2110     OH_DecodingOptions *decodeOpts = reinterpret_cast<OH_DecodingOptions *>(ptr);
2111 
2112     int32_t desiredDynamicRange;
2113     Image_ErrorCode ret = OH_DecodingOptions_GetDesiredDynamicRange(decodeOpts, &desiredDynamicRange);
2114     if (ret != IMAGE_SUCCESS) {
2115         OH_LOG_ERROR(LOG_APP, "DecodingOptionsGetDesiredDynamicRange failed");
2116         napi_create_int32(env, ret, &result);
2117         return result;
2118     }
2119     napi_create_int32(env, desiredDynamicRange, &result);
2120     return result;
2121 }
2122 
GetPixelMapDynamicRange(napi_env env,napi_callback_info info)2123 static napi_value GetPixelMapDynamicRange(napi_env env, napi_callback_info info)
2124 {
2125     napi_value result = nullptr;
2126     napi_value argValue[NUM_1] = {0};
2127     size_t argCount = NUM_1;
2128 
2129     napi_get_undefined(env, &result);
2130 
2131     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
2132         OH_LOG_ERROR(LOG_APP, "GetPixelMapDynamicRange napi_get_cb_info failed");
2133         return result;
2134     }
2135 
2136     void *ptr = nullptr;
2137     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
2138     OH_PixelmapNative *pixmap = reinterpret_cast<OH_PixelmapNative *>(ptr);
2139 
2140     OH_Pixelmap_ImageInfo *pixelmapInfo = nullptr;
2141     OH_PixelmapImageInfo_Create(&pixelmapInfo);
2142     OH_PixelmapNative_GetImageInfo(pixmap, pixelmapInfo);
2143     bool pixelmapIsHdr;
2144     OH_PixelmapImageInfo_GetDynamicRange(pixelmapInfo, &pixelmapIsHdr);
2145     if (CheckVpe()) {
2146         OH_LOG_INFO(LOG_APP, "GetPixelMapDynamicRange CheckVpe is true");
2147         if (pixelmapIsHdr) {
2148             napi_create_int32(env, NUM_0, &result);
2149         } else {
2150             napi_create_int32(env, NUM_1, &result);
2151         }
2152     } else {
2153         OH_LOG_INFO(LOG_APP, "GetPixelMapDynamicRange CheckVpe is false");
2154         napi_create_int32(env, NUM_0, &result);
2155     }
2156 
2157     return result;
2158 }
2159 
PixelMapToSdr(napi_env env,napi_callback_info info)2160 static napi_value PixelMapToSdr(napi_env env, napi_callback_info info)
2161 {
2162     napi_value result = nullptr;
2163     napi_value argValue[NUM_1] = {0};
2164     size_t argCount = NUM_1;
2165 
2166     napi_get_undefined(env, &result);
2167 
2168     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
2169         OH_LOG_ERROR(LOG_APP, "PixelMapToSdr napi_get_cb_info failed");
2170         return result;
2171     }
2172 
2173     void *ptr = nullptr;
2174     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
2175     OH_PixelmapNative *pixmap = reinterpret_cast<OH_PixelmapNative *>(ptr);
2176     Image_ErrorCode errCode = OH_PixelmapNative_ToSdr(pixmap);
2177     if (CheckVpe()) {
2178         OH_LOG_INFO(LOG_APP, "PixelMapToSdr CheckVpe is true");
2179         napi_create_int32(env, errCode, &result);
2180     } else {
2181         OH_LOG_INFO(LOG_APP, "PixelMapToSdr CheckVpe is false");
2182         napi_create_int32(env, NUM_0, &result);
2183     }
2184     return result;
2185 }
2186 
GetImageSourceDynamicRange(napi_env env,napi_callback_info info)2187 static napi_value GetImageSourceDynamicRange(napi_env env, napi_callback_info info)
2188 {
2189     napi_value result = nullptr;
2190     napi_value argValue[NUM_1] = {0};
2191     size_t argCount = NUM_1;
2192 
2193     napi_get_undefined(env, &result);
2194 
2195     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
2196         OH_LOG_ERROR(LOG_APP, "GetImageSourceDynamicRange napi_get_cb_info failed");
2197         return result;
2198     }
2199 
2200     void *ptr = nullptr;
2201     napi_status status = napi_get_value_external(env, argValue[NUM_0], &ptr);
2202     OH_ImageSourceNative *imageSource = reinterpret_cast<OH_ImageSourceNative *>(ptr);
2203 
2204     OH_ImageSource_Info *imageInfo = nullptr;
2205     OH_ImageSourceInfo_Create(&imageInfo);
2206 
2207     Image_ErrorCode errCode = OH_ImageSourceNative_GetImageInfo(imageSource, 0, imageInfo);
2208     if (errCode != IMAGE_SUCCESS) {
2209         OH_LOG_ERROR(LOG_APP, "GetImageSourceDynamicRange getImageInfo failed");
2210         napi_create_int32(env, NUM_1, &result);
2211         return result;
2212     }
2213     bool isHdr = false;
2214     OH_ImageSourceInfo_GetDynamicRange(imageInfo, &isHdr);
2215     if (isHdr) {
2216         napi_create_int32(env, NUM_0, &result);
2217     } else {
2218         napi_create_int32(env, NUM_1, &result);
2219     }
2220     return result;
2221 }
2222 
PackingOptionsSetDesiredDynamicRange(napi_env env,napi_callback_info info)2223 static napi_value PackingOptionsSetDesiredDynamicRange(napi_env env, napi_callback_info info)
2224 {
2225     napi_value result = nullptr;
2226 
2227     napi_value argValue[NUM_2] = {0};
2228     size_t argCount = NUM_2;
2229 
2230     napi_get_undefined(env, &result);
2231 
2232     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
2233         OH_LOG_ERROR(LOG_APP, "PackingOptionsSetDesiredDynamicRange napi_get_cb_info failed");
2234         return result;
2235     }
2236 
2237     void *ptr = nullptr;
2238     napi_get_value_external(env, argValue[NUM_0], &ptr);
2239     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
2240 
2241     int32_t desiredDynamicRange;
2242     napi_get_value_int32(env, argValue[NUM_1], &desiredDynamicRange);
2243 
2244     Image_ErrorCode errCode = OH_PackingOptions_SetDesiredDynamicRange(packingOptions, desiredDynamicRange);
2245     napi_create_int32(env, errCode, &result);
2246     return result;
2247 }
2248 
PackingOptionsGetDesiredDynamicRange(napi_env env,napi_callback_info info)2249 static napi_value PackingOptionsGetDesiredDynamicRange(napi_env env, napi_callback_info info)
2250 {
2251     napi_value result = nullptr;
2252 
2253     napi_value argValue[NUM_1] = {0};
2254     size_t argCount = NUM_1;
2255 
2256     napi_get_undefined(env, &result);
2257 
2258     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
2259         OH_LOG_ERROR(LOG_APP, "PackingOptionsGetDesiredDynamicRange napi_get_cb_info failed");
2260         return result;
2261     }
2262 
2263     void *ptr = nullptr;
2264     napi_get_value_external(env, argValue[NUM_0], &ptr);
2265     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
2266 
2267     int32_t desiredDynamicRange;
2268     Image_ErrorCode errCode = OH_PackingOptions_GetDesiredDynamicRange(packingOptions, &desiredDynamicRange);
2269     if (errCode != IMAGE_SUCCESS) {
2270         napi_create_int32(env, errCode, &result);
2271         return result;
2272     }
2273     napi_create_int32(env, desiredDynamicRange, &result);
2274     return result;
2275 }
2276 
PackingOptionsSetNeedsPackProperties(napi_env env,napi_callback_info info)2277 static napi_value PackingOptionsSetNeedsPackProperties(napi_env env, napi_callback_info info)
2278 {
2279     napi_value result = nullptr;
2280 
2281     napi_value argValue[NUM_2] = {0};
2282     size_t argCount = NUM_2;
2283 
2284     napi_get_undefined(env, &result);
2285 
2286     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
2287         OH_LOG_ERROR(LOG_APP, "PackingOptionsSetNeedsPackProperties napi_get_cb_info failed");
2288         return result;
2289     }
2290 
2291     void *ptr = nullptr;
2292     napi_get_value_external(env, argValue[NUM_0], &ptr);
2293     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
2294 
2295     int needsPackProperties;
2296     napi_get_value_int32(env, argValue[NUM_1], &needsPackProperties);
2297 
2298     Image_ErrorCode errCode = OH_PackingOptions_SetNeedsPackProperties(packingOptions,
2299         static_cast<bool>(needsPackProperties));
2300     napi_create_int32(env, errCode, &result);
2301     return result;
2302 }
2303 
PackingOptionsGetNeedsPackProperties(napi_env env,napi_callback_info info)2304 static napi_value PackingOptionsGetNeedsPackProperties(napi_env env, napi_callback_info info)
2305 {
2306     napi_value result = nullptr;
2307 
2308     napi_value argValue[NUM_1] = {0};
2309     size_t argCount = NUM_1;
2310 
2311     napi_get_undefined(env, &result);
2312 
2313     if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
2314         OH_LOG_ERROR(LOG_APP, "PackingOptionsGetNeedsPackProperties napi_get_cb_info failed");
2315         return result;
2316     }
2317 
2318     void *ptr = nullptr;
2319     napi_get_value_external(env, argValue[NUM_0], &ptr);
2320     OH_PackingOptions *packingOptions = reinterpret_cast<OH_PackingOptions *>(ptr);
2321 
2322     bool needsPackProperties;
2323     Image_ErrorCode errCode = OH_PackingOptions_GetNeedsPackProperties(packingOptions, &needsPackProperties);
2324     if (errCode != IMAGE_SUCCESS) {
2325         napi_create_int32(env, errCode, &result);
2326         return result;
2327     }
2328     napi_create_int32(env, needsPackProperties, &result);
2329     return result;
2330 }
2331 
2332 EXTERN_C_START
Init(napi_env env,napi_value exports)2333 static napi_value Init(napi_env env, napi_value exports) {
2334     napi_property_descriptor desc[] = {
2335         {"testInitializationOptions", nullptr, TestInitializationOptions, nullptr, nullptr, nullptr, napi_default,
2336          nullptr},
2337         {"testCreatePixelmap", nullptr, TestCreatePixelmap, nullptr, nullptr, nullptr, napi_default, nullptr},
2338         {"testCreatePixelmapWithData", nullptr, TestCreatePixelmapWithData, nullptr, nullptr, nullptr, napi_default,
2339          nullptr},
2340         {"testSavePixelmap", nullptr, TestSavePixelmap, nullptr, nullptr, nullptr, napi_default, nullptr},
2341         {"testReadPixels", nullptr, TestReadPixels, nullptr, nullptr, nullptr, napi_default, nullptr},
2342         {"testWritePixels", nullptr, TestWritePixels, nullptr, nullptr, nullptr, napi_default, nullptr},
2343         {"testGetImageInfo", nullptr, TestGetImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr},
2344         {"testOpacity", nullptr, TestOpacity, nullptr, nullptr, nullptr, napi_default, nullptr},
2345         {"testScale", nullptr, TestScale, nullptr, nullptr, nullptr, napi_default, nullptr},
2346         {"testTranslate", nullptr, TestTranslate, nullptr, nullptr, nullptr, napi_default, nullptr},
2347         {"testRotate", nullptr, TestRotate, nullptr, nullptr, nullptr, napi_default, nullptr},
2348         {"testFlip", nullptr, TestFlip, nullptr, nullptr, nullptr, napi_default, nullptr},
2349         {"testCrop", nullptr, TestCrop, nullptr, nullptr, nullptr, napi_default, nullptr},
2350         {"testReleasePixelmap", nullptr, TestReleasePixelmap, nullptr, nullptr, nullptr, napi_default, nullptr},
2351         {"CreateImageInfo", nullptr, CreateImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr},
2352         {"ImageInfoGetWidth", nullptr, ImageInfoGetWidth, nullptr, nullptr, nullptr, napi_default, nullptr},
2353         {"ImageInfoGetHeight", nullptr, ImageInfoGetHeight, nullptr, nullptr, nullptr, napi_default, nullptr},
2354         {"ReleaseImageInfo", nullptr, ReleaseImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr},
2355         {"CreateDecodingOptions", nullptr, CreateDecodingOptions, nullptr, nullptr, nullptr, napi_default, nullptr},
2356         {"DecodingOptionsGetPixelFormat", nullptr, DecodingOptionsGetPixelFormat, nullptr, nullptr, nullptr,
2357          napi_default, nullptr},
2358         {"DecodingOptionsSetPixelFormat", nullptr, DecodingOptionsSetPixelFormat, nullptr, nullptr, nullptr,
2359          napi_default, nullptr},
2360         {"DecodingOptionsGetIndex", nullptr, DecodingOptionsGetIndex, nullptr, nullptr, nullptr, napi_default, nullptr},
2361         {"DecodingOptionsSetIndex", nullptr, DecodingOptionsSetIndex, nullptr, nullptr, nullptr, napi_default, nullptr},
2362         {"DecodingOptionsGetRotate", nullptr, DecodingOptionsGetRotate, nullptr, nullptr, nullptr, napi_default,
2363          nullptr},
2364         {"DecodingOptionsSetRotate", nullptr, DecodingOptionsSetRotate, nullptr, nullptr, nullptr, napi_default,
2365          nullptr},
2366         {"DecodingOptionsGetDesiredSize", nullptr, DecodingOptionsGetDesiredSize, nullptr, nullptr, nullptr,
2367          napi_default, nullptr},
2368         {"DecodingOptionsSetDesiredSize", nullptr, DecodingOptionsSetDesiredSize, nullptr, nullptr, nullptr,
2369          napi_default, nullptr},
2370         {"DecodingOptionsGetDesiredRegion", nullptr, DecodingOptionsGetDesiredRegion, nullptr, nullptr, nullptr,
2371          napi_default, nullptr},
2372         {"DecodingOptionsSetDesiredRegion", nullptr, DecodingOptionsSetDesiredRegion, nullptr, nullptr, nullptr,
2373          napi_default, nullptr},
2374         {"ReleaseDecodingOptions", nullptr, ReleaseDecodingOptions, nullptr, nullptr, nullptr, napi_default, nullptr},
2375         {"CreateFromUri", nullptr, CreateFromUri, nullptr, nullptr, nullptr, napi_default, nullptr},
2376         {"CreateFromFd", nullptr, CreateFromFd, nullptr, nullptr, nullptr, napi_default, nullptr},
2377         {"CreateFromData", nullptr, CreateFromData, nullptr, nullptr, nullptr, napi_default, nullptr},
2378         {"CreatePixelMap", nullptr, CreatePixelMap, nullptr, nullptr, nullptr, napi_default, nullptr},
2379         {"CreatePixelMapList", nullptr, CreatePixelMapList, nullptr, nullptr, nullptr, napi_default, nullptr},
2380         {"GetImageInfo", nullptr, GetImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr},
2381         {"GetImageProperty", nullptr, GetImageProperty, nullptr, nullptr, nullptr, napi_default, nullptr},
2382         {"ModifyImageProperty", nullptr, ModifyImageProperty, nullptr, nullptr, nullptr, napi_default, nullptr},
2383         {"GetFrameCount", nullptr, GetFrameCount, nullptr, nullptr, nullptr, napi_default, nullptr},
2384         {"CreateFromRawFile", nullptr, CreateFromRawFile, nullptr, nullptr, nullptr, napi_default, nullptr},
2385         {"SourceRelease", nullptr, SourceRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
2386         {"PixelMapRelease", nullptr, PixelMapRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
2387         {"CreatePackingOptions", nullptr, CreatePackingOptions, nullptr, nullptr, nullptr, napi_default, nullptr},
2388         {"PackingOptionsGetMimeType", nullptr, PackingOptionsGetMimeType, nullptr, nullptr, nullptr, napi_default,
2389          nullptr},
2390         {"PackingOptionsSetMimeType", nullptr, PackingOptionsSetMimeType, nullptr, nullptr, nullptr, napi_default,
2391          nullptr},
2392         {"PackingOptionsGetQuality", nullptr, PackingOptionsGetQuality, nullptr, nullptr, nullptr, napi_default,
2393          nullptr},
2394         {"PackingOptionsSetQuality", nullptr, PackingOptionsSetQuality, nullptr, nullptr, nullptr, napi_default,
2395          nullptr},
2396         {"ReleasePackingOptions", nullptr, ReleasePackingOptions, nullptr, nullptr, nullptr, napi_default, nullptr},
2397         {"PackerCreate", nullptr, PackerCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
2398         {"PackToDataFromImageSource", nullptr, PackToDataFromImageSource, nullptr, nullptr, nullptr, napi_default,
2399          nullptr},
2400         {"PackToDataFromPixelMap", nullptr, PackToDataFromPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr},
2401         {"PackToFileFromImageSource", nullptr, PackToFileFromImageSource, nullptr, nullptr, nullptr, napi_default,
2402          nullptr},
2403         {"PackToFileFromPixelMap", nullptr, PackToFileFromPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr},
2404         {"PackerRelease", nullptr, PackerRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
2405         {"GetDelayTime", nullptr, GetDelayTime, nullptr, nullptr, nullptr, napi_default, nullptr},
2406         {"DecodingOptionsSetDesiredDynamicRange", nullptr, DecodingOptionsSetDesiredDynamicRange, nullptr, nullptr,
2407          nullptr, napi_default, nullptr},
2408         {"DecodingOptionsGetDesiredDynamicRange", nullptr, DecodingOptionsGetDesiredDynamicRange, nullptr, nullptr,
2409          nullptr, napi_default, nullptr},
2410         {"GetImageSourceDynamicRange", nullptr, GetImageSourceDynamicRange, nullptr, nullptr, nullptr,
2411          napi_default, nullptr},
2412         {"GetPixelMapDynamicRange", nullptr, GetPixelMapDynamicRange, nullptr, nullptr, nullptr,
2413          napi_default, nullptr},
2414         {"PackingOptionsGetDesiredDynamicRange", nullptr, PackingOptionsGetDesiredDynamicRange, nullptr, nullptr,
2415          nullptr, napi_default, nullptr},
2416         {"PackingOptionsSetDesiredDynamicRange", nullptr, PackingOptionsSetDesiredDynamicRange, nullptr, nullptr,
2417          nullptr, napi_default, nullptr},
2418         {"PixelMapToSdr", nullptr, PixelMapToSdr, nullptr, nullptr, nullptr, napi_default, nullptr},
2419         {"CheckHasHdr", nullptr, CheckHasHdr, nullptr, nullptr, nullptr, napi_default, nullptr},
2420         {"PackingOptionsSetNeedsPackProperties", nullptr, PackingOptionsSetNeedsPackProperties, nullptr, nullptr,
2421          nullptr, napi_default, nullptr},
2422         {"PackingOptionsGetNeedsPackProperties", nullptr, PackingOptionsGetNeedsPackProperties, nullptr, nullptr,
2423          nullptr, napi_default, nullptr}
2424     };
2425     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
2426     return exports;
2427 }
2428 EXTERN_C_END
2429 
2430 static napi_module demoModule = {
2431     .nm_version = 1,
2432     .nm_flags = 0,
2433     .nm_filename = nullptr,
2434     .nm_register_func = Init,
2435     .nm_modname = "entry",
2436     .nm_priv = ((void *)0),
2437     .reserved = {0},
2438 };
2439 
RegisterEntryModule(void)2440 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }