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