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_mdk.h"
17 
18 #include "common_utils.h"
19 #include "image_mdk_kits.h"
20 
21 using namespace OHOS::Media;
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 struct ImageNative_ {
26     ImageNapi* napi = nullptr;
27 };
28 
29 MIDK_EXPORT
OH_Image_InitImageNative(napi_env env,napi_value source)30 ImageNative* OH_Image_InitImageNative(napi_env env, napi_value source)
31 {
32     ImageNapi* napi = ImageNapi_Unwrap(env, source);
33     if (napi == nullptr) {
34         return nullptr;
35     }
36     std::unique_ptr<ImageNative> result = std::make_unique<ImageNative>();
37     result->napi = napi;
38     return result.release();
39 }
40 
41 MIDK_EXPORT
OH_Image_ClipRect(const ImageNative * native,struct OhosImageRect * rect)42 int32_t OH_Image_ClipRect(const ImageNative* native, struct OhosImageRect* rect)
43 {
44     if (native == nullptr || native->napi == nullptr) {
45         return IMAGE_RESULT_BAD_PARAMETER;
46     }
47     ImageNapiArgs args;
48     args.outRect = rect;
49     auto res = ImageNapiNativeCtxCall(CTX_FUNC_IMAGE_CLIP_RECT, native->napi, &args);
50     return res;
51 }
52 
53 MIDK_EXPORT
OH_Image_Size(const ImageNative * native,struct OhosImageSize * size)54 int32_t OH_Image_Size(const ImageNative* native, struct OhosImageSize* size)
55 {
56     if (native == nullptr || native->napi == nullptr) {
57         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
58     }
59     ImageNapiArgs args;
60     args.outSize = size;
61     auto res = ImageNapiNativeCtxCall(CTX_FUNC_IMAGE_SIZE, native->napi, &args);
62     return res;
63 }
64 
65 MIDK_EXPORT
OH_Image_Format(const ImageNative * native,int32_t * format)66 int32_t OH_Image_Format(const ImageNative* native, int32_t* format)
67 {
68     if (native == nullptr || native->napi == nullptr) {
69         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
70     }
71     ImageNapiArgs args;
72     args.outNum0 = format;
73     auto res = ImageNapiNativeCtxCall(CTX_FUNC_IMAGE_FORMAT, native->napi, &args);
74     return res;
75 }
76 
77 MIDK_EXPORT
OH_Image_GetComponent(const ImageNative * native,int32_t componentType,struct OhosImageComponent * componentNative)78 int32_t OH_Image_GetComponent(const ImageNative* native, int32_t componentType,
79     struct OhosImageComponent* componentNative)
80 {
81     if (native == nullptr || native->napi == nullptr) {
82         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
83     }
84     ImageNapiArgs args;
85     args.inNum0 = componentType;
86     args.outComponent = componentNative;
87     auto res = ImageNapiNativeCtxCall(CTX_FUNC_IMAGE_GET_COMPONENT, native->napi, &args);
88     return res;
89 }
90 
91 MIDK_EXPORT
OH_Image_Release(ImageNative * native)92 int32_t OH_Image_Release(ImageNative* native)
93 {
94     if (native != nullptr) {
95         delete native;
96     }
97     return IMAGE_RESULT_SUCCESS;
98 }
99 
100 #ifdef __cplusplus
101 };
102 #endif
103