• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "js_color_space.h"
16 
17 #include <array>
18 
19 #include "color_space.h"
20 #include "js_color_space_utils.h"
21 
22 namespace OHOS {
23 namespace ColorManager {
Finalizer(NativeEngine * engine,void * data,void * hint)24 void JsColorSpace::Finalizer(NativeEngine* engine, void* data, void* hint)
25 {
26     auto jsColorSpace = std::unique_ptr<JsColorSpace>(static_cast<JsColorSpace*>(data));
27     if (jsColorSpace == nullptr) {
28         CMLOGE("[NAPI]Finalizer jsColorSpace is nullptr");
29         return;
30     }
31     auto csToken = jsColorSpace->colorSpaceToken_;
32     if (csToken == nullptr) {
33         CMLOGE("[NAPI]Finalizer colorSpaceToken_ is nullptr");
34         return;
35     }
36 }
37 
GetColorSpaceName(NativeEngine * engine,NativeCallbackInfo * info)38 NativeValue* JsColorSpace::GetColorSpaceName(NativeEngine* engine, NativeCallbackInfo* info)
39 {
40     JsColorSpace* me = CheckParamsAndGetThis<JsColorSpace>(engine, info);
41     if (me == nullptr) {
42         engine->Throw(CreateJsError(*engine,
43             static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
44             "Parameter check fails. Js color space object is nullptr."));
45         return nullptr;
46     }
47     return me->OnGetColorSpaceName(*engine, *info);
48 }
49 
GetWhitePoint(NativeEngine * engine,NativeCallbackInfo * info)50 NativeValue* JsColorSpace::GetWhitePoint(NativeEngine* engine, NativeCallbackInfo* info)
51 {
52     JsColorSpace* me = CheckParamsAndGetThis<JsColorSpace>(engine, info);
53     if (me == nullptr) {
54         engine->Throw(CreateJsError(*engine,
55             static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
56             "Parameter check fails. Js color space object is nullptr."));
57         return nullptr;
58     }
59     return me->OnGetWhitePoint(*engine, *info);
60 }
61 
GetGamma(NativeEngine * engine,NativeCallbackInfo * info)62 NativeValue* JsColorSpace::GetGamma(NativeEngine* engine, NativeCallbackInfo* info)
63 {
64     JsColorSpace* me = CheckParamsAndGetThis<JsColorSpace>(engine, info);
65     if (me == nullptr) {
66         engine->Throw(CreateJsError(*engine,
67             static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
68             "Parameter check fails. Js color space object is nullptr."));
69         return nullptr;
70     }
71     return me->OnGetGamma(*engine, *info);
72 }
73 
OnGetColorSpaceName(NativeEngine & engine,NativeCallbackInfo & info)74 NativeValue* JsColorSpace::OnGetColorSpaceName(NativeEngine& engine, NativeCallbackInfo& info)
75 {
76     if (colorSpaceToken_ == nullptr) {
77         CMLOGE("[NAPI]colorSpaceToken_ is nullptr");
78         engine.Throw(CreateJsError(engine, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
79             "Parameter check fails. Native color space object is nullptr."));
80         return engine.CreateUndefined();
81     }
82     ColorSpaceName csName = colorSpaceToken_->GetColorSpaceName();
83     if (NATIVE_TO_JS_COLOR_SPACE_TYPE_MAP.count(csName) != 0) {
84         CMLOGI("[NAPI]get color space name %{public}u, api type %{public}u",
85             csName, NATIVE_TO_JS_COLOR_SPACE_TYPE_MAP.at(csName));
86         return CreateJsValue(engine, NATIVE_TO_JS_COLOR_SPACE_TYPE_MAP.at(csName));
87     }
88     CMLOGE("[NAPI]get color space name %{public}u, but not in api type", csName);
89     engine.Throw(CreateJsError(engine,
90         static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM)),
91         "Parameter check fails. Color space type " + std::to_string(static_cast<int32_t>(csName)) +
92         "does not in supported type list."));
93     return engine.CreateUndefined();
94 }
95 
OnGetWhitePoint(NativeEngine & engine,NativeCallbackInfo & info)96 NativeValue* JsColorSpace::OnGetWhitePoint(NativeEngine& engine, NativeCallbackInfo& info)
97 {
98     if (colorSpaceToken_ == nullptr) {
99         CMLOGE("[NAPI]colorSpaceToken_ is nullptr");
100         engine.Throw(CreateJsError(engine, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
101             "Parameter check fails. Native color space object is nullptr."));
102         return engine.CreateUndefined();
103     }
104     std::array<float, DIMES_2> wp = colorSpaceToken_->GetWhitePoint();
105     NativeValue* arrayValue = engine.CreateArray(DIMES_2);
106     NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
107     for (uint32_t i = 0; i < DIMES_2; i++) {
108         array->SetElement(i, CreateJsValue(engine, wp[i]));
109     }
110     return arrayValue;
111 }
112 
OnGetGamma(NativeEngine & engine,NativeCallbackInfo & info)113 NativeValue* JsColorSpace::OnGetGamma(NativeEngine& engine, NativeCallbackInfo& info)
114 {
115     if (colorSpaceToken_ == nullptr) {
116         CMLOGE("[NAPI]colorSpaceToken_ is nullptr");
117         engine.Throw(CreateJsError(engine, static_cast<int32_t>(JS_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
118             "Parameter check fails. Native color space object is nullptr."));
119         return engine.CreateUndefined();
120     }
121     float gamma = colorSpaceToken_->GetGamma();
122     return CreateJsValue(engine, gamma);
123 }
124 
BindFunctions(NativeEngine & engine,NativeObject * object)125 void BindFunctions(NativeEngine& engine, NativeObject* object)
126 {
127     const char *moduleName = "JsColorSpace";
128     BindNativeFunction(engine, *object, "getColorSpaceName", moduleName, JsColorSpace::GetColorSpaceName);
129     BindNativeFunction(engine, *object, "getWhitePoint", moduleName, JsColorSpace::GetWhitePoint);
130     BindNativeFunction(engine, *object, "getGamma", moduleName, JsColorSpace::GetGamma);
131 }
132 } // namespace ColorManager
133 } // namespace OHOS
134