1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "image_pixel_map_imgndk_test.h"
17 #include "pixel_map_native_test.h"
18 #include "node_api.h"
19 #include "hilog/log.h"
20
21 namespace {
22 constexpr uint32_t NUM_0 = 0;
23 constexpr uint32_t NUM_1 = 1;
24 constexpr uint32_t NUM_2 = 2;
25 constexpr uint32_t NUM_3 = 3;
26 constexpr uint32_t NUM_4 = 4;
27 constexpr uint32_t NUM_5 = 5;
28 }
29
30 #define IMAGE_LOG_TAG "IMAGE_TAGLOG"
31 #define IMAGE_LOG_DOMAIN 0x3200
32
33 #define LOG(fmt, ...) (void)OH_LOG_Print(LOG_APP, LOG_INFO, IMAGE_LOG_DOMAIN, IMAGE_LOG_TAG, fmt, ##__VA_ARGS__)
34
35 #define STATIC_FUNCTION(n, f) { (n), nullptr, (f), nullptr, nullptr, nullptr, napi_static, nullptr }
36
Init(napi_env env,napi_value exports)37 napi_value ImagePixelMapNDKTest::Init(napi_env env, napi_value exports)
38 {
39 napi_property_descriptor props[] = {
40 STATIC_FUNCTION("createPixelMap", CreatePixelMap),
41 STATIC_FUNCTION("createAlphaPixelMap", CreateAlphaPixelMap),
42 STATIC_FUNCTION("initNativePixelMap", InitNativePixelMap),
43 STATIC_FUNCTION("getBytesNumberPerRow", GetBytesNumberPerRow),
44 STATIC_FUNCTION("getIsEditable", GetIsEditable),
45 STATIC_FUNCTION("isSupportAlpha", IsSupportAlpha),
46 STATIC_FUNCTION("setAlphaAble", SetAlphaAble),
47 STATIC_FUNCTION("getDensity", GetDensity),
48 STATIC_FUNCTION("setDensity", SetDensity),
49 STATIC_FUNCTION("setOpacity", SetOpacity),
50 STATIC_FUNCTION("scale", Scale),
51 STATIC_FUNCTION("translate", Translate),
52 STATIC_FUNCTION("rotate", Rotate),
53 STATIC_FUNCTION("flip", Flip),
54 STATIC_FUNCTION("crop", Crop),
55 STATIC_FUNCTION("getImageInfo", GetImageInfo),
56 STATIC_FUNCTION("accessPixels", AccessPixels),
57 STATIC_FUNCTION("unAccessPixels", UnAccessPixels),
58 {
59 "JsNativeToNapi", nullptr, OHOS::Media::ImagePixelMapNativeTest::JsNativeToNapi,
60 nullptr, nullptr, nullptr, napi_static, nullptr
61 },
62 {
63 "JsNapiToNative_ReturnProperty", nullptr,
64 OHOS::Media::ImagePixelMapNativeTest::JsNapiToNative_ReturnProperty,
65 nullptr, nullptr, nullptr, napi_static, nullptr
66 }
67 };
68 napi_define_properties(env, exports, sizeof(props) / sizeof(props[0]), props);
69 return exports;
70 }
71
parseWidth(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)72 static bool parseWidth(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
73 {
74 napi_value napiWidth = nullptr;
75 if (napi_get_named_property(env, root, "width", &napiWidth) != napi_ok || napiWidth == nullptr) {
76 return false;
77 }
78 return (napi_get_value_uint32(env, napiWidth, &(opts->width)) == napi_ok);
79 }
80
parseHeight(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)81 static bool parseHeight(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
82 {
83 napi_value napiHeight = nullptr;
84 if (napi_get_named_property(env, root, "height", &napiHeight) != napi_ok || napiHeight == nullptr) {
85 return false;
86 }
87 return (napi_get_value_uint32(env, napiHeight, &(opts->height)) == napi_ok);
88 }
89
parsePixelFormat(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)90 static bool parsePixelFormat(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
91 {
92 napi_value napiPixelFormat = nullptr;
93 if (napi_get_named_property(env, root, "pixelFormat", &napiPixelFormat) != napi_ok
94 || napiPixelFormat == nullptr) {
95 return false;
96 }
97 return (napi_get_value_int32(env, napiPixelFormat, &(opts->pixelFormat)) == napi_ok);
98 }
99
parseEditable(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)100 static bool parseEditable(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
101 {
102 napi_value napiEditable = nullptr;
103 if (napi_get_named_property(env, root, "editable", &napiEditable) != napi_ok
104 || napiEditable == nullptr) {
105 return false;
106 }
107 return (napi_get_value_uint32(env, napiEditable, &(opts->editable)) == napi_ok);
108 }
109
parseAlphaType(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)110 static bool parseAlphaType(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
111 {
112 napi_value napiAlphaType = nullptr;
113 if (napi_get_named_property(env, root, "alphaType", &napiAlphaType) != napi_ok
114 || napiAlphaType == nullptr) {
115 return false;
116 }
117 return (napi_get_value_uint32(env, napiAlphaType, &(opts->alphaType)) == napi_ok);
118 }
119
parseScaleMode(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)120 static bool parseScaleMode(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
121 {
122 napi_value napiScaleMode = nullptr;
123 if (napi_get_named_property(env, root, "scaleMode", &napiScaleMode) != napi_ok
124 || napiScaleMode == nullptr) {
125 return false;
126 }
127 return (napi_get_value_uint32(env, napiScaleMode, &(opts->scaleMode)) == napi_ok);
128 }
129
parseCreateOptions(napi_env env,napi_value root,struct OhosPixelMapCreateOps * opts)130 static bool parseCreateOptions(napi_env env, napi_value root, struct OhosPixelMapCreateOps* opts)
131 {
132 if (!parseWidth(env, root, opts)) {
133 return false;
134 }
135 if (!parseHeight(env, root, opts)) {
136 return false;
137 }
138 if (!parsePixelFormat(env, root, opts)) {
139 return false;
140 }
141 if (!parseEditable(env, root, opts)) {
142 return false;
143 }
144 if (!parseAlphaType(env, root, opts)) {
145 return false;
146 }
147 if (!parseScaleMode(env, root, opts)) {
148 return false;
149 }
150 return true;
151 }
152
CreatePixelMap(napi_env env,napi_callback_info info)153 napi_value ImagePixelMapNDKTest::CreatePixelMap(napi_env env, napi_callback_info info)
154 {
155 LOG("before ImagePixelMapNDKTest::CreatePixelMap");
156 napi_value udfVar = nullptr;
157 napi_value thisVar = nullptr;
158 napi_value argValue[NUM_2] = {0};
159 size_t argCount = NUM_2;
160
161 void* buffer = nullptr;
162 size_t bufferSize = NUM_0;
163 struct OhosPixelMapCreateOps createOps;
164 napi_value pixelmap = nullptr;
165
166 LOG("napi_value pixelmap = nullptr;");
167 napi_get_undefined(env, &udfVar);
168
169 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
170 argCount < NUM_2 || argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
171 return udfVar;
172 }
173 if (napi_get_arraybuffer_info(env, argValue[NUM_0], &buffer, &bufferSize) != napi_ok ||
174 buffer == nullptr || bufferSize == NUM_0) {
175 return udfVar;
176 }
177 if (!parseCreateOptions(env, argValue[NUM_1], &createOps)) {
178 return udfVar;
179 }
180
181 int32_t res = OH_PixelMap_CreatePixelMap(env, createOps, buffer, bufferSize, &pixelmap);
182 LOG("OH_PixelMap_CreatePixelMap, result is %{public}d", res);
183 if (res != IMAGE_RESULT_SUCCESS || pixelmap == nullptr) {
184 return udfVar;
185 }
186 return pixelmap;
187 }
188
CreateAlphaPixelMap(napi_env env,napi_callback_info info)189 napi_value ImagePixelMapNDKTest::CreateAlphaPixelMap(napi_env env, napi_callback_info info)
190 {
191 napi_value udfVar = nullptr;
192 napi_value thisVar = nullptr;
193 napi_value argValue[NUM_1] = {0};
194 size_t argCount = NUM_1;
195
196 napi_value alphaPixelmap = nullptr;
197
198 napi_get_undefined(env, &udfVar);
199
200 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
201 argCount < NUM_1 || argValue[NUM_0] == nullptr) {
202 return udfVar;
203 }
204 int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue[NUM_0], &alphaPixelmap);
205 if (res != IMAGE_RESULT_SUCCESS || alphaPixelmap == nullptr) {
206 return udfVar;
207 }
208 return alphaPixelmap;
209 }
getNativePixelMap(napi_env env,napi_callback_info info)210 static NativePixelMap* getNativePixelMap(napi_env env, napi_callback_info info)
211 {
212 napi_value thisVar = nullptr;
213 napi_value argValue[NUM_1] = {0};
214 size_t argCount = NUM_1;
215
216 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
217 argCount < NUM_1 || argValue[NUM_0] == nullptr) {
218 return nullptr;
219 }
220 return OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
221 }
222
InitNativePixelMap(napi_env env,napi_callback_info info)223 napi_value ImagePixelMapNDKTest::InitNativePixelMap(napi_env env, napi_callback_info info)
224 {
225 napi_value result = nullptr;
226 napi_get_undefined(env, &result);
227
228 NativePixelMap* native = getNativePixelMap(env, info);
229 if (native == nullptr) {
230 return result;
231 }
232
233 napi_create_int32(env, IMAGE_RESULT_SUCCESS, &result);
234 return result;
235 }
236
GetBytesNumberPerRow(napi_env env,napi_callback_info info)237 napi_value ImagePixelMapNDKTest::GetBytesNumberPerRow(napi_env env, napi_callback_info info)
238 {
239 napi_value result = nullptr;
240 napi_get_undefined(env, &result);
241
242 NativePixelMap* native = getNativePixelMap(env, info);
243 if (native == nullptr) {
244 return result;
245 }
246
247 int32_t rowBytes = NUM_0;
248 int32_t res = OH_PixelMap_GetBytesNumberPerRow(native, &rowBytes);
249 if (res != IMAGE_RESULT_SUCCESS || rowBytes == NUM_0) {
250 return result;
251 }
252
253 napi_create_int32(env, rowBytes, &result);
254 return result;
255 }
256
GetIsEditable(napi_env env,napi_callback_info info)257 napi_value ImagePixelMapNDKTest::GetIsEditable(napi_env env, napi_callback_info info)
258 {
259 napi_value result = nullptr;
260 napi_get_undefined(env, &result);
261
262 NativePixelMap* native = getNativePixelMap(env, info);
263 if (native == nullptr) {
264 return result;
265 }
266
267 int32_t editable = NUM_0;
268 int32_t res = OH_PixelMap_GetIsEditable(native, &editable);
269 if (res != IMAGE_RESULT_SUCCESS) {
270 return result;
271 }
272
273 napi_create_int32(env, editable, &result);
274 return result;
275 }
276
IsSupportAlpha(napi_env env,napi_callback_info info)277 napi_value ImagePixelMapNDKTest::IsSupportAlpha(napi_env env, napi_callback_info info)
278 {
279 napi_value result = nullptr;
280 napi_get_undefined(env, &result);
281
282 NativePixelMap* native = getNativePixelMap(env, info);
283 if (native == nullptr) {
284 return result;
285 }
286
287 int32_t supportAlpha = NUM_0;
288 int32_t res = OH_PixelMap_IsSupportAlpha(native, &supportAlpha);
289 if (res != IMAGE_RESULT_SUCCESS) {
290 return result;
291 }
292
293 napi_create_int32(env, supportAlpha, &result);
294 return result;
295 }
296
SetAlphaAble(napi_env env,napi_callback_info info)297 napi_value ImagePixelMapNDKTest::SetAlphaAble(napi_env env, napi_callback_info info)
298 {
299 napi_value result = nullptr;
300 napi_value thisVar = nullptr;
301 napi_value argValue[NUM_2] = {0};
302 size_t argCount = NUM_2;
303
304 napi_get_undefined(env, &result);
305 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
306 argCount < NUM_2 || argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
307 return result;
308 }
309 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
310 if (native == nullptr) {
311 return result;
312 }
313
314 int32_t alphaAble = NUM_0;
315 if (napi_get_value_int32(env, argValue[NUM_1], &alphaAble) != napi_ok) {
316 return result;
317 }
318
319 int32_t res = OH_PixelMap_SetAlphaAble(native, alphaAble);
320 napi_create_int32(env, res, &result);
321 return result;
322 }
323
GetDensity(napi_env env,napi_callback_info info)324 napi_value ImagePixelMapNDKTest::GetDensity(napi_env env, napi_callback_info info)
325 {
326 napi_value result = nullptr;
327 napi_get_undefined(env, &result);
328
329 NativePixelMap* native = getNativePixelMap(env, info);
330 if (native == nullptr) {
331 return result;
332 }
333
334 int32_t density = NUM_0;
335 int32_t res = OH_PixelMap_GetDensity(native, &density);
336 if (res != IMAGE_RESULT_SUCCESS) {
337 return result;
338 }
339
340 napi_create_int32(env, density, &result);
341 return result;
342 }
343
SetDensity(napi_env env,napi_callback_info info)344 napi_value ImagePixelMapNDKTest::SetDensity(napi_env env, napi_callback_info info)
345 {
346 napi_value result = nullptr;
347 napi_value thisVar = nullptr;
348 napi_value argValue[NUM_2] = {0};
349 size_t argCount = NUM_2;
350
351 napi_get_undefined(env, &result);
352
353 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
354 argCount < NUM_2 || argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
355 return result;
356 }
357
358 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
359 if (native == nullptr) {
360 return result;
361 }
362
363 int32_t density = NUM_0;
364 if (napi_get_value_int32(env, argValue[NUM_1], &density) != napi_ok) {
365 return result;
366 }
367
368 int32_t res = OH_PixelMap_SetDensity(native, density);
369 napi_create_int32(env, res, &result);
370 return result;
371 }
372
SetOpacity(napi_env env,napi_callback_info info)373 napi_value ImagePixelMapNDKTest::SetOpacity(napi_env env, napi_callback_info info)
374 {
375 napi_value result = nullptr;
376 napi_value thisVar = nullptr;
377 napi_value argValue[NUM_2] = {0};
378 size_t argCount = NUM_2;
379
380 napi_get_undefined(env, &result);
381
382 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
383 argCount < NUM_2 || argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
384 return result;
385 }
386
387 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
388 if (native == nullptr) {
389 return result;
390 }
391
392 double opacity = NUM_0;
393 if (napi_get_value_double(env, argValue[NUM_1], &opacity) != napi_ok) {
394 return result;
395 }
396
397 int32_t res = OH_PixelMap_SetOpacity(native, static_cast<float>(opacity));
398 napi_create_int32(env, res, &result);
399 return result;
400 }
401
Scale(napi_env env,napi_callback_info info)402 napi_value ImagePixelMapNDKTest::Scale(napi_env env, napi_callback_info info)
403 {
404 napi_value result = nullptr;
405 napi_value thisVar = nullptr;
406 napi_value argValue[NUM_3] = {0};
407 size_t argCount = NUM_3;
408
409 napi_get_undefined(env, &result);
410 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
411 argCount < NUM_3 || argValue[NUM_0] == nullptr ||
412 argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr) {
413 return result;
414 }
415 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
416 if (native == nullptr) {
417 return result;
418 }
419 double x = NUM_0;
420 double y = NUM_0;
421 if (napi_get_value_double(env, argValue[NUM_1], &x) != napi_ok ||
422 napi_get_value_double(env, argValue[NUM_2], &y) != napi_ok) {
423 return result;
424 }
425
426 int32_t res = OH_PixelMap_Scale(native, static_cast<float>(x), static_cast<float>(y));
427 napi_create_int32(env, res, &result);
428 return result;
429 }
430
Translate(napi_env env,napi_callback_info info)431 napi_value ImagePixelMapNDKTest::Translate(napi_env env, napi_callback_info info)
432 {
433 napi_value result = nullptr;
434 napi_value thisVar = nullptr;
435 napi_value argValue[NUM_3] = {0};
436 size_t argCount = NUM_3;
437
438 napi_get_undefined(env, &result);
439
440 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
441 argCount < NUM_3 || argValue[NUM_0] == nullptr ||
442 argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr) {
443 return result;
444 }
445
446 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
447 if (native == nullptr) {
448 return result;
449 }
450
451 double x = NUM_0;
452 double y = NUM_0;
453 if (napi_get_value_double(env, argValue[NUM_1], &x) != napi_ok ||
454 napi_get_value_double(env, argValue[NUM_2], &y) != napi_ok) {
455 return result;
456 }
457
458 int32_t res = OH_PixelMap_Translate(native, static_cast<float>(x), static_cast<float>(y));
459 napi_create_int32(env, res, &result);
460 return result;
461 }
462
Rotate(napi_env env,napi_callback_info info)463 napi_value ImagePixelMapNDKTest::Rotate(napi_env env, napi_callback_info info)
464 {
465 napi_value result = nullptr;
466 napi_value thisVar = nullptr;
467 napi_value argValue[NUM_2] = {0};
468 size_t argCount = NUM_2;
469
470 napi_get_undefined(env, &result);
471
472 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
473 argCount < NUM_2 || argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr) {
474 return result;
475 }
476
477 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
478 if (native == nullptr) {
479 return result;
480 }
481
482 double angle = NUM_0;
483 if (napi_get_value_double(env, argValue[NUM_1], &angle) != napi_ok) {
484 return result;
485 }
486
487 int32_t res = OH_PixelMap_Rotate(native, static_cast<float>(angle));
488 napi_create_int32(env, res, &result);
489 return result;
490 }
491
Flip(napi_env env,napi_callback_info info)492 napi_value ImagePixelMapNDKTest::Flip(napi_env env, napi_callback_info info)
493 {
494 napi_value result = nullptr;
495 napi_value thisVar = nullptr;
496 napi_value argValue[NUM_3] = {0};
497 size_t argCount = NUM_3;
498
499 napi_get_undefined(env, &result);
500
501 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
502 argCount < NUM_3 || argValue[NUM_0] == nullptr ||
503 argValue[NUM_1] == nullptr || argValue[NUM_2] == nullptr) {
504 return result;
505 }
506
507 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
508 if (native == nullptr) {
509 return result;
510 }
511
512 int32_t x = NUM_0;
513 int32_t y = NUM_0;
514 if (napi_get_value_int32(env, argValue[NUM_1], &x) != napi_ok ||
515 napi_get_value_int32(env, argValue[NUM_2], &y) != napi_ok) {
516 return result;
517 }
518
519 int32_t res = OH_PixelMap_Flip(native, x, y);
520 napi_create_int32(env, res, &result);
521 return result;
522 }
523
Crop(napi_env env,napi_callback_info info)524 napi_value ImagePixelMapNDKTest::Crop(napi_env env, napi_callback_info info)
525 {
526 napi_value result = nullptr;
527 napi_value thisVar = nullptr;
528 napi_value argValue[NUM_5] = {0};
529 size_t argCount = NUM_5;
530
531 napi_get_undefined(env, &result);
532
533 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok ||
534 argCount < NUM_5 || argValue[NUM_0] == nullptr || argValue[NUM_1] == nullptr ||
535 argValue[NUM_2] == nullptr || argValue[NUM_3] == nullptr || argValue[NUM_4] == nullptr) {
536 return result;
537 }
538
539 NativePixelMap* native = OH_PixelMap_InitNativePixelMap(env, argValue[NUM_0]);
540 if (native == nullptr) {
541 return result;
542 }
543
544 int32_t x = NUM_0;
545 int32_t y = NUM_0;
546 int32_t width = NUM_0;
547 int32_t height = NUM_0;
548 if (napi_get_value_int32(env, argValue[NUM_1], &x) != napi_ok ||
549 napi_get_value_int32(env, argValue[NUM_2], &y) != napi_ok ||
550 napi_get_value_int32(env, argValue[NUM_3], &width) != napi_ok ||
551 napi_get_value_int32(env, argValue[NUM_4], &height) != napi_ok) {
552 return result;
553 }
554
555 int32_t res = OH_PixelMap_Crop(native, x, y, width, height);
556 napi_create_int32(env, res, &result);
557 return result;
558 }
559
setInt32NamedProperty(napi_env env,napi_value object,const char * utf8name,uint32_t value)560 static void setInt32NamedProperty(napi_env env, napi_value object, const char* utf8name, uint32_t value)
561 {
562 napi_value tmp;
563 napi_create_int32(env, value, &tmp);
564 napi_set_named_property(env, object, utf8name, tmp);
565 }
566
setUint32NamedProperty(napi_env env,napi_value object,const char * utf8name,uint32_t value)567 static void setUint32NamedProperty(napi_env env, napi_value object, const char* utf8name, uint32_t value)
568 {
569 napi_value tmp;
570 napi_create_uint32(env, value, &tmp);
571 napi_set_named_property(env, object, utf8name, tmp);
572 }
573
GetImageInfo(napi_env env,napi_callback_info info)574 napi_value ImagePixelMapNDKTest::GetImageInfo(napi_env env, napi_callback_info info)
575 {
576 napi_value result = nullptr;
577 napi_get_undefined(env, &result);
578
579 NativePixelMap* native = getNativePixelMap(env, info);
580 if (native == nullptr) {
581 return result;
582 }
583
584 OhosPixelMapInfos pixelmapInfo;
585 int32_t res = OH_PixelMap_GetImageInfo(native, &pixelmapInfo);
586 if (res != IMAGE_RESULT_SUCCESS) {
587 return result;
588 }
589 napi_create_object(env, &result);
590 setUint32NamedProperty(env, result, "width", pixelmapInfo.width);
591 setUint32NamedProperty(env, result, "height", pixelmapInfo.height);
592 setUint32NamedProperty(env, result, "rowSize", pixelmapInfo.rowSize);
593 setInt32NamedProperty(env, result, "pixelFormat", pixelmapInfo.pixelFormat);
594 return result;
595 }
596
AccessPixels(napi_env env,napi_callback_info info)597 napi_value ImagePixelMapNDKTest::AccessPixels(napi_env env, napi_callback_info info)
598 {
599 napi_value result = nullptr;
600 napi_get_undefined(env, &result);
601
602 NativePixelMap* native = getNativePixelMap(env, info);
603 if (native == nullptr) {
604 return result;
605 }
606
607 void* pixelAddr = nullptr;
608 int32_t res = OH_PixelMap_AccessPixels(native, &pixelAddr);
609 if (res != IMAGE_RESULT_SUCCESS || pixelAddr == nullptr) {
610 return result;
611 }
612 napi_create_int32(env, res, &result);
613 return result;
614 }
615
UnAccessPixels(napi_env env,napi_callback_info info)616 napi_value ImagePixelMapNDKTest::UnAccessPixels(napi_env env, napi_callback_info info)
617 {
618 napi_value result = nullptr;
619 napi_get_undefined(env, &result);
620
621 NativePixelMap* native = getNativePixelMap(env, info);
622 if (native == nullptr) {
623 return result;
624 }
625
626 int32_t res = OH_PixelMap_UnAccessPixels(native);
627 napi_create_int32(env, res, &result);
628 return result;
629 }
630
631 EXTERN_C_START
ModuleRegister(napi_env env,napi_value exports)632 static napi_value ModuleRegister(napi_env env, napi_value exports)
633 {
634 ImagePixelMapNDKTest::Init(env, exports);
635 return exports;
636 }
637
638 static napi_module demoModule = {
639 .nm_version =1,
640 .nm_flags = 0,
641 .nm_filename = nullptr,
642 .nm_register_func = ModuleRegister,
643 .nm_modname = "entry",
644 .nm_priv = ((void*)0),
645 .reserved = { 0 },
646 };
647
RegisterModule(void)648 __attribute__((constructor)) void RegisterModule(void)
649 {
650 napi_module_register(&demoModule);
651 }
652 EXTERN_C_END
653