• 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 #include "pixelmap_loader.h"
20 
21 namespace OHOS {
22 namespace UDMF {
23 static constexpr size_t BYTES_PER_COLOR = sizeof(uint32_t);
24 static constexpr const char* PIXEL_MAP_WIDTH = "width";
25 static constexpr const char* PIXEL_MAP_HEIGHT = "height";
26 static constexpr const char* PIXEL_MAP_FORMAT = "pixel-format";
27 static constexpr const char* PIXEL_MAP_ALPHA_TYPE = "alpha-type";
SystemDefinedPixelMap()28 SystemDefinedPixelMap::SystemDefinedPixelMap()
29 {
30     SetType(SYSTEM_DEFINED_PIXEL_MAP);
31 }
32 
SystemDefinedPixelMap(std::vector<uint8_t> & data)33 SystemDefinedPixelMap::SystemDefinedPixelMap(std::vector<uint8_t> &data)
34 {
35     SetType(SYSTEM_DEFINED_PIXEL_MAP);
36     this->rawData_ = std::move(data);
37 }
38 
SystemDefinedPixelMap(UDType type,ValueType value)39 SystemDefinedPixelMap::SystemDefinedPixelMap(UDType type, ValueType value) : SystemDefinedRecord(type, value)
40 {
41     SetType(SYSTEM_DEFINED_PIXEL_MAP);
42     if (std::holds_alternative<std::vector<uint8_t>>(value)) {
43         rawData_ = std::get<std::vector<uint8_t>>(value);
44         return;
45     } else if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(value)) {
46         auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(value);
47         ParseInfoFromPixelMap(pixelMap);
48     } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
49         auto object = std::get<std::shared_ptr<Object>>(value);
50         auto it = object->value_.find(PIXEL_MAP);
51         if (it == object->value_.end()) {
52             return;
53         }
54         if (std::holds_alternative<std::shared_ptr<OHOS::Media::PixelMap>>(it->second)) {
55             auto pixelMap = std::get<std::shared_ptr<OHOS::Media::PixelMap>>(it->second);
56             ParseInfoFromPixelMap(pixelMap);
57         } else if (std::holds_alternative<std::vector<uint8_t>>(it->second)) {
58             rawData_ = std::get<std::vector<uint8_t>>(it->second);
59         }
60     }
61 }
62 
GetSize()63 int64_t SystemDefinedPixelMap::GetSize()
64 {
65     return static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + rawData_.size()) +
66         GetInnerEntriesSize();
67 }
68 
GetRawData() const69 std::vector<uint8_t> SystemDefinedPixelMap::GetRawData() const
70 {
71     return this->rawData_;
72 }
73 
SetRawData(const std::vector<uint8_t> & rawData)74 void SystemDefinedPixelMap::SetRawData(const std::vector<uint8_t> &rawData)
75 {
76     this->rawData_ = rawData;
77     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
78         auto object = std::get<std::shared_ptr<Object>>(value_);
79         auto pixelMap = GetPixelMapFromRawData();
80         if (pixelMap == nullptr) {
81             LOG_ERROR(UDMF_KITS_INNER, "Get pixelMap from rawData fail!");
82             object->value_[PIXEL_MAP] = rawData;
83             return;
84         }
85         object->value_[PIXEL_MAP] = pixelMap;
86     }
87 }
88 
InitObject()89 void SystemDefinedPixelMap::InitObject()
90 {
91     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
92         auto value = value_;
93         value_ = std::make_shared<Object>();
94         auto object = std::get<std::shared_ptr<Object>>(value_);
95         auto pixelMap = GetPixelMapFromRawData();
96         if (pixelMap == nullptr) {
97             LOG_WARN(UDMF_KITS_INNER, "Get pixelMap from rawData fail!");
98             object->value_[PIXEL_MAP] = rawData_;
99         } else {
100             object->value_[PIXEL_MAP] = pixelMap;
101         }
102         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
103         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
104         object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
105     }
106 }
107 
GetPixelMapFromRawData()108 std::shared_ptr<OHOS::Media::PixelMap> SystemDefinedPixelMap::GetPixelMapFromRawData()
109 {
110     if (rawData_.empty()) {
111         LOG_WARN(UDMF_KITS_INNER, "No RawData");
112         return nullptr;
113     }
114     if (rawData_.size() % BYTES_PER_COLOR != 0) {
115         LOG_ERROR(UDMF_KITS_INNER, "RawData size error, size = %{public}zu", rawData_.size());
116         return nullptr;
117     }
118     auto details = ObjectUtils::ConvertToObject(details_);
119     if (details == nullptr) {
120         LOG_ERROR(UDMF_KITS_INNER, "No details");
121         return nullptr;
122     }
123     PixelMapDetails pixelMapDetails;
124     pixelMapDetails.rawData = std::ref(rawData_);
125     pixelMapDetails.width = details->GetValue(PIXEL_MAP_WIDTH, pixelMapDetails.width) ? pixelMapDetails.width : -1;
126     pixelMapDetails.height = details->GetValue(PIXEL_MAP_HEIGHT, pixelMapDetails.height) ? pixelMapDetails.height : -1;
127     pixelMapDetails.pixelFormat = details->GetValue(PIXEL_MAP_FORMAT, pixelMapDetails.pixelFormat)
128         ? pixelMapDetails.pixelFormat : static_cast<int32_t>(OHOS::Media::PixelFormat::UNKNOWN);
129     PixelMapLoader loader;
130     return loader.GetPixelMapFromRawData(pixelMapDetails);
131 }
132 
ParseInfoFromPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)133 void SystemDefinedPixelMap::ParseInfoFromPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)
134 {
135     if (pixelMap == nullptr) {
136         LOG_ERROR(UDMF_KITS_INNER, "PixelMap is null");
137         return;
138     }
139     PixelMapLoader loader;
140     auto details = loader.ParseInfoFromPixelMap(pixelMap);
141     if (details == nullptr) {
142         LOG_ERROR(UDMF_KITS_INNER, "Parse info failed");
143         return;
144     }
145     details_[PIXEL_MAP_WIDTH] = details->width;
146     details_[PIXEL_MAP_HEIGHT] = details->height;
147     details_[PIXEL_MAP_FORMAT] = details->pixelFormat;
148     details_[PIXEL_MAP_ALPHA_TYPE] = details->alphaType;
149     if (!details->rawDataResult.has_value()) {
150         LOG_ERROR(UDMF_KITS_INNER, "No rawData");
151         return;
152     }
153     rawData_ = std::move(*details->rawDataResult);
154 }
155 } // namespace UDMF
156 } // namespace OHOS