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 UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size();
62 }
63 
GetRawData() const64 std::vector<uint8_t> SystemDefinedPixelMap::GetRawData() const
65 {
66     return this->rawData_;
67 }
68 
SetRawData(const std::vector<uint8_t> & rawData)69 void SystemDefinedPixelMap::SetRawData(const std::vector<uint8_t> &rawData)
70 {
71     this->rawData_ = rawData;
72     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
73         auto object = std::get<std::shared_ptr<Object>>(value_);
74         auto pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(OHOS::Media::PixelMap::DecodeTlv(rawData_));
75         if (pixelMap == nullptr) {
76             LOG_ERROR(UDMF_KITS_INNER, "pixelMap decode fail!");
77             object->value_[PIXEL_MAP] = rawData;
78             return;
79         }
80         object->value_[PIXEL_MAP] = pixelMap;
81     }
82 }
83 
InitObject()84 void SystemDefinedPixelMap::InitObject()
85 {
86     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
87         auto value = value_;
88         value_ = std::make_shared<Object>();
89         auto object = std::get<std::shared_ptr<Object>>(value_);
90         auto pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(OHOS::Media::PixelMap::DecodeTlv(rawData_));
91         if (pixelMap == nullptr) {
92             LOG_ERROR(UDMF_KITS_INNER, "pixelMap decode fail!");
93             object->value_[PIXEL_MAP] = rawData_;
94         } else {
95             object->value_[PIXEL_MAP] = pixelMap;
96         }
97         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
98         object->value_["VALUE_TYPE"] = value;
99     }
100 }
101 
102 } // namespace UDMF
103 } // namespace OHOS