1 /*
2  * Copyright (c) 2024 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 "cj_color_manager.h"
17 
18 namespace OHOS {
19 namespace ColorManager {
CjColorManager(std::shared_ptr<ColorSpace> ptr)20 CjColorManager::CjColorManager(std::shared_ptr<ColorSpace> ptr): colorSpaceToken_(ptr) {}
21 
create(ApiColorSpaceType csType)22 std::tuple<int32_t, std::string, std::shared_ptr<ColorSpace>> CjColorManager::create(ApiColorSpaceType csType)
23 {
24     if (csType == ApiColorSpaceType::UNKNOWN || csType == ApiColorSpaceType::CUSTOM) {
25         CMLOGE("[CJ]ColorSpaceType is invalid: %{public}u", csType);
26         return std::make_tuple(static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_ENUM_USAGE)),
27             "Parameter value is abnormal. Cannot create color manager object using ApiColorSpaceType 5",
28             nullptr);
29     }
30     std::shared_ptr<ColorSpace> colorSpace =
31             std::make_shared<ColorSpace>(CJ_TO_NATIVE_COLOR_SPACE_NAME_MAP.at(csType));
32     if (colorSpace == nullptr) {
33         return std::make_tuple(static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR)),
34             "Parameter value is abnormal. Fail to create JsColorSpaceObject with input parameter(s).",
35             nullptr);
36     }
37     CMLOGI("[CJ]OnCreateColorSpace CreateJsColorSpaceObject is called");
38     return std::make_tuple(0, "", colorSpace);
39 }
40 
create(ColorSpacePrimaries primaries,float gamma,int32_t * errCode)41 std::shared_ptr<ColorSpace> CjColorManager::create(ColorSpacePrimaries primaries, float gamma, int32_t* errCode)
42 {
43     std::shared_ptr<ColorSpace> colorSpace = std::make_shared<ColorSpace>(primaries, gamma);
44     if (colorSpace == nullptr) {
45         *errCode = static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR));
46         return nullptr;
47     }
48     CMLOGI("[CJ]OnCreateColorSpace CreateJsColorSpaceObject is called");
49     *errCode = 0;
50     return colorSpace;
51 }
52 
GetColorSpaceName(int32_t * errCode)53 uint32_t CjColorManager::GetColorSpaceName(int32_t* errCode)
54 {
55     if (colorSpaceToken_ == nullptr) {
56         CMLOGE("[CJ]colorSpaceToken_ is nullptr");
57         *errCode = static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR));
58         return 0;
59     }
60     ColorSpaceName csName = colorSpaceToken_->GetColorSpaceName();
61     if (NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP.count(csName) != 0) {
62         CMLOGI("[CJ]get color space name %{public}u, api type %{public}u",
63             csName, NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP.at(csName));
64         *errCode = 0;
65         return static_cast<uint32_t>(NATIVE_TO_CJ_COLOR_SPACE_TYPE_MAP.at(csName));
66     }
67     CMLOGE("[CJ]get color space name %{public}u, but not in api type", csName);
68     *errCode = static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_INVALID_PARAM));
69     return 0;
70 }
71 
GetWhitePoint(int32_t * errCode)72 std::array<float, DIMES_2> CjColorManager::GetWhitePoint(int32_t* errCode)
73 {
74     std::array<float, DIMES_2> wp;
75     if (colorSpaceToken_ == nullptr) {
76         CMLOGE("[CJ]colorSpaceToken_ is nullptr");
77         *errCode = static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR));
78         return wp;
79     }
80     wp = colorSpaceToken_->GetWhitePoint();
81     *errCode = 0;
82     return wp;
83 }
84 
GetGamma(int32_t * errCode)85 float CjColorManager::GetGamma(int32_t* errCode)
86 {
87     if (colorSpaceToken_ == nullptr) {
88         CMLOGE("[CJ]colorSpaceToken_ is nullptr");
89         *errCode = static_cast<int32_t>(CJ_TO_ERROR_CODE_MAP.at(CMError::CM_ERROR_NULLPTR));
90         return 0;
91     }
92     float gamma = colorSpaceToken_->GetGamma();
93     *errCode = 0;
94     return gamma;
95 }
96 }
97 }