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