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