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 "colorspace_manager.h"
17
18 #include "effect_log.h"
19 #include "colorspace_processor.h"
20 #include "metadata_processor.h"
21
22 namespace OHOS {
23 namespace Media {
24 namespace Effect {
ColorSpaceManager()25 ColorSpaceManager::ColorSpaceManager()
26 {
27 strategy_ = std::make_shared<ColorSpaceStrategy>();
28 }
29
IsNeedConvertColorSpace(EffectColorSpace colorSpace)30 bool ColorSpaceManager::IsNeedConvertColorSpace(EffectColorSpace colorSpace)
31 {
32 return ColorSpaceStrategy::IsNeedConvertColorSpace(colorSpace);
33 }
34
IsSupportedColorSpace(EffectColorSpace colorSpace)35 bool ColorSpaceManager::IsSupportedColorSpace(EffectColorSpace colorSpace)
36 {
37 return ColorSpaceStrategy::IsSupportedColorSpace(colorSpace);
38 }
39
GetAllSupportedColorSpaces()40 std::unordered_set<EffectColorSpace> ColorSpaceManager::GetAllSupportedColorSpaces()
41 {
42 return ColorSpaceStrategy::GetAllSupportedColorSpaces();
43 }
44
Init(std::shared_ptr<EffectBuffer> & src,std::shared_ptr<EffectBuffer> & dst)45 void ColorSpaceManager::Init(std::shared_ptr<EffectBuffer> &src, std::shared_ptr<EffectBuffer> &dst)
46 {
47 strategy_->Init(src, dst);
48 }
49
ApplyColorSpace(EffectBuffer * effectBuffer,const EffectColorSpace & colorSpace,EffectColorSpace & outputColorSpace)50 ErrorCode ColorSpaceManager::ApplyColorSpace(EffectBuffer *effectBuffer, const EffectColorSpace &colorSpace,
51 EffectColorSpace &outputColorSpace)
52 {
53 CHECK_AND_RETURN_RET_LOG(effectBuffer != nullptr, ErrorCode::ERR_INPUT_NULL,
54 "ApplyColorSpace: effectBuffer is null!");
55 if (!IsNeedConvertColorSpace(colorSpace)) {
56 outputColorSpace = colorSpace;
57 return ErrorCode::SUCCESS;
58 }
59
60 EffectColorSpace targetColorSpace = ColorSpaceStrategy::GetTargetColorSpace(colorSpace);
61 ErrorCode res = strategy_->CheckConverterColorSpace(targetColorSpace);
62 CHECK_AND_RETURN_RET_LOG(res == ErrorCode::SUCCESS, res,
63 "ApplyColorSpace: CheckConverterColorSpace fail! res=%{public}d", res);
64
65 res = ColorSpaceProcessor::ApplyColorSpace(effectBuffer, targetColorSpace);
66 CHECK_AND_RETURN_RET_LOG(res == ErrorCode::SUCCESS, res, "ApplyColorSpace: convert fail! "
67 "res=%{public}d, colorSpace=%{public}d, targetColorSpace=%{public}d", res, colorSpace, targetColorSpace);
68 outputColorSpace = targetColorSpace;
69
70 return ErrorCode::SUCCESS;
71 }
72
ChooseColorSpace(const std::unordered_set<EffectColorSpace> & filtersSupportedColorSpace,const EffectColorSpace & srcRealColorSpace,EffectColorSpace & outputColorSpace)73 ErrorCode ColorSpaceManager::ChooseColorSpace(const std::unordered_set<EffectColorSpace> &filtersSupportedColorSpace,
74 const EffectColorSpace &srcRealColorSpace, EffectColorSpace &outputColorSpace)
75 {
76 return strategy_->ChooseColorSpace(filtersSupportedColorSpace, srcRealColorSpace, outputColorSpace);
77 }
78
Deinit()79 void ColorSpaceManager::Deinit()
80 {
81 strategy_->Deinit();
82 }
83
GetColorSpaceProcessor()84 std::shared_ptr<ColorSpaceProcessor> ColorSpaceManager::GetColorSpaceProcessor()
85 {
86 if (!colorSpaceConverter_) {
87 colorSpaceConverter_ = std::make_shared<ColorSpaceProcessor>();
88 }
89 return colorSpaceConverter_;
90 }
91
GetMetaDataProcessor()92 std::shared_ptr<MetadataProcessor> ColorSpaceManager::GetMetaDataProcessor()
93 {
94 if (!metadataGenerator_) {
95 metadataGenerator_ = std::make_shared<MetadataProcessor>();
96 }
97 return metadataGenerator_;
98 }
99
100 } // namespace Effect
101 } // namespace Media
102 } // namespace OHOS