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