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
20namespace OHOS {
21namespace UDMF {
22SystemDefinedPixelMap::SystemDefinedPixelMap()
23{
24    SetType(SYSTEM_DEFINED_PIXEL_MAP);
25}
26
27SystemDefinedPixelMap::SystemDefinedPixelMap(std::vector<uint8_t> &data)
28{
29    SetType(SYSTEM_DEFINED_PIXEL_MAP);
30    this->rawData_ = std::move(data);
31}
32
33SystemDefinedPixelMap::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        hasObject_ = true;
48        if (it == object->value_.end()) {
49            return;
50        }
51        if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(it->second)) {
52            auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(it->second);
53            if (!pixelMap->EncodeTlv(rawData_)) {
54                LOG_ERROR(UDMF_KITS_INNER, "pixelMap encode fail!");
55            }
56        } else if (std::holds_alternative<std::vector<uint8_t>>(it->second)) {
57            rawData_ = std::get<std::vector<uint8_t>>(it->second);
58        }
59    }
60}
61
62int64_t SystemDefinedPixelMap::GetSize()
63{
64    return UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size();
65}
66
67std::vector<uint8_t> SystemDefinedPixelMap::GetRawData() const
68{
69    return this->rawData_;
70}
71
72void 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
87void 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_[VALUE_TYPE] = value;
103    }
104}
105} // namespace UDMF
106} // namespace OHOS