• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #define LOG_TAG "SystemDefinedPixelMap"
16 
17 #include "system_defined_pixelmap.h"
18 #include "logger.h"
19 
20 namespace OHOS {
21 namespace UDMF {
SystemDefinedPixelMap()22 SystemDefinedPixelMap::SystemDefinedPixelMap()
23 {
24     SetType(SYSTEM_DEFINED_PIXEL_MAP);
25 }
26 
SystemDefinedPixelMap(std::vector<uint8_t> & data)27 SystemDefinedPixelMap::SystemDefinedPixelMap(std::vector<uint8_t> &data)
28 {
29     SetType(SYSTEM_DEFINED_PIXEL_MAP);
30     this->rawData_ = std::move(data);
31 }
32 
SystemDefinedPixelMap(UDType type,ValueType value)33 SystemDefinedPixelMap::SystemDefinedPixelMap(UDType type, ValueType value) : SystemDefinedRecord(type, value)
34 {
35     SetType(SYSTEM_DEFINED_PIXEL_MAP);
36     if (std::holds_alternative<std::vector<uint8_t>>(value)) {
37         rawData_ = std::get<std::vector<uint8_t>>(value);
38         return;
39     } else if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(value)) {
40         auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(value);
41         if (!pixelMap->EncodeTlv(rawData_)) {
42             LOG_ERROR(UDMF_KITS_INNER, "pixelMap encode fail!");
43         }
44     } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
45         auto object = std::get<std::shared_ptr<Object>>(value);
46         auto it = object->value_.find(PIXEL_MAP);
47         if (it == object->value_.end()) {
48             return;
49         }
50         if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(it->second)) {
51             auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(it->second);
52             if (!pixelMap->EncodeTlv(rawData_)) {
53                 LOG_ERROR(UDMF_KITS_INNER, "pixelMap encode fail!");
54             }
55         } else if (std::holds_alternative<std::vector<uint8_t>>(it->second)) {
56             rawData_ = std::get<std::vector<uint8_t>>(it->second);
57         }
58     }
59 }
60 
GetSize()61 int64_t SystemDefinedPixelMap::GetSize()
62 {
63     return static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size()) +
64         GetInnerEntriesSize();
65 }
66 
GetRawData() const67 std::vector<uint8_t> SystemDefinedPixelMap::GetRawData() const
68 {
69     return this->rawData_;
70 }
71 
SetRawData(const std::vector<uint8_t> & rawData)72 void SystemDefinedPixelMap::SetRawData(const std::vector<uint8_t> &rawData)
73 {
74     this->rawData_ = rawData;
75     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
76         auto object = std::get<std::shared_ptr<Object>>(value_);
77         auto pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(OHOS::Media::PixelMap::DecodeTlv(rawData_));
78         if (pixelMap == nullptr) {
79             LOG_ERROR(UDMF_KITS_INNER, "pixelMap decode fail!");
80             object->value_[PIXEL_MAP] = rawData;
81             return;
82         }
83         object->value_[PIXEL_MAP] = pixelMap;
84     }
85 }
86 
InitObject()87 void SystemDefinedPixelMap::InitObject()
88 {
89     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
90         auto value = value_;
91         value_ = std::make_shared<Object>();
92         auto object = std::get<std::shared_ptr<Object>>(value_);
93         auto pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(OHOS::Media::PixelMap::DecodeTlv(rawData_));
94         if (pixelMap == nullptr) {
95             LOG_ERROR(UDMF_KITS_INNER, "pixelMap decode fail!");
96             object->value_[PIXEL_MAP] = rawData_;
97         } else {
98             object->value_[PIXEL_MAP] = pixelMap;
99         }
100         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
101         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
102         object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
103     }
104 }
105 } // namespace UDMF
106 } // namespace OHOS