1 /*
2  * Copyright (c) 2024-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 "features/moon_capture_boost_feature.h"
17 
18 #include <cstdint>
19 
20 #include "abilities/sketch_ability.h"
21 #include "camera_log.h"
22 #include "capture_scene_const.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 constexpr uint32_t INVALID_MODE = 0xffffffff;
27 constexpr float INVALID_ZOOM_RATIO = -1.0f;
28 constexpr float SKETCH_DEFAULT_FOV_RATIO = 1.0f;
29 constexpr float SKETCH_DIV = 100.0f;
MoonCaptureBoostFeature(SceneMode relatedMode,const std::shared_ptr<OHOS::Camera::CameraMetadata> & deviceAbility)30 MoonCaptureBoostFeature::MoonCaptureBoostFeature(
31     SceneMode relatedMode, const std::shared_ptr<OHOS::Camera::CameraMetadata>& deviceAbility)
32     : relatedMode_(relatedMode)
33 {
34     MEDIA_INFO_LOG("MoonCaptureBoostFeature::MoonCaptureBoostFeature SceneMode:%{public}d", relatedMode);
35     camera_metadata_item_t item;
36     int ret = OHOS::Camera::FindCameraMetadataItem(deviceAbility->get(), OHOS_ABILITY_MOON_CAPTURE_BOOST, &item);
37     CHECK_ERROR_RETURN_LOG(ret != CAM_META_SUCCESS || item.count <= 0,
38         "MoonCaptureBoostFeature get OHOS_ABILITY_MOON_CAPTURE_BOOST failed");
39     uint32_t currentMode = INVALID_MODE;
40     float currentMinRatio = INVALID_ZOOM_RATIO;
41     float currentMaxRatio = INVALID_ZOOM_RATIO;
42     for (uint32_t i = 0; i < item.count; i++) {
43         if (currentMode == INVALID_MODE) {
44             currentMode = static_cast<SceneMode>(item.data.ui32[i]);
45             continue;
46         }
47         if (currentMinRatio == INVALID_ZOOM_RATIO) {
48             currentMinRatio = static_cast<float>(item.data.ui32[i]) / SKETCH_DIV;
49             continue;
50         }
51         if (currentMaxRatio == INVALID_ZOOM_RATIO) {
52             currentMaxRatio = static_cast<float>(item.data.ui32[i]) / SKETCH_DIV;
53             continue;
54         }
55         SketchReferenceFovRange fovRange;
56         fovRange.zoomMin = static_cast<float>(item.data.ui32[i]) / SKETCH_DIV;
57         fovRange.zoomMax = static_cast<float>(item.data.ui32[i + 1]) / SKETCH_DIV;        // Offset 1 data
58         fovRange.referenceValue = static_cast<float>(item.data.ui32[i + 2]) / SKETCH_DIV; // Offset 2 data
59         i = i + 2;                                                                        // Offset 2 data
60         sketchFovRangeList_.emplace_back(fovRange);
61         MEDIA_DEBUG_LOG(
62             "MoonCaptureBoostFeature::MoonCaptureBoostFeature get sketch reference fov ratio:mode->%{public}d "
63             "%{public}f-%{public}f value->%{public}f",
64             currentMode, fovRange.zoomMin, fovRange.zoomMax, fovRange.referenceValue);
65         if (fovRange.zoomMax - currentMaxRatio >= -std::numeric_limits<float>::epsilon()) {
66             if (currentMode == static_cast<uint32_t>(relatedMode_)) {
67                 sketchZoomRatioRange_.zoomMin = currentMinRatio;
68                 sketchZoomRatioRange_.zoomMax = currentMaxRatio;
69                 break;
70             }
71             currentMode = INVALID_MODE;
72             currentMinRatio = INVALID_ZOOM_RATIO;
73             currentMaxRatio = INVALID_ZOOM_RATIO;
74             sketchFovRangeList_.clear();
75         }
76     }
77 }
78 
GetSketchEnableRatio()79 float MoonCaptureBoostFeature::GetSketchEnableRatio()
80 {
81     return sketchZoomRatioRange_.zoomMin;
82 }
83 
GetSketchReferenceFovRatio(float inputZoomRatio)84 float MoonCaptureBoostFeature::GetSketchReferenceFovRatio(float inputZoomRatio)
85 {
86     auto itRange =
87         std::find_if(sketchFovRangeList_.begin(), sketchFovRangeList_.end(), [inputZoomRatio](const auto& range) {
88             return inputZoomRatio - range.zoomMin >= -std::numeric_limits<float>::epsilon() &&
89                    inputZoomRatio - range.zoomMax < -std::numeric_limits<float>::epsilon();
90         });
91     if (itRange != sketchFovRangeList_.end()) {
92         return itRange->referenceValue;
93     }
94     MEDIA_WARNING_LOG("MoonCaptureBoostFeature::GetSketchReferenceFovRatio fail input:%{public}f, "
95                       "zoomRange:%{public}f-%{public}f ,return default value.",
96         inputZoomRatio, sketchZoomRatioRange_.zoomMin, sketchZoomRatioRange_.zoomMax);
97     return SKETCH_DEFAULT_FOV_RATIO;
98 }
99 
IsSupportedMoonCaptureBoostFeature()100 bool MoonCaptureBoostFeature::IsSupportedMoonCaptureBoostFeature()
101 {
102     return !sketchFovRangeList_.empty();
103 }
104 
GetRelatedMode()105 SceneMode MoonCaptureBoostFeature::GetRelatedMode()
106 {
107     return relatedMode_;
108 }
109 } // namespace CameraStandard
110 } // namespace OHOS