• 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 FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_BASIC_TRANSFORMER_H_
17 #define FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_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 
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;
PixmapInfoPixmapInfo73     PixmapInfo(){}
74 
~PixmapInfoPixmapInfo75     ~PixmapInfo()
76     {
77         if (isAutoDestruct) {
78             if (data != nullptr) {
79                 free(data);
80                 data = nullptr;
81             }
82         }
83     }
84 
PixmapInfoPixmapInfo85     explicit PixmapInfo(bool isAuto)
86     {
87         isAutoDestruct = isAuto;
88     }
PixmapInfoPixmapInfo89     explicit PixmapInfo(const PixmapInfo &src)
90     {
91         Init(src);
92     }
93 
InitPixmapInfo94     void Init(const PixmapInfo &src)
95     {
96         imageInfo = src.imageInfo;
97         data = nullptr;
98         bufferSize = 0;
99     }
100 
DestroyPixmapInfo101     void Destroy()
102     {
103         if (data != nullptr) {
104             free(data);
105             data = nullptr;
106         }
107     }
108 
PrintPixmapInfoPixmapInfo109     void PrintPixmapInfo(const std::string &strFlag) const
110     {
111         IMAGE_LOGD("[PixmapInfo][%{public}s][width, height:%{public}d, %{public}d]\
112                     [bufferSize:%{public}u][pixelFormat:%{public}d].",
113                    strFlag.c_str(), imageInfo.size.width, imageInfo.size.height, bufferSize,
114                    static_cast<int32_t>(imageInfo.pixelFormat));
115     }
116 };
117 
118 class BasicTransformer {
119 public:
120     using AllocateMem = uint8_t *(*)(const Size &size, const uint64_t bufferSize, int &fd);
121 
BasicTransformer()122     BasicTransformer()
123     {
124         ResetParam();
125     }
~BasicTransformer()126     ~BasicTransformer(){}
127 
128     // Reset pixel map info transform param, back to the original state
129     void ResetParam();
130 
131     // Reserved interface
132     void SetTranslateParam(const float tx, const float ty);
133 
134     void SetScaleParam(const float sx, const float sy);
135 
136     /** Set rotates param by degrees about a point at (px, py). Positive degrees rotates
137      * clockwise.
138      *
139      * @param degrees  amount to rotate, in degrees
140      * @param px       x-axis value of the point to rotate about
141      * @param py       y-axis value of the point to rotate about
142      */
143     void SetRotateParam(const float degrees, const float px = 0.0f, const float py = 0.0f);
144 
145     /**
146      * Transform pixel map info. before transform, you should set pixel transform param first.
147      * @param inPixmap The input pixel map info
148      * @param outPixmap The output pixel map info, the pixelFormat and colorSpace same as the inPixmap
149      * @param allocate This is func pointer, if it is null, this function will new heap memory,
150      * so you must active release memory, if it is not null, that means you need allocate memory by yourself,
151      * so you should invoke GetDstWH function to get the dest width and height,
152      * then fill to outPixmap's width and height.
153      * @return the error no
154      */
155     uint32_t TransformPixmap(const PixmapInfo &inPixmap, PixmapInfo &outPixmap, AllocateMem allocate = nullptr);
156 
157     void GetDstDimension(const Size &srcSize, Size &dstSize);
158 
159 private:
160     struct AroundPixels {
161         uint32_t color00 = 0;
162         uint32_t color01 = 0;
163         uint32_t color10 = 0;
164         uint32_t color11 = 0;
165     };
166     struct AroundPos {
167         uint32_t x0 = 0;
168         uint32_t x1 = 0;
169         uint32_t y0 = 0;
170         uint32_t y1 = 0;
171     };
172 
173     uint32_t RightShift16Bit(uint32_t num, int32_t maxNum);
174 
175     void GetRotateDimension(Matrix::CalcXYProc fInvProc, const Size &srcSize, Size &dstSize);
176 
177     bool DrawPixelmap(const PixmapInfo &pixmapInfo, const int32_t pixelBytes, const Size &size, uint8_t *data);
178 
179     bool CheckAllocateBuffer(PixmapInfo &outPixmap, AllocateMem allocate, int &fd, uint64_t &bufferSize, Size &dstSize);
180 
181     void BilinearProc(const Point &pt, const PixmapInfo &pixmapInfo, const uint32_t rb, const int32_t shiftBytes,
182                       uint8_t *data);
183     void GetAroundPixelRGB565(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
184 
185     void GetAroundPixelRGB888(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
186 
187     void GetAroundPixelRGBA(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
188 
189     void GetAroundPixelALPHA8(const AroundPos aroundPos, uint8_t *data, uint32_t rb, AroundPixels &aroundPixels);
190 
191     void BilinearPixelProc(const AroundPos aroundPos, BilinearPixelProcArgs &args);
192     /* Calculate the target pixel based on the pixels of 4 nearby points.
193      * Fill in new pixels with formula
194      * 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)
195      */
196     uint32_t FilterProc(const uint32_t subx, const uint32_t suby, const AroundPixels &aroundPixels);
197 
198     void ReleaseBuffer(AllocatorType allocatorType, int fd, int dataSize, uint8_t *buffer);
199 
200     Matrix matrix_;
201     float minX_ = 0.0f;
202     float minY_ = 0.0f;
203 };
204 } // namespace Media
205 } // namespace OHOS
206 #endif // FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_BASIC_TRANSFORMER_H_
207