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 #ifndef FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_BASIC_TRANSFORMER_H
17 #define FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_BASIC_TRANSFORMER_H
18
19 #include <algorithm>
20 #include <string>
21
22 #include "image_log.h"
23 #include "image_type.h"
24 #include "matrix.h"
25
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
28
29 #undef LOG_TAG
30 #define LOG_TAG "BasicTransformer"
31
32 static constexpr uint32_t IMAGE_SUCCESS = 0; // success
33 static constexpr uint32_t IMAGE_BASE_ERROR = 1000; // base error
34 static constexpr uint32_t ERR_IMAGE_GENERAL_ERROR = IMAGE_BASE_ERROR + 1; // general error
35 static constexpr uint32_t ERR_IMAGE_INVALID_PIXEL = IMAGE_BASE_ERROR + 2; // invalid pixel
36 static constexpr uint32_t ERR_IMAGE_MATRIX_NOT_INVERT = IMAGE_BASE_ERROR + 3; // matrix can not invert
37 static constexpr uint32_t ERR_IMAGE_ALLOC_MEMORY_FAILED = IMAGE_BASE_ERROR + 4; // alloc memory failed
38
39 static constexpr float FHALF = 0.5f;
40 static constexpr uint32_t BASIC = 1 << 16;
41 static constexpr uint32_t HALF_BASIC = 1 << 15;
42 static constexpr float MULTI_65536 = 65536.0f;
43 static constexpr uint32_t SUB_VALUE_SHIFT = 12;
44 static constexpr uint8_t COLOR_DEFAULT = 0;
45 static constexpr int32_t RGB888_BYTE = 3;
46
47 namespace OHOS {
48 namespace Media {
49 struct BilinearPixelProcArgs;
50 using BilinearPixelProcArgs = struct BilinearPixelProcArgs;
51
CheckOutOfRange(const Point & pt,const Size & size)52 static inline bool CheckOutOfRange(const Point &pt, const Size &size)
53 {
54 if ((pt.x >= 0) && (pt.x < size.width) && (pt.y >= 0) && (pt.y < size.height)) {
55 return false;
56 }
57 return true;
58 }
59
ClampMax(int value,int max)60 static inline int32_t ClampMax(int value, int max)
61 {
62 if (value > max) {
63 value = max;
64 }
65 return (value > 0) ? value : 0;
66 }
67
GetSubValue(int32_t value)68 static inline uint32_t GetSubValue(int32_t value)
69 {
70 // In order to speed up the calculation, use offset
71 return ((value >> SUB_VALUE_SHIFT) & 0xF);
72 }
73
74 struct PixmapInfo {
75 ImageInfo imageInfo;
76 uint8_t *data = nullptr;
77 uint32_t bufferSize = 0;
78 bool isAutoDestruct = true;
79 int32_t *context = nullptr;
80 uint32_t uniqueId = -1;
PixmapInfoPixmapInfo81 PixmapInfo(){}
82
~PixmapInfoPixmapInfo83 ~PixmapInfo()
84 {
85 if (isAutoDestruct) {
86 if (data != nullptr) {
87 free(data);
88 data = nullptr;
89 }
90 }
91 }
92
PixmapInfoPixmapInfo93 explicit PixmapInfo(bool isAuto)
94 {
95 isAutoDestruct = isAuto;
96 }
PixmapInfoPixmapInfo97 explicit PixmapInfo(const PixmapInfo &src)
98 {
99 Init(src);
100 }
101
InitPixmapInfo102 void Init(const PixmapInfo &src)
103 {
104 imageInfo = src.imageInfo;
105 data = nullptr;
106 bufferSize = 0;
107 }
108
DestroyPixmapInfo109 void Destroy()
110 {
111 if (data != nullptr) {
112 free(data);
113 data = nullptr;
114 }
115 }
116
PrintPixmapInfoPixmapInfo117 void PrintPixmapInfo(const std::string &strFlag) const
118 {
119 IMAGE_LOGD("[PixmapInfo][%{public}s][width, height:%{public}d, %{public}d]"
120 "[bufferSize:%{public}u][pixelFormat:%{public}d].", strFlag.c_str(), imageInfo.size.width,
121 imageInfo.size.height, bufferSize, static_cast<int32_t>(imageInfo.pixelFormat));
122 }
123 };
124
125 class BasicTransformer {
126 public:
127 using AllocateMem = uint8_t *(*)(const Size &size, const uint64_t bufferSize, int &fd, uint32_t uniqueId);
128
BasicTransformer()129 BasicTransformer()
130 {
131 ResetParam();
132 }
~BasicTransformer()133 ~BasicTransformer(){}
134
135 // Reset pixel map info transform param, back to the original state
136 void ResetParam();
137
138 // Reserved interface
139 void SetTranslateParam(const float tx, const float ty);
140
141 void SetScaleParam(const float sx, const float sy);
142
143 /** Set rotates param by degrees about a point at (px, py). Positive degrees rotates
144 * clockwise.
145 *
146 * @param degrees amount to rotate, in degrees
147 * @param px x-axis value of the point to rotate about
148 * @param py y-axis value of the point to rotate about
149 */
150 void SetRotateParam(const float degrees, const float px = 0.0f, const float py = 0.0f);
151
152 /**
153 * Transform pixel map info. before transform, you should set pixel transform param first.
154 * @param inPixmap The input pixel map info
155 * @param outPixmap The output pixel map info, the pixelFormat and colorSpace same as the inPixmap
156 * @param allocate This is func pointer, if it is null, this function will new heap memory,
157 * so you must active release memory, if it is not null, that means you need allocate memory by yourself,
158 * so you should invoke GetDstWH function to get the dest width and height,
159 * then fill to outPixmap's width and height.
160 * @return the error no
161 */
162 uint32_t TransformPixmap(const PixmapInfo &inPixmap, PixmapInfo &outPixmap, AllocateMem allocate = nullptr);
163
164 void GetDstDimension(const Size &srcSize, Size &dstSize);
165
166 private:
167 struct AroundPixels {
168 uint32_t color00 = 0;
169 uint32_t color01 = 0;
170 uint32_t color10 = 0;
171 uint32_t color11 = 0;
172 };
173 struct AroundPos {
174 uint32_t x0 = 0;
175 uint32_t x1 = 0;
176 uint32_t y0 = 0;
177 uint32_t y1 = 0;
178 };
179
180 uint32_t RightShift16Bit(uint32_t num, int32_t maxNum);
181
182 void GetRotateDimension(Matrix::CalcXYProc fInvProc, const Size &srcSize, Size &dstSize);
183
184 bool DrawPixelmap(const PixmapInfo &pixmapInfo, const int32_t pixelBytes, const Size &size, uint8_t *data);
185
186 bool CheckAllocateBuffer(PixmapInfo &outPixmap, AllocateMem allocate, int &fd, uint64_t &bufferSize, Size &dstSize);
187
188 void BilinearProc(const Point &pt, const PixmapInfo &pixmapInfo, const uint32_t rb, const int32_t shiftBytes,
189 uint8_t *data);
190 void GetAroundPixelRGB565(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
191
192 void GetAroundPixelRGB888(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
193
194 void GetAroundPixelRGBA(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
195
196 void GetAroundPixelALPHA8(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
197
198 void BilinearPixelProc(const AroundPos aroundPos, BilinearPixelProcArgs &args);
199 /* Calculate the target pixel based on the pixels of 4 nearby points.
200 * Fill in new pixels with formula
201 * f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1)
202 */
203 uint32_t FilterProc(const uint32_t subx, const uint32_t suby, const AroundPixels &aroundPixels);
204
205 void ReleaseBuffer(AllocatorType allocatorType, int fd, int dataSize, uint8_t *buffer);
206
207 Matrix matrix_;
208 float minX_ = 0.0f;
209 float minY_ = 0.0f;
210 };
211 } // namespace Media
212 } // namespace OHOS
213 #endif // FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_BASIC_TRANSFORMER_H
214