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