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 "bridge/common/utils/engine_helper.h"
17 #include "core/common/ai/image_analyzer_adapter_impl.h"
18 #include "core/common/ai/image_analyzer_mgr.h"
19 #if defined(PIXEL_MAP_SUPPORTED)
20 #include "pixel_map_napi.h"
21 #endif
22 
23 namespace OHOS::Ace {
ImageAnalyzerAdapterImpl()24 ImageAnalyzerAdapterImpl::ImageAnalyzerAdapterImpl()
25 {
26     auto engine = EngineHelper::GetCurrentEngine();
27     CHECK_NULL_VOID(engine);
28     NativeEngine* nativeEngine = engine->GetNativeEngine();
29     env_ = reinterpret_cast<napi_env>(nativeEngine);
30 }
31 
~ImageAnalyzerAdapterImpl()32 ImageAnalyzerAdapterImpl::~ImageAnalyzerAdapterImpl()
33 {
34     if (analyzerConfigRef_ != nullptr) {
35         napi_delete_reference(env_, analyzerConfigRef_);
36     }
37 }
38 
SetImageAnalyzerConfig(void * config,bool isOptions)39 void ImageAnalyzerAdapterImpl::SetImageAnalyzerConfig(void* config, bool isOptions)
40 {
41     napi_ref configRef = nullptr;
42     napi_value configValue = reinterpret_cast<napi_value>(config);
43     napi_create_reference(env_, configValue, 1, &configRef);
44     analyzerConfigRef_ = configRef;
45     if (isOptions) {
46         hasOptions_ = true;
47     }
48 }
49 
GetImageAnalyzerConfig()50 void* ImageAnalyzerAdapterImpl::GetImageAnalyzerConfig()
51 {
52     napi_value analyzerConfig = nullptr;
53     napi_get_reference_value(env_, analyzerConfigRef_, &analyzerConfig);
54 
55     // If config is not set, all types are enabled by default.
56     if (analyzerConfig == nullptr) {
57         std::vector<ImageAnalyzerType> types = {ImageAnalyzerType::SUBJECT, ImageAnalyzerType::TEXT};
58         napi_value typeNapi = nullptr;
59         napi_create_array(env_, &typeNapi);
60         for (int i = 0; i < static_cast<int>(types.size()); ++i) {
61             napi_value tmpValue = nullptr;
62             napi_create_int32(env_, static_cast<int>(types[i]), &tmpValue);
63             napi_set_element(env_, typeNapi, i, tmpValue);
64         }
65         napi_set_named_property(env_, analyzerConfig, "types", typeNapi);
66     }
67     return analyzerConfig;
68 }
69 
ConvertPixmapNapi(const RefPtr<PixelMap> & pixelMap)70 void* ImageAnalyzerAdapterImpl::ConvertPixmapNapi(const RefPtr<PixelMap>& pixelMap)
71 {
72 #if defined(PIXEL_MAP_SUPPORTED)
73     napi_handle_scope scope = nullptr;
74     napi_open_handle_scope(env_, &scope);
75     auto engine = EngineHelper::GetCurrentEngine();
76     CHECK_NULL_RETURN(engine, {});
77     NativeEngine* nativeEngine = engine->GetNativeEngine();
78     auto env = reinterpret_cast<napi_env>(nativeEngine);
79     auto napiValue = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMap->GetPixelMapSharedPtr());
80     napi_ref napiValueRef = nullptr;
81     napi_create_reference(env_, napiValue, 1, &napiValueRef);
82     napi_close_handle_scope(env_, scope);
83     return napiValueRef;
84 #else
85     return nullptr;
86 #endif
87 }
88 
HasImageAnalyzerConfig()89 bool ImageAnalyzerAdapterImpl::HasImageAnalyzerConfig()
90 {
91     napi_value analyzerConfig = nullptr;
92     napi_get_reference_value(env_, analyzerConfigRef_, &analyzerConfig);
93     return analyzerConfig != nullptr && hasOptions_;
94 }
95 
CreateImageAnalyzerAdapter()96 ImageAnalyzerAdapter* CreateImageAnalyzerAdapter()
97 {
98     return new ImageAnalyzerAdapterImpl();
99 }
100 }