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_kits.h"
17 #include "media_errors.h"
18 
19 #include <map>
20 
21 namespace {
22     constexpr uint32_t NUM_0 = 0;
23 }
24 
25 namespace OHOS {
26 namespace Media {
27 using ImageNapiEnvFunc = int32_t (*)(napi_env env, struct ImageNapiArgs* args);
28 using ImageNapiCtxFunc = int32_t (*)(ImageNapi* native, struct ImageNapiArgs* args);
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
GetNativeImage(ImageNapi * napi)33 static NativeImage* GetNativeImage(ImageNapi* napi)
34 {
35     if (napi == nullptr) {
36         return nullptr;
37     }
38     return napi->GetNative();
39 }
40 
CheckAndGetImage(ImageNapi * native,const struct ImageNapiArgs * args)41 static NativeImage* CheckAndGetImage(ImageNapi* native, const struct ImageNapiArgs* args)
42 {
43     if (args == nullptr) {
44         return nullptr;
45     }
46     return GetNativeImage(native);
47 }
48 
ImageNapiClipRect(ImageNapi * native,struct ImageNapiArgs * args)49 static int32_t ImageNapiClipRect(ImageNapi* native, struct ImageNapiArgs* args)
50 {
51     auto nativeImage = CheckAndGetImage(native, args);
52     if (nativeImage == nullptr) {
53         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
54     }
55 
56     if (nativeImage->GetSize(args->outRect->width, args->outRect->height) != NUM_0) {
57         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
58     }
59 
60     args->outRect->x = NUM_0;
61     args->outRect->y = NUM_0;
62     return IMAGE_RESULT_SUCCESS;
63 }
64 
ImageNapiSize(ImageNapi * native,struct ImageNapiArgs * args)65 static int32_t ImageNapiSize(ImageNapi* native, struct ImageNapiArgs* args)
66 {
67     auto nativeImage = CheckAndGetImage(native, args);
68     if (nativeImage == nullptr) {
69         return IMAGE_RESULT_BAD_PARAMETER;
70     }
71 
72     if (nativeImage->GetSize(args->outSize->width, args->outSize->height) != NUM_0) {
73         return IMAGE_RESULT_BAD_PARAMETER;
74     }
75     return IMAGE_RESULT_SUCCESS;
76 }
77 
ImageNapiFormat(ImageNapi * native,struct ImageNapiArgs * args)78 static int32_t ImageNapiFormat(ImageNapi* native, struct ImageNapiArgs* args)
79 {
80     auto nativeImage = CheckAndGetImage(native, args);
81     if (nativeImage == nullptr) {
82         return IMAGE_RESULT_BAD_PARAMETER;
83     }
84     int32_t format;
85     if (nativeImage->GetFormat(format) != NUM_0) {
86         return IMAGE_RESULT_BAD_PARAMETER;
87     }
88     *(args->outNum0) = format;
89     return IMAGE_RESULT_SUCCESS;
90 }
91 
ImageNapiGetComponent(ImageNapi * native,struct ImageNapiArgs * args)92 static int32_t ImageNapiGetComponent(ImageNapi* native, struct ImageNapiArgs* args)
93 {
94     auto nativeImage = CheckAndGetImage(native, args);
95     if (nativeImage == nullptr || args->outComponent == nullptr) {
96         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
97     }
98 
99     auto nativeComponent = nativeImage->GetComponent(args->inNum0);
100     if (nativeComponent == nullptr || nativeComponent->size == NUM_0) {
101         return IMAGE_RESULT_BAD_PARAMETER;
102     }
103 
104     if (nativeComponent->virAddr != nullptr) {
105         args->outComponent->byteBuffer = nativeComponent->virAddr;
106     } else {
107         args->outComponent->byteBuffer = nativeComponent->raw.data();
108     }
109 
110     if (args->outComponent->byteBuffer == nullptr) {
111         return IMAGE_RESULT_BAD_PARAMETER;
112     }
113     args->outComponent->size = nativeComponent->size;
114     args->outComponent->componentType = args->inNum0;
115     args->outComponent->pixelStride = nativeComponent->pixelStride;
116     args->outComponent->rowStride = nativeComponent->rowStride;
117     return IMAGE_RESULT_SUCCESS;
118 }
119 
120 static const std::map<int32_t, ImageNapiCtxFunc> g_CtxFunctions = {
121     {CTX_FUNC_IMAGE_CLIP_RECT, ImageNapiClipRect},
122     {CTX_FUNC_IMAGE_SIZE, ImageNapiSize},
123     {CTX_FUNC_IMAGE_FORMAT, ImageNapiFormat},
124     {CTX_FUNC_IMAGE_GET_COMPONENT, ImageNapiGetComponent},
125 };
126 
127 MIDK_EXPORT
ImageNapiNativeCtxCall(int32_t mode,ImageNapi * native,struct ImageNapiArgs * args)128 int32_t ImageNapiNativeCtxCall(int32_t mode, ImageNapi* native, struct ImageNapiArgs* args)
129 {
130     auto funcSearch = g_CtxFunctions.find(mode);
131     if (funcSearch == g_CtxFunctions.end()) {
132         return IMAGE_RESULT_BAD_PARAMETER;
133     }
134     return funcSearch->second(native, args);
135 }
136 
137 MIDK_EXPORT
ImageNapi_Unwrap(napi_env env,napi_value value)138 ImageNapi* ImageNapi_Unwrap(napi_env env, napi_value value)
139 {
140     napi_valuetype valueType;
141     napi_typeof(env, value, &valueType);
142     if (valueType != napi_object) {
143         return nullptr;
144     }
145     std::unique_ptr<ImageNapi> imageNapi = nullptr;
146     napi_status status = napi_unwrap(env, value, reinterpret_cast<void**>(&imageNapi));
147     if ((status == napi_ok) && imageNapi != nullptr) {
148         return imageNapi.release();
149     }
150     return nullptr;
151 }
152 #ifdef __cplusplus
153 };
154 #endif
155 }  // namespace Media
156 }  // namespace OHOS
157