1 /*
2 * Copyright (c) 2025 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 "PixelmapWrapper"
16 #include "pixelmap_wrapper.h"
17
18 #include "logger.h"
19 #include "media_errors.h"
20 #include "pixel_map.h"
21
22 using namespace OHOS::UDMF;
23
24 static constexpr size_t BYTES_PER_COLOR = sizeof(uint32_t);
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
DecodeTlv(const PixelMapDetails details)30 OHOS::Media::PixelMap *DecodeTlv(const PixelMapDetails details)
31 {
32 return OHOS::Media::PixelMap::DecodeTlv(details.rawData->get());
33 }
34
EncodeTlv(const OHOS::Media::PixelMap * pixelMap,PixelMapDetails * details)35 bool EncodeTlv(const OHOS::Media::PixelMap *pixelMap, PixelMapDetails *details)
36 {
37 if (details == nullptr) {
38 LOG_ERROR(UDMF_KITS_INNER, "details is null");
39 return false;
40 }
41 auto &rawData = *(details->rawDataResult);
42 return pixelMap->EncodeTlv(rawData);
43 }
44
GetPixelMapFromRawData(const PixelMapDetails details)45 OHOS::Media::PixelMap *GetPixelMapFromRawData(const PixelMapDetails details)
46 {
47 OHOS::Media::InitializationOptions opts;
48 opts.size.width = details.width;
49 opts.size.height = details.height;
50 opts.pixelFormat = static_cast<OHOS::Media::PixelFormat>(details.pixelFormat);
51 if (!details.rawData.has_value()) {
52 return nullptr;
53 }
54 // This create does not do pre-multiplication.
55 opts.alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
56 LOG_INFO(UDMF_KITS_INNER,
57 "PixelMap width = %{public}d, height = %{public}d, pixelFormat = %{public}d, rawData size = %{public}zu",
58 opts.size.width, opts.size.height, opts.pixelFormat, details.
59 rawData->get().size());
60 opts.srcPixelFormat = opts.pixelFormat;
61 auto pixelMap = OHOS::Media::PixelMap::Create(
62 reinterpret_cast<const uint32_t *>(details.rawData->get().data()),
63 details.rawData->get().size() / BYTES_PER_COLOR, opts);
64 if (pixelMap == nullptr) {
65 LOG_ERROR(UDMF_KITS_INNER, "Create PixelMap from rawData failed");
66 return nullptr;
67 }
68 return pixelMap.release();
69 }
70
ParseInfoFromPixelMap(OHOS::Media::PixelMap * pixelMap)71 PixelMapDetails *ParseInfoFromPixelMap(OHOS::Media::PixelMap *pixelMap)
72 {
73 if (pixelMap == nullptr) {
74 LOG_ERROR(UDMF_KITS_INNER, "PixelMap is null");
75 return nullptr;
76 }
77 auto details = new (std::nothrow) PixelMapDetails;
78 if (details == nullptr) {
79 LOG_ERROR(UDMF_KITS_INNER, "Apply memory for details failed");
80 return nullptr;
81 }
82 details->width = pixelMap->GetWidth();
83 details->height = pixelMap->GetHeight();
84 details->pixelFormat = static_cast<int32_t>(pixelMap->GetPixelFormat());
85 details->alphaType = static_cast<int32_t>(pixelMap->GetAlphaType());
86 auto length = pixelMap->GetByteCount();
87 if (length <= 0) {
88 LOG_ERROR(UDMF_KITS_INNER, "Has no length");
89 return details;
90 }
91 std::vector<uint8_t> rawData;
92 rawData.resize(length);
93 auto status = pixelMap->ReadPixels(length, rawData.data());
94 if (status != OHOS::Media::SUCCESS) {
95 LOG_ERROR(UDMF_KITS_INNER, "Get Pixels error, status = %{public}u", status);
96 return details;
97 }
98 details->rawDataResult.emplace(std::move(rawData));
99 return details;
100 }
101
102 #ifdef __cplusplus
103 };
104 #endif