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 
16 #include "pixel_astc.h"
17 
18 #include "image_log.h"
19 #include "image_utils.h"
20 #include "image_trace.h"
21 #include "image_type_converter.h"
22 #include "memory_manager.h"
23 #include "include/core/SkBitmap.h"
24 #include "include/core/SkCanvas.h"
25 #include "include/core/SkImage.h"
26 #include "hitrace_meter.h"
27 #include "media_errors.h"
28 #include "pubdef.h"
29 
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
32 
33 #undef LOG_TAG
34 #define LOG_TAG "PixelAstc"
35 
36 namespace OHOS {
37 namespace Media {
38 using namespace std;
39 
~PixelAstc()40 PixelAstc::~PixelAstc()
41 {
42     IMAGE_LOGD("PixelAstc destory");
43     FreePixelMap();
44 }
45 
GetPixel8(int32_t x,int32_t y)46 const uint8_t *PixelAstc::GetPixel8(int32_t x, int32_t y)
47 {
48     IMAGE_LOGE("GetPixel8 is not support on pixelastc");
49     return nullptr;
50 }
51 
GetPixel16(int32_t x,int32_t y)52 const uint16_t *PixelAstc::GetPixel16(int32_t x, int32_t y)
53 {
54     IMAGE_LOGE("GetPixel16 is not support on pixelastc");
55     return nullptr;
56 }
57 
GetPixel32(int32_t x,int32_t y)58 const uint32_t *PixelAstc::GetPixel32(int32_t x, int32_t y)
59 {
60     IMAGE_LOGE("GetPixel32 is not support on pixelastc");
61     return nullptr;
62 }
63 
GetARGB32Color(int32_t x,int32_t y,uint32_t & color)64 bool PixelAstc::GetARGB32Color(int32_t x, int32_t y, uint32_t &color)
65 {
66     IMAGE_LOGE("GetARGB32Color is not support on pixelastc");
67     return false;
68 }
69 
scale(float xAxis,float yAxis)70 void PixelAstc::scale(float xAxis, float yAxis)
71 {
72     if (xAxis == 0 || yAxis == 0) {
73         IMAGE_LOGE("scale param incorrect on pixelastc");
74         return;
75     } else {
76         TransformData transformData;
77         GetTransformData(transformData);
78         transformData.scaleX *= xAxis;
79         transformData.scaleY *= yAxis;
80         SetTransformData(transformData);
81         ImageInfo imageInfo;
82         GetImageInfo(imageInfo);
83         imageInfo.size.width *= abs(xAxis);
84         imageInfo.size.height *= abs(yAxis);
85         SetImageInfo(imageInfo, true);
86     }
87 }
88 
resize(float xAxis,float yAxis)89 bool PixelAstc::resize(float xAxis, float yAxis)
90 {
91     IMAGE_LOGE("resize is not support on pixelastc");
92     return false;
93 }
94 
translate(float xAxis,float yAxis)95 void PixelAstc::translate(float xAxis, float yAxis)
96 {
97     TransformData transformData;
98     GetTransformData(transformData);
99     transformData.translateX = xAxis;
100     transformData.translateY = yAxis;
101     SetTransformData(transformData);
102 }
103 
rotate(float degrees)104 void PixelAstc::rotate(float degrees)
105 {
106     TransformData transformData;
107     GetTransformData(transformData);
108     transformData.rotateD = degrees;
109     SetTransformData(transformData);
110 }
111 
flip(bool xAxis,bool yAxis)112 void PixelAstc::flip(bool xAxis, bool yAxis)
113 {
114     TransformData transformData;
115     GetTransformData(transformData);
116     transformData.flipX = xAxis;
117     transformData.flipY = yAxis;
118     SetTransformData(transformData);
119 }
120 
crop(const Rect & rect)121 uint32_t PixelAstc::crop(const Rect &rect)
122 {
123     ImageInfo imageInfo;
124     GetImageInfo(imageInfo);
125     if (rect.left >= 0 && rect.top >= 0 && rect.width > 0 && rect.height > 0 &&
126         rect.left + rect.width <= imageInfo.size.width &&
127         rect.top + rect.height <= imageInfo.size.height) {
128         TransformData transformData;
129         GetTransformData(transformData);
130         transformData.cropLeft = rect.left;
131         transformData.cropTop = rect.top;
132         transformData.cropWidth = rect.width;
133         transformData.cropHeight = rect.height;
134         SetTransformData(transformData);
135         imageInfo.size.width = rect.width;
136         imageInfo.size.height = rect.height;
137         SetImageInfo(imageInfo, true);
138     } else {
139         IMAGE_LOGE("crop failed");
140         return ERR_IMAGE_CROP;
141     }
142     return SUCCESS;
143 }
144 
SetAlpha(const float percent)145 uint32_t PixelAstc::SetAlpha(const float percent)
146 {
147     IMAGE_LOGE("SetAlpha is not support on pixelastc");
148     return ERR_IMAGE_DATA_UNSUPPORT;
149 }
150 
GetARGB32ColorA(uint32_t color)151 uint8_t PixelAstc::GetARGB32ColorA(uint32_t color)
152 {
153     IMAGE_LOGE("GetARGB32ColorA is not support on pixelastc");
154     return 0;
155 }
156 
GetARGB32ColorR(uint32_t color)157 uint8_t PixelAstc::GetARGB32ColorR(uint32_t color)
158 {
159     IMAGE_LOGE("GetARGB32ColorR is not support on pixelastc");
160     return 0;
161 }
162 
GetARGB32ColorG(uint32_t color)163 uint8_t PixelAstc::GetARGB32ColorG(uint32_t color)
164 {
165     IMAGE_LOGE("GetARGB32ColorG is not support on pixelastc");
166     return 0;
167 }
168 
GetARGB32ColorB(uint32_t color)169 uint8_t PixelAstc::GetARGB32ColorB(uint32_t color)
170 {
171     IMAGE_LOGE("GetARGB32ColorB is not support on pixelastc");
172     return 0;
173 }
174 
IsSameImage(const PixelMap & other)175 bool PixelAstc::IsSameImage(const PixelMap &other)
176 {
177     IMAGE_LOGE("IsSameImage is not support on pixelastc");
178     return false;
179 }
180 
ReadPixels(const uint64_t & bufferSize,const uint32_t & offset,const uint32_t & stride,const Rect & region,uint8_t * dst)181 uint32_t PixelAstc::ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride,
182                                const Rect &region, uint8_t *dst)
183 {
184     IMAGE_LOGE("ReadPixels is not support on pixelastc");
185     return ERR_IMAGE_INVALID_PARAMETER;
186 }
187 
ReadPixels(const uint64_t & bufferSize,uint8_t * dst)188 uint32_t PixelAstc::ReadPixels(const uint64_t &bufferSize, uint8_t *dst)
189 {
190     IMAGE_LOGE("ReadPixels is not support on pixelastc");
191     return ERR_IMAGE_INVALID_PARAMETER;
192 }
193 
ReadPixel(const Position & pos,uint32_t & dst)194 uint32_t PixelAstc::ReadPixel(const Position &pos, uint32_t &dst)
195 {
196     IMAGE_LOGE("ReadPixel is not support on pixelastc");
197     return ERR_IMAGE_INVALID_PARAMETER;
198 }
199 
ResetConfig(const Size & size,const PixelFormat & format)200 uint32_t PixelAstc::ResetConfig(const Size &size, const PixelFormat &format)
201 {
202     IMAGE_LOGE("ResetConfig is not support on pixelastc");
203     return ERR_IMAGE_INVALID_PARAMETER;
204 }
205 
SetAlphaType(const AlphaType & alphaType)206 bool PixelAstc::SetAlphaType(const AlphaType &alphaType)
207 {
208     IMAGE_LOGE("SetAlphaType is not support on pixelastc");
209     return false;
210 }
211 
WritePixel(const Position & pos,const uint32_t & color)212 uint32_t PixelAstc::WritePixel(const Position &pos, const uint32_t &color)
213 {
214     IMAGE_LOGE("WritePixel is not support on pixelastc");
215     return ERR_IMAGE_INVALID_PARAMETER;
216 }
217 
WritePixels(const uint8_t * source,const uint64_t & bufferSize,const uint32_t & offset,const uint32_t & stride,const Rect & region)218 uint32_t PixelAstc::WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset,
219                                 const uint32_t &stride, const Rect &region)
220 {
221     IMAGE_LOGE("WritePixels is not support on pixelastc");
222     return ERR_IMAGE_INVALID_PARAMETER;
223 }
224 
WritePixels(const uint8_t * source,const uint64_t & bufferSize)225 uint32_t PixelAstc::WritePixels(const uint8_t *source, const uint64_t &bufferSize)
226 {
227     IMAGE_LOGE("WritePixels is not support on pixelastc");
228     return ERR_IMAGE_INVALID_PARAMETER;
229 }
230 
WritePixels(const uint32_t & color)231 bool PixelAstc::WritePixels(const uint32_t &color)
232 {
233     IMAGE_LOGE("WritePixels is not support on pixelastc");
234     return false;
235 }
236 
SetTransformered(bool isTransformered)237 void PixelAstc::SetTransformered(bool isTransformered)
238 {
239     IMAGE_LOGE("SetTransformered is not support on pixelastc");
240 }
241 
IsTransformered()242 bool PixelAstc::IsTransformered()
243 {
244     IMAGE_LOGE("IsTransformered is not support on pixelastc");
245     return false;
246 }
247 
SetRowStride(uint32_t stride)248 void PixelAstc::SetRowStride(uint32_t stride)
249 {
250     IMAGE_LOGE("SetRowStride is not support on pixelastc");
251 }
252 
GetRowStride()253 int32_t PixelAstc::GetRowStride()
254 {
255     IMAGE_LOGD("GetRowStride is not support on pixelastc");
256     return 0;
257 }
258 
IsSourceAsResponse()259 bool PixelAstc::IsSourceAsResponse()
260 {
261     IMAGE_LOGE("IsSourceAsResponse is not support on pixelastc");
262     return false;
263 }
264 
GetWritablePixels() const265 void* PixelAstc::GetWritablePixels() const
266 {
267     IMAGE_LOGE("GetWritablePixels is not support on pixelastc");
268     return nullptr;
269 }
270 } // namespace Media
271 } // namespace OHOS