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_MATRIX_H_
17 #define FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_MATRIX_H_
18
19 #include <cmath>
20 #include "image_log.h"
21 #include "image_type.h"
22
23 namespace OHOS {
24 namespace Media {
25 struct Point {
26 float x = 0.0f;
27 float y = 0.0f;
28 };
29
30 static constexpr int32_t IMAGE_SCALEX = 0;
31 static constexpr int32_t IMAGE_SKEWX = 1;
32 static constexpr int32_t IMAGE_TRANSX = 2;
33 static constexpr int32_t IMAGE_SKEWY = 3;
34 static constexpr int32_t IMAGE_SCALEY = 4;
35 static constexpr int32_t IMAGE_TRANSY = 5;
36 static constexpr int32_t IMAGE_PERSP0 = 6;
37 static constexpr int32_t IMAGE_PERSP1 = 7;
38 static constexpr int32_t IMAGE_PERSP2 = 8;
39 static constexpr int32_t MATIRX_ITEM_NUM = 9;
40
41 static constexpr uint32_t IDENTITY_TYPE = 0;
42 static constexpr uint32_t TRANSLATE_TYPE = 0x01;
43 static constexpr uint32_t SCALE_TYPE = 0x02;
44 static constexpr uint32_t ROTATEORSKEW_TYPE = 0x04;
45 static constexpr uint32_t PERSPECTIVE_TYPE = 0x08;
46
47 static constexpr float FLOAT_PI = 3.14159265f;
48 static constexpr float FLOAT_NEAR_ZERO = (1.0f / (1 << 12));
49 static constexpr float RADIAN_FACTOR = 180.0f;
50 static constexpr uint32_t CALCPROC_FACTOR = 0x07;
51 static constexpr uint32_t OPERTYPE_MASK = 0xF;
52 static constexpr float MATRIX_EPSILON = 1e-6;
53
54 // Degrees to Radians
DegreesToRadians(const float degrees)55 static inline float DegreesToRadians(const float degrees)
56 {
57 return degrees * (FLOAT_PI / RADIAN_FACTOR);
58 }
59
60 // Radians to Degrees
RadiansToDegrees(const float radians)61 static inline float RadiansToDegrees(const float radians)
62 {
63 return radians * (RADIAN_FACTOR / FLOAT_PI);
64 }
65
ValueNearToZero(const float radians,bool isSin)66 static inline float ValueNearToZero(const float radians, bool isSin)
67 {
68 float value = (isSin ? sinf(radians) : cosf(radians));
69 return (fabsf(value) <= FLOAT_NEAR_ZERO) ? 0.0f : value;
70 }
71
MulAddMul(const float a,const float b,const float c,const float d)72 static inline double MulAddMul(const float a, const float b, const float c, const float d)
73 {
74 return a * b + c * d;
75 }
76
MulSubMul(const float a,const float b,const float c,const float d)77 static inline double MulSubMul(const float a, const float b, const float c, const float d)
78 {
79 return a * b - c * d;
80 }
81
FDivide(const float number,const float denom)82 static inline float FDivide(const float number, const float denom)
83 {
84 if (std::fabs(denom - 0) < MATRIX_EPSILON) {
85 return 0.0f;
86 }
87 return number / denom;
88 }
89
90 class Matrix {
91 public:
92 enum OperType {
93 IDENTITY = IDENTITY_TYPE,
94 TRANSLATE = TRANSLATE_TYPE,
95 SCALE = SCALE_TYPE,
96 ROTATEORSKEW = ROTATEORSKEW_TYPE,
97 PERSPECTIVE = PERSPECTIVE_TYPE,
98 };
99
100 using CalcXYProc = void (*)(const Matrix &m, const float x, const float y, Point &result);
101
102 /** The default is the identity matrix
103 * | 1 0 0 |
104 * | 0 1 0 |
105 * | 0 0 1 |
106 */
Matrix()107 constexpr Matrix() : Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1, IDENTITY){}
108
Matrix(float sx,float kx,float tx,float ky,float sy,float ty,float p0,float p1,float p2,uint32_t operType)109 constexpr Matrix(float sx, float kx, float tx, float ky, float sy, float ty, float p0, float p1, float p2,
110 uint32_t operType)
111 : fMat_ { sx, kx, tx, ky, sy, ty, p0, p1, p2 }, operType_(operType){}
112
113 ~Matrix() = default;
114
115 static const CalcXYProc gCalcXYProcs[];
116
IsIdentity()117 bool IsIdentity() const
118 {
119 return GetOperType() == 0;
120 }
121
122 Matrix &SetTranslate(const float tx, const float ty);
123
124 Matrix &SetScale(const float sx, const float sy);
125
126 Matrix &SetRotate(const float degrees, const float px = 0.0, const float py = 0.0);
127
128 Matrix &SetSinCos(const float sinValue, const float cosValue, const float px, const float py);
129
130 Matrix &SetConcat(const Matrix &m);
131
132 Matrix &Reset();
133
134 OperType GetOperType() const;
135
136 void SetTranslateAndScale(const float tx, const float ty, const float sx, const float sy);
137
138 static void IdentityXY(const Matrix &m, const float sx, const float sy, Point &pt);
139
140 static void ScaleXY(const Matrix &m, const float sx, const float sy, Point &pt);
141
142 static void TransXY(const Matrix &m, const float tx, const float sy, Point &pt);
143
144 static void RotXY(const Matrix &m, const float rx, const float ry, Point &pt);
145
GetXYProc(const OperType operType)146 static CalcXYProc GetXYProc(const OperType operType)
147 {
148 return gCalcXYProcs[static_cast<uint8_t>(operType) & CALCPROC_FACTOR];
149 }
150
151 bool Invert(Matrix &invMatrix);
152
153 bool InvertForRotate(Matrix &invMatrix);
154
GetScaleX()155 float GetScaleX() const
156 {
157 return fMat_[IMAGE_SCALEX];
158 }
159
GetScaleY()160 float GetScaleY() const
161 {
162 return fMat_[IMAGE_SCALEY];
163 }
164
GetTransX()165 float GetTransX() const
166 {
167 return fMat_[IMAGE_TRANSX];
168 }
169
GetTranY()170 float GetTranY() const
171 {
172 return fMat_[IMAGE_TRANSY];
173 }
174
GetSkewX()175 float GetSkewX() const
176 {
177 return fMat_[IMAGE_SKEWX];
178 }
179
GetSkewY()180 float GetSkewY() const
181 {
182 return fMat_[IMAGE_SKEWY];
183 }
184
185 void Print();
186
187 private:
188 /** The fMat_ elements
189 * | scalex skewx transx |
190 * | skewy scaley transy |
191 * | persp0 persp1 persp2 |
192 */
193 float fMat_[MATIRX_ITEM_NUM];
194 uint32_t operType_;
195 };
196 } // namespace Media
197 } // namespace OHOS
198 #endif // FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_MATRIX_H_
199