1 /*
2 * Copyright (c) 2021 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
16 #include "pixel_map_ohos.h"
17
18 #include "base/log/log_wrapper.h"
19
20 namespace OHOS::Ace {
21
PixelFormatConverter(Media::PixelFormat pixelFormat)22 PixelFormat PixelMapOhos::PixelFormatConverter(Media::PixelFormat pixelFormat)
23 {
24 switch (pixelFormat) {
25 case Media::PixelFormat::RGB_565:
26 return PixelFormat::RGB_565;
27 case Media::PixelFormat::RGBA_8888:
28 return PixelFormat::RGBA_8888;
29 case Media::PixelFormat::BGRA_8888:
30 return PixelFormat::BGRA_8888;
31 case Media::PixelFormat::ALPHA_8:
32 return PixelFormat::ALPHA_8;
33 case Media::PixelFormat::RGBA_F16:
34 return PixelFormat::RGBA_F16;
35 case Media::PixelFormat::UNKNOWN:
36 return PixelFormat::UNKNOWN;
37 case Media::PixelFormat::ARGB_8888:
38 return PixelFormat::ARGB_8888;
39 case Media::PixelFormat::RGB_888:
40 return PixelFormat::RGB_888;
41 case Media::PixelFormat::NV21:
42 return PixelFormat::NV21;
43 case Media::PixelFormat::NV12:
44 return PixelFormat::NV12;
45 case Media::PixelFormat::CMYK:
46 return PixelFormat::CMYK;
47 default:
48 return PixelFormat::UNKNOWN;
49 }
50 }
51
AlphaTypeConverter(Media::AlphaType alphaType)52 AlphaType PixelMapOhos::AlphaTypeConverter(Media::AlphaType alphaType)
53 {
54 switch (alphaType) {
55 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
56 return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
57 case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
58 return AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
59 case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
60 return AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
61 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
62 return AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
63 default:
64 return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
65 }
66 }
67
CreatePixelMap(void * rawPtr)68 RefPtr<PixelMap> PixelMap::CreatePixelMap(void* rawPtr)
69 {
70 std::shared_ptr<Media::PixelMap>* pixmapPtr = reinterpret_cast<std::shared_ptr<Media::PixelMap>*>(rawPtr);
71 if (pixmapPtr == nullptr || *pixmapPtr == nullptr) {
72 LOGW("pixmap pointer is nullptr when CreatePixelMap.");
73 return nullptr;
74 }
75 return AceType::MakeRefPtr<PixelMapOhos>(*pixmapPtr);
76 }
77
CreatePixelMapFromDataAbility(void * uniquePtr)78 RefPtr<PixelMap> PixelMap::CreatePixelMapFromDataAbility(void* uniquePtr)
79 {
80 std::unique_ptr<Media::PixelMap>* pixmapPtr = reinterpret_cast<std::unique_ptr<Media::PixelMap>*>(uniquePtr);
81 if (pixmapPtr == nullptr || *pixmapPtr == nullptr) {
82 LOGW("pixmap pointer is nullptr when CreatePixelMapFromDataAbility.");
83 return nullptr;
84 }
85 auto rawPtr = (*pixmapPtr).release();
86 return AceType::MakeRefPtr<PixelMapOhos>(std::shared_ptr<Media::PixelMap>(rawPtr));
87 }
88
GetWidth() const89 int32_t PixelMapOhos::GetWidth() const
90 {
91 if (!pixmap_) {
92 LOGE("pixmap is nullptr, return default width.");
93 return 0;
94 }
95 return pixmap_->GetWidth();
96 }
97
GetHeight() const98 int32_t PixelMapOhos::GetHeight() const
99 {
100 if (!pixmap_) {
101 LOGE("pixmap is nullptr, return default height.");
102 return 0;
103 }
104 return pixmap_->GetHeight();
105 }
106
GetPixels() const107 const uint8_t* PixelMapOhos::GetPixels() const
108 {
109 if (!pixmap_) {
110 LOGE("pixmap is nullptr, return default address of pixels.");
111 return nullptr;
112 }
113 return pixmap_->GetPixels();
114 }
115
GetPixelFormat() const116 PixelFormat PixelMapOhos::GetPixelFormat() const
117 {
118 if (!pixmap_) {
119 LOGE("pixmap is nullptr, return default pixel format.");
120 return PixelFormat::UNKNOWN;
121 }
122 return PixelFormatConverter(pixmap_->GetPixelFormat());
123 }
124
GetAlphaType() const125 AlphaType PixelMapOhos::GetAlphaType() const
126 {
127 if (!pixmap_) {
128 LOGE("pixmap is nullptr, return default alpha type.");
129 return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
130 }
131 return AlphaTypeConverter(pixmap_->GetAlphaType());
132 }
133
GetRowBytes() const134 int32_t PixelMapOhos::GetRowBytes() const
135 {
136 if (!pixmap_) {
137 LOGE("pixmap is nullptr, return default row bytes.");
138 return 0;
139 }
140 return pixmap_->GetRowBytes();
141 }
142
GetByteCount() const143 int32_t PixelMapOhos::GetByteCount() const
144 {
145 if (!pixmap_) {
146 return 0;
147 }
148 return pixmap_->GetByteCount();
149 }
150
GetPixelManager() const151 void* PixelMapOhos::GetPixelManager() const
152 {
153 Media::InitializationOptions opts;
154 if (!pixmap_) {
155 return nullptr;
156 }
157 auto newPixelMap = Media::PixelMap::Create(*pixmap_, opts);
158 return reinterpret_cast<void*>(new Media::PixelMapManager(newPixelMap.release()));
159 }
160
GetRawPixelMapPtr() const161 void* PixelMapOhos::GetRawPixelMapPtr() const
162 {
163 if (!pixmap_) {
164 LOGE("pixmap is nullptr, raw pixel map pointer is nullptr.");
165 return nullptr;
166 }
167 return pixmap_.get();
168 }
169
GetId()170 std::string PixelMapOhos::GetId()
171 {
172 // TODO: media should generate Id of each [PixelMap] to distinguish different objects
173 return std::string();
174 }
175
GetModifyId()176 std::string PixelMapOhos::GetModifyId()
177 {
178 // TODO: media should generate ModifyId of [PixelMap] to distinguish [PixelMap] object after different operations
179 return std::string();
180 }
181
182 } // namespace OHOS::Ace