• 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_PIXEL_CONVERT_H
17 #define FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_PIXEL_CONVERT_H
18 
19 #include <cstdint>
20 #include <cmath>
21 #include <memory>
22 #include "image_type.h"
23 
24 namespace OHOS {
25 namespace Media {
26 enum class AlphaConvertType : uint32_t {
27     NO_CONVERT = 0,
28     PREMUL_CONVERT_UNPREMUL = 1,
29     PREMUL_CONVERT_OPAQUE = 2,
30     UNPREMUL_CONVERT_PREMUL = 3,
31     UNPREMUL_CONVERT_OPAQUE = 4,
32 };
33 
34 // now support AlphaConvertType
35 struct ProcFuncExtension {
36     AlphaConvertType alphaConvertType;
37 };
38 
39 struct BufferInfo {
40     void *pixels;
41     int32_t rowStride;
42     ImageInfo imageInfo;
43     uint8_t range;
44     uint32_t length;
45     YuvConversion yuvConversion;
46 };
47 
48 // These values SHOULD be sync with image_type.h PixelFormat
49 constexpr uint32_t GRAY_BIT = 0x80000001; /* Tow value image, just white or black. */
50 constexpr uint32_t GRAY_ALPHA = 0x80000002;
51 constexpr uint32_t ARGB_8888 = 0x00000001;
52 constexpr uint32_t RGB_565 = 0x00000002;
53 constexpr uint32_t RGBA_8888 = 0x00000003;
54 constexpr uint32_t BGRA_8888 = 0x00000004;
55 constexpr uint32_t RGB_888 = 0x00000005;
56 constexpr uint32_t ALPHA_8 = 0x00000006; /* Gray image, 8 bit = 255 color. */
57 constexpr uint32_t RGBA_F16 = 0x00000007;
58 constexpr uint32_t ABGR_8888 = 0x00000008;
59 constexpr uint32_t BGR_888 = 0x40000002;
60 constexpr uint32_t RGB_161616 = 0x40000007;
61 constexpr uint32_t RGBA_16161616 = 0x40000008;
62 
63 constexpr uint32_t CMKY = 0x0000000A;
64 
65 constexpr uint32_t SIZE_1_BYTE = 0x00000001; /* a pixel has 8 bit = 1 byte */
66 constexpr uint32_t SIZE_2_BYTE = 0x00000002; /* a pixel has 16 bit = 2 byte */
67 constexpr uint32_t SIZE_3_BYTE = 0x00000003;
68 constexpr uint32_t SIZE_4_BYTE = 0x00000004;
69 constexpr uint32_t SIZE_6_BYTE = 0x00000006;
70 constexpr uint32_t SIZE_8_BYTE = 0x00000008;
71 
72 constexpr uint8_t GRAYSCALE_WHITE = 0xFF;
73 constexpr uint8_t GRAYSCALE_BLACK = 0x00;
74 constexpr uint32_t ARGB_WHITE = 0xFFFFFFFF;
75 constexpr uint32_t ARGB_BLACK = 0xFF000000;
76 constexpr uint16_t RGB_WHITE = 0xFFFF;
77 constexpr uint16_t RGB_BLACK = 0x0000;
78 
79 constexpr uint8_t ALPHA_OPAQUE = 0xFF;
80 constexpr uint8_t ALPHA_TRANSPARENT = 0x00;
81 
82 constexpr uint32_t GET_8_BIT = 0x80;
83 constexpr uint32_t GET_1_BIT = 0x01;
84 
85 constexpr uint32_t SHIFT_48_BIT = 0x30;
86 constexpr uint32_t SHIFT_32_BIT = 0x20;
87 constexpr uint32_t SHIFT_24_BIT = 0x18;
88 constexpr uint32_t SHIFT_16_BIT = 0x10;
89 constexpr uint32_t SHIFT_8_BIT = 0x08;
90 constexpr uint32_t SHIFT_11_BIT = 0x0B;
91 constexpr uint32_t SHIFT_5_BIT = 0x05;
92 constexpr uint32_t SHIFT_3_BIT = 0x03;
93 constexpr uint32_t SHIFT_2_BIT = 0x02;
94 
95 constexpr uint32_t SHIFT_32_MASK = 0x80000000;
96 constexpr uint32_t SHIFT_16_MASK = 0x8000;
97 constexpr uint32_t SHIFT_7_MASK = 0x1C000;
98 constexpr uint8_t SHIFT_5_MASK = 0x1F;
99 constexpr uint8_t SHIFT_3_MASK = 0x07;
100 
101 constexpr uint8_t SHIFT_HALF_BIT = 0x0D;
102 constexpr uint32_t SHIFT_HALF_MASK = 0x38000000;
103 
104 constexpr uint16_t MAX_15_BIT_VALUE = 0x7FFF;
105 constexpr uint16_t MAX_16_BIT_VALUE = 0xFFFF;
106 constexpr uint32_t MAX_31_BIT_VALUE = 0x7FFFFFFF;
107 constexpr float HALF_ONE = 0.5F;
108 constexpr float MAX_HALF = 65504;
109 constexpr float MIN_EPSILON = 1e-6;
110 
FloatCompareTo(float val,float compare)111 static inline bool FloatCompareTo(float val, float compare)
112 {
113     return fabs(val - compare) < MIN_EPSILON;
114 }
115 
Premul255(uint32_t colorComponent,uint32_t alpha)116 static inline uint32_t Premul255(uint32_t colorComponent, uint32_t alpha)
117 {
118     if (colorComponent == 0 || colorComponent > MAX_15_BIT_VALUE || alpha > MAX_15_BIT_VALUE) {
119         return 0;
120     }
121     uint32_t product = colorComponent * alpha + GET_8_BIT;
122     if (colorComponent * alpha / colorComponent != alpha) {
123         return 0;
124     }
125     return ((product + (product >> SHIFT_8_BIT)) >> SHIFT_8_BIT);
126 }
127 
Unpremul255(uint32_t colorComponent,uint32_t alpha)128 static inline uint32_t Unpremul255(uint32_t colorComponent, uint32_t alpha)
129 {
130     if (colorComponent > ALPHA_OPAQUE || alpha > ALPHA_OPAQUE) {
131         return 0;
132     }
133     if (alpha == ALPHA_TRANSPARENT) {
134         return ALPHA_TRANSPARENT;
135     }
136     if (alpha == ALPHA_OPAQUE) {
137         return colorComponent;
138     }
139     uint32_t result = static_cast<float>(colorComponent) * ALPHA_OPAQUE / alpha + HALF_ONE;
140     return (result > ALPHA_OPAQUE) ? ALPHA_OPAQUE : result;
141 }
142 
FloatToUint(float f)143 static inline uint32_t FloatToUint(float f)
144 {
145     uint32_t *p = reinterpret_cast<uint32_t*>(&f);
146     return *p;
147 }
148 
UintToFloat(uint32_t ui)149 static inline float UintToFloat(uint32_t ui)
150 {
151     float *pf = reinterpret_cast<float*>(&ui);
152     return *pf;
153 }
154 
FloatToHalf(float f)155 static inline uint16_t FloatToHalf(float f)
156 {
157     uint32_t u32 = FloatToUint(f);
158     uint16_t u16 = static_cast<uint16_t>(
159         (((u32 & MAX_31_BIT_VALUE) >> SHIFT_HALF_BIT) - SHIFT_7_MASK) & MAX_16_BIT_VALUE);
160     u16 |= static_cast<uint16_t>(
161         ((u32 & SHIFT_32_MASK) >> SHIFT_16_BIT) & MAX_16_BIT_VALUE);
162     return u16;
163 }
164 
HalfToFloat(uint16_t ui)165 static inline float HalfToFloat(uint16_t ui)
166 {
167     uint32_t u32 = ((ui & MAX_15_BIT_VALUE) << SHIFT_HALF_BIT) + SHIFT_HALF_MASK;
168     u32 |= ((ui & SHIFT_16_MASK) << SHIFT_16_BIT);
169     return UintToFloat(u32);
170 }
171 
U8ToU16(uint8_t val1,uint8_t val2)172 static inline uint16_t U8ToU16(uint8_t val1, uint8_t val2)
173 {
174     uint16_t ret = val1;
175     return ((ret << SHIFT_8_BIT) | val2);
176 }
177 
HalfToUint32(const uint8_t * ui,bool isLittleEndian)178 static inline uint32_t HalfToUint32(const uint8_t* ui, bool isLittleEndian)
179 {
180     uint16_t val = isLittleEndian ? U8ToU16(*ui, *(ui + 1)) : U8ToU16(*(ui + 1), *ui);
181     float fRet = HalfToFloat(val);
182     return static_cast<uint32_t>(fRet);
183 }
184 
185 using ProcFuncType = void (*)(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
186                               const ProcFuncExtension &extension);
187 class PixelConvert {
188 public:
189     PixelConvert(ProcFuncType funcPtr, ProcFuncExtension extension, bool isNeedConvert);
190     ~PixelConvert() = default;
191     static std::unique_ptr<PixelConvert> Create(const ImageInfo &srcInfo, const ImageInfo &dstInfo);
192     void Convert(void *destinationPixels, const uint8_t *sourcePixels, uint32_t sourcePixelsNum);
193 
194     static int32_t PixelsConvert(const BufferInfo &src, BufferInfo &dst, int32_t srcLength, bool useDMA);
195     static int32_t PixelsConvert(const BufferInfo &src, BufferInfo &dst, bool useDMA);
196     static std::unique_ptr<PixelMap> AstcToRgba(PixelMap *source, uint32_t &errorCode, PixelFormat destFormat);
197 private:
198     static AlphaConvertType GetAlphaConvertType(const AlphaType &srcType, const AlphaType &dstType);
199     static bool IsValidRowStride(int32_t rowStride, const ImageInfo &imageInfo);
200     static bool IsValidBufferInfo(const BufferInfo &bufferInfo);
201     static int32_t CopySrcBufferAndConvert(const BufferInfo &src, BufferInfo &dst, int32_t srcLength, bool useDMA);
202 
203     ProcFuncType procFunc_;
204     ProcFuncExtension procFuncExtension_;
205     bool isNeedConvert_ = true;
206 };
207 } // namespace Media
208 } // namespace OHOS
209 
210 #endif // FRAMEWORKS_INNERKITSIMPL_CONVERTER_INCLUDE_PIXEL_CONVERT_H
211