• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ImageInfo imageInfo;
102     GetImageInfo(imageInfo);
103     imageInfo.size.width += static_cast<int32_t>(xAxis);
104     imageInfo.size.height += static_cast<int32_t>(yAxis);
105     if (imageInfo.size.width <= 0 || imageInfo.size.height <= 0) {
106         IMAGE_LOGE("PixelAstc translate failed");
107         return;
108     }
109     SetTransformData(transformData);
110     SetImageInfo(imageInfo, true);
111 }
112 
calculateRotatedDimensions(float width,float height,float rotationDegrees)113 std::pair<float, float> calculateRotatedDimensions(float width, float height, float rotationDegrees)
114 {
115     float radians = rotationDegrees * M_PI / 180.0f;
116 
117     float cosTheta = std::cos(radians);
118     float sinTheta = std::sin(radians);
119 
120     float newWidth = std::abs(width * cosTheta) + std::abs(height * sinTheta);
121     float newHeight = std::abs(width * sinTheta) + std::abs(height * cosTheta);
122     return {newWidth, newHeight};
123 }
124 
rotate(float degrees)125 void PixelAstc::rotate(float degrees)
126 {
127     TransformData transformData;
128     GetTransformData(transformData);
129     transformData.rotateD += degrees;
130     transformData.rotateD = fmod(fmod(transformData.rotateD, 360.0f) + 360.0f, 360.0f); // Normalize to [0, 360)
131     SetTransformData(transformData);
132     ImageInfo imageInfo;
133     GetImageInfo(imageInfo);
134     auto newDimensions = calculateRotatedDimensions(imageInfo.size.width, imageInfo.size.height, degrees);
135     imageInfo.size.width = static_cast<int32_t>(newDimensions.first);
136     imageInfo.size.height = static_cast<int32_t>(newDimensions.second);
137     SetImageInfo(imageInfo, true);
138 }
139 
flip(bool xAxis,bool yAxis)140 void PixelAstc::flip(bool xAxis, bool yAxis)
141 {
142     TransformData transformData;
143     GetTransformData(transformData);
144     transformData.flipX = xAxis;
145     transformData.flipY = yAxis;
146     SetTransformData(transformData);
147 }
148 
crop(const Rect & rect)149 uint32_t PixelAstc::crop(const Rect &rect)
150 {
151     ImageInfo imageInfo;
152     GetImageInfo(imageInfo);
153     if (rect.left >= 0 && rect.top >= 0 && rect.width > 0 && rect.height > 0 &&
154         rect.left + rect.width <= imageInfo.size.width &&
155         rect.top + rect.height <= imageInfo.size.height) {
156         TransformData transformData;
157         GetTransformData(transformData);
158         transformData.cropLeft = rect.left;
159         transformData.cropTop = rect.top;
160         transformData.cropWidth = rect.width;
161         transformData.cropHeight = rect.height;
162         SetTransformData(transformData);
163         imageInfo.size.width = rect.width;
164         imageInfo.size.height = rect.height;
165         SetImageInfo(imageInfo, true);
166     } else {
167         IMAGE_LOGE("crop failed");
168         return ERR_IMAGE_CROP;
169     }
170     return SUCCESS;
171 }
172 
SetAlpha(const float percent)173 uint32_t PixelAstc::SetAlpha(const float percent)
174 {
175     IMAGE_LOGE("SetAlpha is not support on pixelastc");
176     return ERR_IMAGE_DATA_UNSUPPORT;
177 }
178 
GetARGB32ColorA(uint32_t color)179 uint8_t PixelAstc::GetARGB32ColorA(uint32_t color)
180 {
181     IMAGE_LOGE("GetARGB32ColorA is not support on pixelastc");
182     return 0;
183 }
184 
GetARGB32ColorR(uint32_t color)185 uint8_t PixelAstc::GetARGB32ColorR(uint32_t color)
186 {
187     IMAGE_LOGE("GetARGB32ColorR is not support on pixelastc");
188     return 0;
189 }
190 
GetARGB32ColorG(uint32_t color)191 uint8_t PixelAstc::GetARGB32ColorG(uint32_t color)
192 {
193     IMAGE_LOGE("GetARGB32ColorG is not support on pixelastc");
194     return 0;
195 }
196 
GetARGB32ColorB(uint32_t color)197 uint8_t PixelAstc::GetARGB32ColorB(uint32_t color)
198 {
199     IMAGE_LOGE("GetARGB32ColorB is not support on pixelastc");
200     return 0;
201 }
202 
IsSameImage(const PixelMap & other)203 bool PixelAstc::IsSameImage(const PixelMap &other)
204 {
205     IMAGE_LOGE("IsSameImage is not support on pixelastc");
206     return false;
207 }
208 
ReadPixels(const uint64_t & bufferSize,const uint32_t & offset,const uint32_t & stride,const Rect & region,uint8_t * dst)209 uint32_t PixelAstc::ReadPixels(const uint64_t &bufferSize, const uint32_t &offset, const uint32_t &stride,
210                                const Rect &region, uint8_t *dst)
211 {
212     IMAGE_LOGE("ReadPixels is not support on pixelastc");
213     return ERR_IMAGE_INVALID_PARAMETER;
214 }
215 
ReadPixels(const uint64_t & bufferSize,uint8_t * dst)216 uint32_t PixelAstc::ReadPixels(const uint64_t &bufferSize, uint8_t *dst)
217 {
218     IMAGE_LOGE("ReadPixels is not support on pixelastc");
219     return ERR_IMAGE_INVALID_PARAMETER;
220 }
221 
ReadPixel(const Position & pos,uint32_t & dst)222 uint32_t PixelAstc::ReadPixel(const Position &pos, uint32_t &dst)
223 {
224     IMAGE_LOGE("ReadPixel is not support on pixelastc");
225     return ERR_IMAGE_INVALID_PARAMETER;
226 }
227 
ResetConfig(const Size & size,const PixelFormat & format)228 uint32_t PixelAstc::ResetConfig(const Size &size, const PixelFormat &format)
229 {
230     IMAGE_LOGE("ResetConfig is not support on pixelastc");
231     return ERR_IMAGE_INVALID_PARAMETER;
232 }
233 
SetAlphaType(const AlphaType & alphaType)234 bool PixelAstc::SetAlphaType(const AlphaType &alphaType)
235 {
236     IMAGE_LOGE("SetAlphaType is not support on pixelastc");
237     return false;
238 }
239 
WritePixel(const Position & pos,const uint32_t & color)240 uint32_t PixelAstc::WritePixel(const Position &pos, const uint32_t &color)
241 {
242     IMAGE_LOGE("WritePixel is not support on pixelastc");
243     return ERR_IMAGE_INVALID_PARAMETER;
244 }
245 
WritePixels(const uint8_t * source,const uint64_t & bufferSize,const uint32_t & offset,const uint32_t & stride,const Rect & region)246 uint32_t PixelAstc::WritePixels(const uint8_t *source, const uint64_t &bufferSize, const uint32_t &offset,
247                                 const uint32_t &stride, const Rect &region)
248 {
249     IMAGE_LOGE("WritePixels is not support on pixelastc");
250     return ERR_IMAGE_INVALID_PARAMETER;
251 }
252 
WritePixels(const uint8_t * source,const uint64_t & bufferSize)253 uint32_t PixelAstc::WritePixels(const uint8_t *source, const uint64_t &bufferSize)
254 {
255     IMAGE_LOGE("WritePixels is not support on pixelastc");
256     return ERR_IMAGE_INVALID_PARAMETER;
257 }
258 
WritePixels(const uint32_t & color)259 bool PixelAstc::WritePixels(const uint32_t &color)
260 {
261     IMAGE_LOGE("WritePixels is not support on pixelastc");
262     return false;
263 }
264 
SetTransformered(bool isTransformered)265 void PixelAstc::SetTransformered(bool isTransformered)
266 {
267     IMAGE_LOGE("SetTransformered is not support on pixelastc");
268 }
269 
IsTransformered()270 bool PixelAstc::IsTransformered()
271 {
272     IMAGE_LOGE("IsTransformered is not support on pixelastc");
273     return false;
274 }
275 
IsSourceAsResponse()276 bool PixelAstc::IsSourceAsResponse()
277 {
278     IMAGE_LOGE("IsSourceAsResponse is not support on pixelastc");
279     return false;
280 }
281 
GetWritablePixels() const282 void* PixelAstc::GetWritablePixels() const
283 {
284     IMAGE_LOGE("GetWritablePixels is not support on pixelastc");
285     return nullptr;
286 }
287 } // namespace Media
288 } // namespace OHOS