1 /* 2 * Copyright (c) 2023-2023 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 #ifndef EFFECT_SUGGESTION_INFO_PARSE_H 16 #define EFFECT_SUGGESTION_INFO_PARSE_H 17 18 #include <numeric> 19 #include <queue> 20 #include <sstream> 21 #include <string> 22 #include <vector> 23 #include <iostream> 24 #include <cstdint> 25 26 namespace OHOS { 27 namespace CameraStandard { 28 using namespace std; 29 30 typedef struct EffectSuggestionModeInfo { 31 int32_t modeType; 32 std::vector<int32_t> effectSuggestionList; to_stringEffectSuggestionModeInfo33 std::string to_string() const 34 { 35 std::ostringstream oss; 36 std::string listStr = std::accumulate(effectSuggestionList.cbegin(), effectSuggestionList.cend(), std::string(), 37 [](const auto& prefix, const auto& item) { 38 return prefix + (prefix.empty() ? "" : ",") + std::to_string(item); 39 }); 40 oss << "EffectSuggestionModeInfo{modeType:" << modeType << ",effectSuggestionList:[" << listStr << "]}"; 41 return oss.str(); 42 } 43 } EffectSuggestionModeInfo; 44 45 typedef struct EffectSuggestionInfo { 46 uint32_t modeCount; 47 std::vector<EffectSuggestionModeInfo> modeInfo; to_stringEffectSuggestionInfo48 std::string to_string() const 49 { 50 std::ostringstream oss; 51 std::string listStr = std::accumulate(modeInfo.cbegin(), modeInfo.cend(), std::string(), 52 [](const auto& prefix, const auto& item) { 53 return prefix + (prefix.empty() ? "" : ",") + item.to_string(); 54 }); 55 oss << "EffectSuggestionInfo{modeCount:" << modeCount << ",modeInfo:[" << listStr << "]}"; 56 return oss.str(); 57 } 58 } EffectSuggestionInfo; 59 60 class EffectSuggestionInfoParse { 61 public: GetEffectSuggestionInfo(int32_t * originInfo,uint32_t count,EffectSuggestionInfo & effectSuggestionInfo)62 void GetEffectSuggestionInfo(int32_t* originInfo, uint32_t count, EffectSuggestionInfo& effectSuggestionInfo) 63 { 64 if (count <= 0 || originInfo == nullptr) { 65 return; 66 } 67 ResizeModeInfo(originInfo, count, effectSuggestionInfo); 68 ResizeEffectSuggestionList(originInfo, effectSuggestionInfo); 69 } 70 private: ResizeModeInfo(int32_t * originInfo,uint32_t count,EffectSuggestionInfo & effectSuggestionInfo)71 void ResizeModeInfo(int32_t* originInfo, uint32_t count, EffectSuggestionInfo& effectSuggestionInfo) 72 { 73 int32_t MODE_END = -1; 74 uint32_t i = 0; 75 uint32_t j = i + 1; 76 while (j < count) { 77 if (originInfo[j] == MODE_END) { 78 std::pair<uint32_t, uint32_t> indexPair(i, j - 1); 79 modeInfoIndexRange_.push_back(indexPair); 80 effectSuggestionInfo.modeCount++; 81 i = j + 1; 82 j = i + 1; 83 } else { 84 j++; 85 } 86 } 87 effectSuggestionInfo.modeInfo.resize(effectSuggestionInfo.modeCount); 88 } 89 ResizeEffectSuggestionList(int32_t * originInfo,EffectSuggestionInfo & effectSuggestionInfo)90 void ResizeEffectSuggestionList(int32_t* originInfo, EffectSuggestionInfo& effectSuggestionInfo) 91 { 92 for (auto it = modeInfoIndexRange_.begin(); it != modeInfoIndexRange_.end(); ++it) { 93 uint32_t start = it->first; 94 int modeInfoIndex = std::distance(modeInfoIndexRange_.begin(), it); 95 EffectSuggestionModeInfo &modeInfo = effectSuggestionInfo.modeInfo[modeInfoIndex]; 96 int32_t mode = originInfo[start]; 97 int32_t typeNum = originInfo[start + 1]; 98 modeInfo.modeType = mode; 99 modeInfo.effectSuggestionList.resize(typeNum); 100 uint32_t effectStartIndex = start + 2; 101 for (int i = 0; i < typeNum; i++) { 102 modeInfo.effectSuggestionList[i] = originInfo[effectStartIndex + static_cast<uint32_t>(i)]; 103 } 104 } 105 } 106 std::vector<std::pair<uint32_t, uint32_t>> modeInfoIndexRange_; 107 }; 108 } // namespace CameraStandard 109 } // namespace OHOS 110 #endif // EFFECT_SUGGESTION_INFO_PARSE_H