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