• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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_log.h"
17 #include "image_taihe_utils.h"
18 
19 namespace ANI::Image {
20 constexpr char CLASS_NAME_BUSINESSERROR[] = "L@ohos/base/BusinessError;";
21 
HicheckerReport()22 void ImageTaiheUtils::HicheckerReport()
23 {
24 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) && defined(HICHECKER_ENABLE)
25     uint32_t pid = getpid();
26     uint32_t tid = gettid();
27     std::string cautionMsg = "Trigger: pid = " + std::to_string(pid) + ", tid = " + std::to_string(tid);
28     HiviewDFX::HiChecker::NotifySlowProcess(cautionMsg);
29 #endif
30 }
31 
ThrowExceptionError(const std::string errMsg)32 void ImageTaiheUtils::ThrowExceptionError(const std::string errMsg)
33 {
34     IMAGE_LOGE("errMsg: %{public}s", errMsg.c_str());
35     taihe::set_error(errMsg);
36 }
37 
ThrowExceptionError(const int32_t errCode,const std::string errMsg)38 void ImageTaiheUtils::ThrowExceptionError(const int32_t errCode, const std::string errMsg)
39 {
40     IMAGE_LOGE("errCode: %{public}d, errMsg: %{public}s", errCode, errMsg.c_str());
41     taihe::set_business_error(errCode, errMsg);
42 }
43 
GetPropertyDouble(ani_env * env,ani_object obj,const std::string & name,double & value)44 bool ImageTaiheUtils::GetPropertyDouble(ani_env *env, ani_object obj, const std::string &name, double &value)
45 {
46     CHECK_ERROR_RETURN_RET_LOG(env == nullptr || obj == nullptr, false, "%{public}s param is nullptr", __func__);
47     ani_double result;
48     env->Object_GetPropertyByName_Double(obj, name.c_str(), &result);
49     value = static_cast<double>(result);
50     return true;
51 }
52 
ToBusinessError(ani_env * env,int32_t code,const std::string & message)53 ani_object ImageTaiheUtils::ToBusinessError(ani_env *env, int32_t code, const std::string &message)
54 {
55     ani_object err {};
56     ani_class cls {};
57     CHECK_ERROR_RETURN_RET_LOG(ANI_OK != env->FindClass(CLASS_NAME_BUSINESSERROR, &cls), err,
58         "find class %{public}s failed", CLASS_NAME_BUSINESSERROR);
59     ani_method ctor {};
60     CHECK_ERROR_RETURN_RET_LOG(ANI_OK != env->Class_FindMethod(cls, "<ctor>", ":V", &ctor), err,
61         "find method BusinessError constructor failed");
62     ani_object error {};
63     CHECK_ERROR_RETURN_RET_LOG(ANI_OK != env->Object_New(cls, ctor, &error), err,
64         "new object %{public}s failed", CLASS_NAME_BUSINESSERROR);
65     CHECK_ERROR_RETURN_RET_LOG(
66         ANI_OK != env->Object_SetPropertyByName_Double(error, "code", static_cast<ani_double>(code)), err,
67         "set property BusinessError.code failed");
68     ani_string messageRef {};
69     CHECK_ERROR_RETURN_RET_LOG(ANI_OK != env->String_NewUTF8(message.c_str(), message.size(), &messageRef), err,
70         "new message string failed");
71     CHECK_ERROR_RETURN_RET_LOG(
72         ANI_OK != env->Object_SetPropertyByName_Ref(error, "message", static_cast<ani_ref>(messageRef)), err,
73         "set property BusinessError.message failed");
74     return error;
75 }
76 
ParseSourceOptions(SourceOptions const & options)77 OHOS::Media::SourceOptions ImageTaiheUtils::ParseSourceOptions(SourceOptions const& options)
78 {
79     OHOS::Media::SourceOptions opts {};
80     opts.baseDensity = options.sourceDensity;
81     OHOS::Media::PixelFormat pixelFormat = OHOS::Media::PixelFormat::UNKNOWN;
82     if (options.sourcePixelFormat.has_value()) {
83         pixelFormat = static_cast<OHOS::Media::PixelFormat>(options.sourcePixelFormat->get_value());
84     }
85     opts.pixelFormat = pixelFormat;
86     OHOS::Media::Size size {};
87     if (options.sourceSize.has_value()) {
88         size.width = options.sourceSize.value().width;
89         size.height = options.sourceSize.value().height;
90     }
91     opts.size = size;
92     return opts;
93 }
94 
ToTaiheImageInfo(const OHOS::Media::ImageInfo & src,bool isHdr)95 ImageInfo ImageTaiheUtils::ToTaiheImageInfo(const OHOS::Media::ImageInfo &src, bool isHdr)
96 {
97     Size size {
98         .width = src.size.width,
99         .height = src.size.height,
100     };
101 
102     PixelMapFormat::key_t pixelFormatKey;
103     GetEnumKeyByValue<PixelMapFormat>(static_cast<int32_t>(src.pixelFormat), pixelFormatKey);
104     AlphaType::key_t alphaTypeKey;
105     GetEnumKeyByValue<AlphaType>(static_cast<int32_t>(src.alphaType), alphaTypeKey);
106 
107     ImageInfo result {
108         .size = size,
109         .pixelFormat = PixelMapFormat(pixelFormatKey),
110         .alphaType = AlphaType(alphaTypeKey),
111         .mimeType = src.encodedFormat,
112         .isHdr = isHdr,
113     };
114     return result;
115 }
116 
ToTaiheArrayString(const std::vector<std::string> & src)117 array<string> ImageTaiheUtils::ToTaiheArrayString(const std::vector<std::string> &src)
118 {
119     std::vector<::taihe::string> vec;
120     for (const auto &item : src) {
121         vec.emplace_back(item);
122     }
123     return array<string>(vec);
124 }
125 
CreateTaiheArrayBuffer(uint8_t * src,size_t srcLen)126 array<uint8_t> ImageTaiheUtils::CreateTaiheArrayBuffer(uint8_t* src, size_t srcLen)
127 {
128     if (src == nullptr || srcLen == 0) {
129         return array<uint8_t>(0);
130     }
131     return array<uint8_t>(copy_data_t{}, src, srcLen);
132 }
133 
GetUndefinedPtr(ani_env * env)134 uintptr_t ImageTaiheUtils::GetUndefinedPtr(ani_env *env)
135 {
136     ani_ref undefinedRef {};
137     env->GetUndefined(&undefinedRef);
138     ani_object undefinedObj = static_cast<ani_object>(undefinedRef);
139     return reinterpret_cast<uintptr_t>(undefinedObj);
140 }
141 
142 template <typename EnumType, typename ValueType>
GetEnumKeyByValue(ValueType value,typename EnumType::key_t & key)143 bool ImageTaiheUtils::GetEnumKeyByValue(ValueType value, typename EnumType::key_t &key)
144 {
145     for (size_t index = 0; index < std::size(EnumType::table); ++index) {
146         if (EnumType::table[index] == value) {
147             key = static_cast<typename EnumType::key_t>(index);
148             return true;
149         }
150     }
151     return false;
152 }
153 
154 template
155 bool ImageTaiheUtils::GetEnumKeyByValue<ImageFormat, int32_t>(int32_t value, typename ImageFormat::key_t &key);
156 
157 template
158 bool ImageTaiheUtils::GetEnumKeyByValue<PixelMapFormat, int32_t>(int32_t value, typename PixelMapFormat::key_t &key);
159 
160 template
161 bool ImageTaiheUtils::GetEnumKeyByValue<AlphaType, int32_t>(int32_t value, typename AlphaType::key_t &key);
162 
163 template
164 bool ImageTaiheUtils::GetEnumKeyByValue<PropertyKey, std::string>(std::string value, typename PropertyKey::key_t &key);
165 
166 template
167 bool ImageTaiheUtils::GetEnumKeyByValue<AuxiliaryPictureType, int32_t>(int32_t value,
168     typename AuxiliaryPictureType::key_t &key);
169 } // namespace ANI::Image