114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License.
514cf0368Sopenharmony_ci * You may obtain a copy of the License at
614cf0368Sopenharmony_ci *
714cf0368Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
814cf0368Sopenharmony_ci *
914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and
1314cf0368Sopenharmony_ci * limitations under the License.
1414cf0368Sopenharmony_ci */
1514cf0368Sopenharmony_ci#define LOG_TAG "SystemDefinedPixelMap"
1614cf0368Sopenharmony_ci
1714cf0368Sopenharmony_ci#include "system_defined_pixelmap.h"
1814cf0368Sopenharmony_ci#include "logger.h"
1914cf0368Sopenharmony_ci
2014cf0368Sopenharmony_cinamespace OHOS {
2114cf0368Sopenharmony_cinamespace UDMF {
2214cf0368Sopenharmony_ciSystemDefinedPixelMap::SystemDefinedPixelMap()
2314cf0368Sopenharmony_ci{
2414cf0368Sopenharmony_ci    SetType(SYSTEM_DEFINED_PIXEL_MAP);
2514cf0368Sopenharmony_ci}
2614cf0368Sopenharmony_ci
2714cf0368Sopenharmony_ciSystemDefinedPixelMap::SystemDefinedPixelMap(std::vector<uint8_t> &data)
2814cf0368Sopenharmony_ci{
2914cf0368Sopenharmony_ci    SetType(SYSTEM_DEFINED_PIXEL_MAP);
3014cf0368Sopenharmony_ci    this->rawData_ = std::move(data);
3114cf0368Sopenharmony_ci}
3214cf0368Sopenharmony_ci
3314cf0368Sopenharmony_ciSystemDefinedPixelMap::SystemDefinedPixelMap(UDType type, ValueType value) : SystemDefinedRecord(type, value)
3414cf0368Sopenharmony_ci{
3514cf0368Sopenharmony_ci    SetType(SYSTEM_DEFINED_PIXEL_MAP);
3614cf0368Sopenharmony_ci    if (std::holds_alternative<std::vector<uint8_t>>(value)) {
3714cf0368Sopenharmony_ci        rawData_ = std::get<std::vector<uint8_t>>(value);
3814cf0368Sopenharmony_ci        return;
3914cf0368Sopenharmony_ci    } else if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(value)) {
4014cf0368Sopenharmony_ci        auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(value);
4114cf0368Sopenharmony_ci        if (!pixelMap->EncodeTlv(rawData_)) {
4214cf0368Sopenharmony_ci            LOG_ERROR(UDMF_KITS_INNER, "pixelMap encode fail!");
4314cf0368Sopenharmony_ci        }
4414cf0368Sopenharmony_ci    } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
4514cf0368Sopenharmony_ci        auto object = std::get<std::shared_ptr<Object>>(value);
4614cf0368Sopenharmony_ci        auto it = object->value_.find(PIXEL_MAP);
4714cf0368Sopenharmony_ci        hasObject_ = true;
4814cf0368Sopenharmony_ci        if (it == object->value_.end()) {
4914cf0368Sopenharmony_ci            return;
5014cf0368Sopenharmony_ci        }
5114cf0368Sopenharmony_ci        if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(it->second)) {
5214cf0368Sopenharmony_ci            auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(it->second);
5314cf0368Sopenharmony_ci            if (!pixelMap->EncodeTlv(rawData_)) {
5414cf0368Sopenharmony_ci                LOG_ERROR(UDMF_KITS_INNER, "pixelMap encode fail!");
5514cf0368Sopenharmony_ci            }
5614cf0368Sopenharmony_ci        } else if (std::holds_alternative<std::vector<uint8_t>>(it->second)) {
5714cf0368Sopenharmony_ci            rawData_ = std::get<std::vector<uint8_t>>(it->second);
5814cf0368Sopenharmony_ci        }
5914cf0368Sopenharmony_ci    }
6014cf0368Sopenharmony_ci}
6114cf0368Sopenharmony_ci
6214cf0368Sopenharmony_ciint64_t SystemDefinedPixelMap::GetSize()
6314cf0368Sopenharmony_ci{
6414cf0368Sopenharmony_ci    return UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size();
6514cf0368Sopenharmony_ci}
6614cf0368Sopenharmony_ci
6714cf0368Sopenharmony_cistd::vector<uint8_t> SystemDefinedPixelMap::GetRawData() const
6814cf0368Sopenharmony_ci{
6914cf0368Sopenharmony_ci    return this->rawData_;
7014cf0368Sopenharmony_ci}
7114cf0368Sopenharmony_ci
7214cf0368Sopenharmony_civoid SystemDefinedPixelMap::SetRawData(const std::vector<uint8_t> &rawData)
7314cf0368Sopenharmony_ci{
7414cf0368Sopenharmony_ci    this->rawData_ = rawData;
7514cf0368Sopenharmony_ci    if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
7614cf0368Sopenharmony_ci        auto object = std::get<std::shared_ptr<Object>>(value_);
7714cf0368Sopenharmony_ci        auto pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(OHOS::Media::PixelMap::DecodeTlv(rawData_));
7814cf0368Sopenharmony_ci        if (pixelMap == nullptr) {
7914cf0368Sopenharmony_ci            LOG_ERROR(UDMF_KITS_INNER, "pixelMap decode fail!");
8014cf0368Sopenharmony_ci            object->value_[PIXEL_MAP] = rawData;
8114cf0368Sopenharmony_ci            return;
8214cf0368Sopenharmony_ci        }
8314cf0368Sopenharmony_ci        object->value_[PIXEL_MAP] = pixelMap;
8414cf0368Sopenharmony_ci    }
8514cf0368Sopenharmony_ci}
8614cf0368Sopenharmony_ci
8714cf0368Sopenharmony_civoid SystemDefinedPixelMap::InitObject()
8814cf0368Sopenharmony_ci{
8914cf0368Sopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
9014cf0368Sopenharmony_ci        auto value = value_;
9114cf0368Sopenharmony_ci        value_ = std::make_shared<Object>();
9214cf0368Sopenharmony_ci        auto object = std::get<std::shared_ptr<Object>>(value_);
9314cf0368Sopenharmony_ci        auto pixelMap = std::shared_ptr<OHOS::Media::PixelMap>(OHOS::Media::PixelMap::DecodeTlv(rawData_));
9414cf0368Sopenharmony_ci        if (pixelMap == nullptr) {
9514cf0368Sopenharmony_ci            LOG_ERROR(UDMF_KITS_INNER, "pixelMap decode fail!");
9614cf0368Sopenharmony_ci            object->value_[PIXEL_MAP] = rawData_;
9714cf0368Sopenharmony_ci        } else {
9814cf0368Sopenharmony_ci            object->value_[PIXEL_MAP] = pixelMap;
9914cf0368Sopenharmony_ci        }
10014cf0368Sopenharmony_ci        object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
10114cf0368Sopenharmony_ci        object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
10214cf0368Sopenharmony_ci        object->value_[VALUE_TYPE] = value;
10314cf0368Sopenharmony_ci    }
10414cf0368Sopenharmony_ci}
10514cf0368Sopenharmony_ci} // namespace UDMF
10614cf0368Sopenharmony_ci} // namespace OHOS