• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RETURN_ELOG(ret != CAM_META_SUCCESS || item.count <= 0,
38         "MoonCaptureBoostFeature get OHOS_ABILITY_MOON_CAPTURE_BOOST failed");
39 
40     uint32_t currentMode = INVALID_MODE;
41     float currentMinRatio = INVALID_ZOOM_RATIO;
42     float currentMaxRatio = INVALID_ZOOM_RATIO;
43     const int32_t MAX_OFFSET = 2;
44     for (uint32_t i = 0; i + MAX_OFFSET < item.count; i++) {
45         if (currentMode == INVALID_MODE) {
46             currentMode = static_cast<SceneMode>(item.data.ui32[i]);
47             continue;
48         }
49         if (currentMinRatio == INVALID_ZOOM_RATIO) {
50             currentMinRatio = static_cast<float>(item.data.ui32[i]) / SKETCH_DIV;
51             continue;
52         }
53         if (currentMaxRatio == INVALID_ZOOM_RATIO) {
54             currentMaxRatio = static_cast<float>(item.data.ui32[i]) / SKETCH_DIV;
55             continue;
56         }
57         SketchReferenceFovRange sketchReferencefovRange;
58         sketchReferencefovRange.zoomMin = static_cast<float>(item.data.ui32[i]) / SKETCH_DIV;
59         sketchReferencefovRange.zoomMax = static_cast<float>(item.data.ui32[i + 1]) / SKETCH_DIV;  // Offset 1 data
60         sketchReferencefovRange.referenceValue =
61             static_cast<float>(item.data.ui32[i + 2]) / SKETCH_DIV; // Offset 2 data
62         i = i + 2;                                                                        // Offset 2 data
63         sketchFovRangeList_.emplace_back(sketchReferencefovRange);
64         MEDIA_DEBUG_LOG(
65             "MoonCaptureBoostFeature::MoonCaptureBoostFeature get sketch reference fov ratio:mode->%{public}d "
66             "%{public}f-%{public}f value->%{public}f",
67             currentMode, sketchReferencefovRange.zoomMin,
68                 sketchReferencefovRange.zoomMax, sketchReferencefovRange.referenceValue);
69         if (sketchReferencefovRange.zoomMax - currentMaxRatio >= -std::numeric_limits<float>::epsilon()) {
70             if (currentMode == static_cast<uint32_t>(relatedMode_)) {
71                 sketchZoomRatioRange_.zoomMin = currentMinRatio;
72                 sketchZoomRatioRange_.zoomMax = currentMaxRatio;
73                 break;
74             }
75             currentMode = INVALID_MODE;
76             currentMinRatio = INVALID_ZOOM_RATIO;
77             currentMaxRatio = INVALID_ZOOM_RATIO;
78             sketchFovRangeList_.clear();
79         }
80     }
81 }
82 
GetSketchEnableRatio()83 float MoonCaptureBoostFeature::GetSketchEnableRatio()
84 {
85     return sketchZoomRatioRange_.zoomMin;
86 }
87 
GetSketchReferenceFovRatio(float inputZoomRatio)88 float MoonCaptureBoostFeature::GetSketchReferenceFovRatio(float inputZoomRatio)
89 {
90     auto itRange =
91         std::find_if(sketchFovRangeList_.begin(), sketchFovRangeList_.end(), [inputZoomRatio](const auto& range) {
92             return inputZoomRatio - range.zoomMin >= -std::numeric_limits<float>::epsilon() &&
93                    inputZoomRatio - range.zoomMax < -std::numeric_limits<float>::epsilon();
94         });
95     CHECK_RETURN_RET(itRange != sketchFovRangeList_.end(), itRange->referenceValue);
96     MEDIA_WARNING_LOG("MoonCaptureBoostFeature::GetSketchReferenceFovRatio fail input:%{public}f, "
97                       "zoomRange:%{public}f-%{public}f ,return default value.",
98         inputZoomRatio, sketchZoomRatioRange_.zoomMin, sketchZoomRatioRange_.zoomMax);
99     return SKETCH_DEFAULT_FOV_RATIO;
100 }
101 
IsSupportedMoonCaptureBoostFeature()102 bool MoonCaptureBoostFeature::IsSupportedMoonCaptureBoostFeature()
103 {
104     return !sketchFovRangeList_.empty();
105 }
106 
GetRelatedMode()107 SceneMode MoonCaptureBoostFeature::GetRelatedMode()
108 {
109     return relatedMode_;
110 }
111 } // namespace CameraStandard
112 } // namespace OHOS