• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "image_mdk_kits.h"
17 
18 #include <map>
19 
20 namespace {
21     constexpr uint32_t NUM_0 = 0;
22 }
23 
24 namespace OHOS {
25 namespace Media {
26 using ImageNapiEnvFunc = int32_t (*)(napi_env env, struct ImageNapiArgs* args);
27 using ImageNapiCtxFunc = int32_t (*)(ImageNapi* native, struct ImageNapiArgs* args);
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
GetNativeImage(ImageNapi * napi)32 static NativeImage* GetNativeImage(ImageNapi* napi)
33 {
34     if (napi == nullptr) {
35         return nullptr;
36     }
37     return napi->GetNative();
38 }
39 
CheckAndGetImage(ImageNapi * native,const struct ImageNapiArgs * args)40 static NativeImage* CheckAndGetImage(ImageNapi* native, const struct ImageNapiArgs* args)
41 {
42     if (args == nullptr) {
43         return nullptr;
44     }
45     return GetNativeImage(native);
46 }
47 
ImageNapiClipRect(ImageNapi * native,struct ImageNapiArgs * args)48 static int32_t ImageNapiClipRect(ImageNapi* native, struct ImageNapiArgs* args)
49 {
50     auto nativeImage = CheckAndGetImage(native, args);
51     if (nativeImage == nullptr) {
52         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
53     }
54 
55     if (nativeImage->GetSize(args->outRect->width, args->outRect->height) != NUM_0) {
56         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
57     }
58 
59     args->outRect->x = NUM_0;
60     args->outRect->y = NUM_0;
61     return IMAGE_RESULT_SUCCESS;
62 }
63 
ImageNapiSize(ImageNapi * native,struct ImageNapiArgs * args)64 static int32_t ImageNapiSize(ImageNapi* native, struct ImageNapiArgs* args)
65 {
66     auto nativeImage = CheckAndGetImage(native, args);
67     if (nativeImage == nullptr) {
68         return IMAGE_RESULT_BAD_PARAMETER;
69     }
70 
71     if (nativeImage->GetSize(args->outSize->width, args->outSize->height) != NUM_0) {
72         return IMAGE_RESULT_BAD_PARAMETER;
73     }
74     return IMAGE_RESULT_SUCCESS;
75 }
76 
ImageNapiFormat(ImageNapi * native,struct ImageNapiArgs * args)77 static int32_t ImageNapiFormat(ImageNapi* native, struct ImageNapiArgs* args)
78 {
79     auto nativeImage = CheckAndGetImage(native, args);
80     if (nativeImage == nullptr) {
81         return IMAGE_RESULT_BAD_PARAMETER;
82     }
83     int32_t format;
84     if (nativeImage->GetFormat(format) != NUM_0) {
85         return IMAGE_RESULT_BAD_PARAMETER;
86     }
87     *(args->outNum0) = format;
88     return IMAGE_RESULT_SUCCESS;
89 }
90 
ImageNapiGetComponent(ImageNapi * native,struct ImageNapiArgs * args)91 static int32_t ImageNapiGetComponent(ImageNapi* native, struct ImageNapiArgs* args)
92 {
93     auto nativeImage = CheckAndGetImage(native, args);
94     if (nativeImage == nullptr || args->outComponent == nullptr) {
95         return IMAGE_RESULT_JNI_ENV_ABNORMAL;
96     }
97 
98     auto nativeComponent = nativeImage->GetComponent(args->inNum0);
99     if (nativeComponent == nullptr || nativeComponent->size == NUM_0) {
100         return IMAGE_RESULT_BAD_PARAMETER;
101     }
102 
103     if (nativeComponent->virAddr != nullptr) {
104         args->outComponent->byteBuffer = nativeComponent->virAddr;
105     } else {
106         args->outComponent->byteBuffer = nativeComponent->raw.data();
107     }
108 
109     if (args->outComponent->byteBuffer == nullptr) {
110         return IMAGE_RESULT_BAD_PARAMETER;
111     }
112     args->outComponent->size = nativeComponent->size;
113     args->outComponent->componentType = args->inNum0;
114     args->outComponent->pixelStride = nativeComponent->pixelStride;
115     args->outComponent->rowStride = nativeComponent->rowStride;
116     return IMAGE_RESULT_SUCCESS;
117 }
118 
119 static const std::map<int32_t, ImageNapiCtxFunc> g_CtxFunctions = {
120     {CTX_FUNC_IMAGE_CLIP_RECT, ImageNapiClipRect},
121     {CTX_FUNC_IMAGE_SIZE, ImageNapiSize},
122     {CTX_FUNC_IMAGE_FORMAT, ImageNapiFormat},
123     {CTX_FUNC_IMAGE_GET_COMPONENT, ImageNapiGetComponent},
124 };
125 
126 MIDK_EXPORT
ImageNapiNativeCtxCall(int32_t mode,ImageNapi * native,struct ImageNapiArgs * args)127 int32_t ImageNapiNativeCtxCall(int32_t mode, ImageNapi* native, struct ImageNapiArgs* args)
128 {
129     auto funcSearch = g_CtxFunctions.find(mode);
130     if (funcSearch == g_CtxFunctions.end()) {
131         return IMAGE_RESULT_BAD_PARAMETER;
132     }
133     return funcSearch->second(native, args);
134 }
135 
136 MIDK_EXPORT
ImageNapi_Unwrap(napi_env env,napi_value value)137 ImageNapi* ImageNapi_Unwrap(napi_env env, napi_value value)
138 {
139     napi_valuetype valueType;
140     napi_typeof(env, value, &valueType);
141     if (valueType != napi_object) {
142         return nullptr;
143     }
144     std::unique_ptr<ImageNapi> imageNapi = nullptr;
145     napi_status status = napi_unwrap(env, value, reinterpret_cast<void**>(&imageNapi));
146     if ((status == napi_ok) && imageNapi != nullptr) {
147         return imageNapi.release();
148     }
149     return nullptr;
150 }
151 #ifdef __cplusplus
152 };
153 #endif
154 }  // namespace Media
155 }  // namespace OHOS
156