1 /*
2 * Copyright (c) 2024 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 #include "pixel_map_impl.h"
16
17 #include "image_log.h"
18 #include "media_errors.h"
19
20 namespace OHOS {
21 namespace Media {
GetRealPixelMap()22 std::shared_ptr<PixelMap> PixelMapImpl::GetRealPixelMap()
23 {
24 return real_;
25 }
26
CreatePixelMap(const InitializationOptions & opts)27 std::unique_ptr<PixelMap> PixelMapImpl::CreatePixelMap(const InitializationOptions& opts)
28 {
29 if (opts.pixelFormat == PixelFormat::RGBA_1010102 || opts.pixelFormat == PixelFormat::YCBCR_P010 ||
30 opts.pixelFormat == PixelFormat::YCRCB_P010) {
31 return nullptr;
32 }
33 std::unique_ptr<PixelMap> ptr_ = PixelMap::Create(opts);
34 if (ptr_ == nullptr) {
35 IMAGE_LOGE("[PixelMapImpl] instance init failed!");
36 }
37 return ptr_;
38 }
39
CreatePixelMap(uint32_t * colors,uint32_t colorLength,InitializationOptions & opts)40 std::unique_ptr<PixelMap> PixelMapImpl::CreatePixelMap(
41 uint32_t* colors, uint32_t colorLength, InitializationOptions& opts)
42 {
43 if (opts.pixelFormat == PixelFormat::RGBA_1010102 || opts.pixelFormat == PixelFormat::YCBCR_P010 ||
44 opts.pixelFormat == PixelFormat::YCRCB_P010) {
45 return nullptr;
46 }
47 std::unique_ptr<PixelMap> ptr_ = PixelMap::Create(colors, colorLength, opts);
48 if (ptr_ == nullptr) {
49 IMAGE_LOGE("[PixelMapImpl] instance init failed!");
50 }
51 return ptr_;
52 }
53
CreateAlphaPixelMap(PixelMap & source,InitializationOptions & opts)54 std::unique_ptr<PixelMap> PixelMapImpl::CreateAlphaPixelMap(PixelMap& source, InitializationOptions& opts)
55 {
56 std::unique_ptr<PixelMap> ptr_ = PixelMap::Create(source, opts);
57 if (ptr_ == nullptr) {
58 IMAGE_LOGE("[PixelMapImpl] instance init failed!");
59 }
60 return ptr_;
61 }
62
CreatePremultipliedPixelMap(std::shared_ptr<PixelMap> src,std::shared_ptr<PixelMap> dst)63 uint32_t PixelMapImpl::CreatePremultipliedPixelMap(std::shared_ptr<PixelMap> src, std::shared_ptr<PixelMap> dst)
64 {
65 if (src == nullptr || dst == nullptr) {
66 return ERR_IMAGE_READ_PIXELMAP_FAILED;
67 } else {
68 bool isPremul = true;
69 if (dst->IsEditable()) {
70 return src->ConvertAlphaFormat(*dst.get(), isPremul);
71 } else {
72 return ERR_IMAGE_PIXELMAP_NOT_ALLOW_MODIFY;
73 }
74 }
75 }
76
CreateUnpremultipliedPixelMap(std::shared_ptr<PixelMap> src,std::shared_ptr<PixelMap> dst)77 uint32_t PixelMapImpl::CreateUnpremultipliedPixelMap(std::shared_ptr<PixelMap> src, std::shared_ptr<PixelMap> dst)
78 {
79 if (src == nullptr || dst == nullptr) {
80 return ERR_IMAGE_READ_PIXELMAP_FAILED;
81 } else {
82 bool isPremul = false;
83 if (dst->IsEditable()) {
84 return src->ConvertAlphaFormat(*dst.get(), isPremul);
85 } else {
86 return ERR_IMAGE_PIXELMAP_NOT_ALLOW_MODIFY;
87 }
88 }
89 }
90
PixelMapImpl(std::shared_ptr<PixelMap> ptr_)91 PixelMapImpl::PixelMapImpl(std::shared_ptr<PixelMap> ptr_)
92 {
93 real_ = ptr_;
94 }
95
ReadPixelsToBuffer(uint64_t & bufferSize,uint8_t * dst)96 uint32_t PixelMapImpl::ReadPixelsToBuffer(uint64_t& bufferSize, uint8_t* dst)
97 {
98 if (real_ == nullptr) {
99 return ERR_IMAGE_READ_PIXELMAP_FAILED;
100 }
101 return real_->ReadPixels(bufferSize, dst);
102 }
103
ReadPixels(uint64_t & bufferSize,uint32_t & offset,uint32_t & stride,Rect & region,uint8_t * dst)104 uint32_t PixelMapImpl::ReadPixels(uint64_t& bufferSize, uint32_t& offset, uint32_t& stride, Rect& region, uint8_t* dst)
105 {
106 if (real_ == nullptr) {
107 return ERR_IMAGE_READ_PIXELMAP_FAILED;
108 }
109 return real_->ReadPixels(bufferSize, offset, stride, region, dst);
110 }
111
WriteBufferToPixels(uint8_t * source,uint64_t & bufferSize)112 uint32_t PixelMapImpl::WriteBufferToPixels(uint8_t* source, uint64_t& bufferSize)
113 {
114 if (real_ == nullptr) {
115 return ERR_IMAGE_READ_PIXELMAP_FAILED;
116 }
117 return real_->WritePixels(source, bufferSize);
118 }
119
WritePixels(uint8_t * source,uint64_t & bufferSize,uint32_t & offset,uint32_t & stride,Rect & region)120 uint32_t PixelMapImpl::WritePixels(
121 uint8_t* source, uint64_t& bufferSize, uint32_t& offset, uint32_t& stride, Rect& region)
122 {
123 if (real_ == nullptr) {
124 return ERR_IMAGE_READ_PIXELMAP_FAILED;
125 }
126 return real_->WritePixels(source, bufferSize, offset, stride, region);
127 }
128
GetImageInfo(ImageInfo & imageInfo)129 void PixelMapImpl::GetImageInfo(ImageInfo& imageInfo)
130 {
131 if (real_ == nullptr) {
132 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
133 return;
134 }
135 real_->GetImageInfo(imageInfo);
136 }
137
GetDensity()138 int32_t PixelMapImpl::GetDensity()
139 {
140 if (real_ == nullptr) {
141 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
142 return 0;
143 }
144 return real_->GetBaseDensity();
145 }
146
Opacity(float percent)147 uint32_t PixelMapImpl::Opacity(float percent)
148 {
149 if (real_ == nullptr) {
150 return ERR_IMAGE_READ_PIXELMAP_FAILED;
151 }
152 return real_->SetAlpha(percent);
153 }
154
Scale(float xAxis,float yAxis)155 void PixelMapImpl::Scale(float xAxis, float yAxis)
156 {
157 if (real_ == nullptr) {
158 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
159 return;
160 }
161 real_->scale(xAxis, yAxis);
162 }
163
Scale(float xAxis,float yAxis,AntiAliasingOption option)164 void PixelMapImpl::Scale(float xAxis, float yAxis, AntiAliasingOption option)
165 {
166 if (real_ == nullptr) {
167 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
168 return;
169 }
170 real_->scale(xAxis, yAxis, option);
171 }
172
Crop(Rect & rect)173 uint32_t PixelMapImpl::Crop(Rect& rect)
174 {
175 if (real_ == nullptr) {
176 return ERR_IMAGE_READ_PIXELMAP_FAILED;
177 }
178 return real_->crop(rect);
179 }
180
ToSdr()181 uint32_t PixelMapImpl::ToSdr()
182 {
183 if (real_ == nullptr) {
184 return ERR_IMAGE_READ_PIXELMAP_FAILED;
185 }
186 if (!GetPixelMapImplEditable()) {
187 IMAGE_LOGE("ToSdrExec pixelmap is not editable");
188 return ERR_RESOURCE_UNAVAILABLE;
189 }
190 return real_->ToSdr();
191 }
192
Flip(bool xAxis,bool yAxis)193 void PixelMapImpl::Flip(bool xAxis, bool yAxis)
194 {
195 if (real_ == nullptr) {
196 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
197 return;
198 }
199 real_->flip(xAxis, yAxis);
200 }
201
Rotate(float degrees)202 void PixelMapImpl::Rotate(float degrees)
203 {
204 if (real_ == nullptr) {
205 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
206 return;
207 }
208 real_->rotate(degrees);
209 }
210
Translate(float xAxis,float yAxis)211 void PixelMapImpl::Translate(float xAxis, float yAxis)
212 {
213 if (real_ == nullptr) {
214 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
215 return;
216 }
217 real_->translate(xAxis, yAxis);
218 }
219
GetPixelBytesNumber()220 uint32_t PixelMapImpl::GetPixelBytesNumber()
221 {
222 if (real_ == nullptr) {
223 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
224 return 0;
225 }
226 return real_->GetByteCount();
227 }
228
GetBytesNumberPerRow()229 uint32_t PixelMapImpl::GetBytesNumberPerRow()
230 {
231 if (real_ == nullptr) {
232 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
233 return 0;
234 }
235 return real_->GetRowBytes();
236 }
237
GetIsEditable()238 bool PixelMapImpl::GetIsEditable()
239 {
240 if (real_ == nullptr) {
241 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
242 return false;
243 }
244 return real_->IsEditable();
245 }
246
GetIsStrideAlignment()247 bool PixelMapImpl::GetIsStrideAlignment()
248 {
249 if (real_ == nullptr) {
250 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
251 return false;
252 }
253 bool isDMA = real_->IsStrideAlignment();
254 return isDMA;
255 }
256
SetColorSpace(std::shared_ptr<OHOS::ColorManager::ColorSpace> colorSpace)257 uint32_t PixelMapImpl::SetColorSpace(std::shared_ptr<OHOS::ColorManager::ColorSpace> colorSpace)
258 {
259 #ifdef IMAGE_COLORSPACE_FLAG
260 if (real_ == nullptr || colorSpace == nullptr) {
261 return ERR_IMAGE_SOURCE_DATA_INCOMPLETE;
262 }
263 real_->InnerSetColorSpace(*colorSpace);
264 return 0;
265 #else
266 return ERR_IMAGE_SOURCE_DATA_INCOMPLETE;
267 #endif
268 }
269
GetColorSpace()270 std::shared_ptr<OHOS::ColorManager::ColorSpace> PixelMapImpl::GetColorSpace()
271 {
272 #ifdef IMAGE_COLORSPACE_FLAG
273 if (real_ == nullptr) {
274 IMAGE_LOGE("[PixelMapImpl] real_ is nullptr!");
275 return nullptr;
276 }
277 auto colorSpace = real_->InnerGetGrColorSpacePtr();
278 return colorSpace;
279 #else
280 return nullptr;
281 #endif
282 }
283
ApplyColorSpace(std::shared_ptr<OHOS::ColorManager::ColorSpace> colorSpace)284 uint32_t PixelMapImpl::ApplyColorSpace(std::shared_ptr<OHOS::ColorManager::ColorSpace> colorSpace)
285 {
286 if (real_ == nullptr || colorSpace == nullptr) {
287 return ERR_IMAGE_READ_PIXELMAP_FAILED;
288 }
289 return real_->ApplyColorSpace(*colorSpace);
290 }
291 } // namespace Media
292 } // namespace OHOS