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 #include "pixel_convert.h"
17 #include <map>
18 #include <mutex>
19
20 namespace OHOS {
21 namespace Media {
22 using namespace std;
23 using namespace OHOS::HiviewDFX;
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_IMAGE, "PixelConvert" };
25 #if __BYTE_ORDER == __LITTLE_ENDIAN
26 constexpr bool IS_LITTLE_ENDIAN = true;
27 #else
28 constexpr bool IS_LITTLE_ENDIAN = false;
29 #endif
30
AlphaTypeConvertOnRGB(uint32_t & A,uint32_t & R,uint32_t & G,uint32_t & B,const ProcFuncExtension & extension)31 static void AlphaTypeConvertOnRGB(uint32_t &A, uint32_t &R, uint32_t &G, uint32_t &B,
32 const ProcFuncExtension &extension)
33 {
34 switch (extension.alphaConvertType) {
35 case AlphaConvertType::PREMUL_CONVERT_UNPREMUL:
36 R = Unpremul255(R, A);
37 G = Unpremul255(G, A);
38 B = Unpremul255(B, A);
39 break;
40 case AlphaConvertType::PREMUL_CONVERT_OPAQUE:
41 R = Unpremul255(R, A);
42 G = Unpremul255(G, A);
43 B = Unpremul255(B, A);
44 A = ALPHA_OPAQUE;
45 break;
46 case AlphaConvertType::UNPREMUL_CONVERT_PREMUL:
47 R = Premul255(R, A);
48 G = Premul255(G, A);
49 B = Premul255(B, A);
50 break;
51 case AlphaConvertType::UNPREMUL_CONVERT_OPAQUE:
52 A = ALPHA_OPAQUE;
53 break;
54 default:
55 break;
56 }
57 }
58
FillARGB8888(uint32_t A,uint32_t R,uint32_t G,uint32_t B)59 static uint32_t FillARGB8888(uint32_t A, uint32_t R, uint32_t G, uint32_t B)
60 {
61 if (IS_LITTLE_ENDIAN) {
62 return ((B << SHIFT_24_BIT) | (G << SHIFT_16_BIT) | (R << SHIFT_8_BIT) | A);
63 }
64 return ((A << SHIFT_24_BIT) | (R << SHIFT_16_BIT) | (G << SHIFT_8_BIT) | B);
65 }
66
FillABGR8888(uint32_t A,uint32_t B,uint32_t G,uint32_t R)67 static uint32_t FillABGR8888(uint32_t A, uint32_t B, uint32_t G, uint32_t R)
68 {
69 if (IS_LITTLE_ENDIAN) {
70 return ((R << SHIFT_24_BIT) | (G << SHIFT_16_BIT) | (B << SHIFT_8_BIT) | A);
71 }
72 return ((A << SHIFT_24_BIT) | (B << SHIFT_16_BIT) | (G << SHIFT_8_BIT) | R);
73 }
74
FillRGBA8888(uint32_t R,uint32_t G,uint32_t B,uint32_t A)75 static uint32_t FillRGBA8888(uint32_t R, uint32_t G, uint32_t B, uint32_t A)
76 {
77 if (IS_LITTLE_ENDIAN) {
78 return ((A << SHIFT_24_BIT) | (B << SHIFT_16_BIT) | (G << SHIFT_8_BIT) | R);
79 }
80 return ((R << SHIFT_24_BIT) | (G << SHIFT_16_BIT) | (B << SHIFT_8_BIT) | A);
81 }
82
FillBGRA8888(uint32_t B,uint32_t G,uint32_t R,uint32_t A)83 static uint32_t FillBGRA8888(uint32_t B, uint32_t G, uint32_t R, uint32_t A)
84 {
85 if (IS_LITTLE_ENDIAN) {
86 return ((A << SHIFT_24_BIT) | (R << SHIFT_16_BIT) | (G << SHIFT_8_BIT) | B);
87 }
88 return ((B << SHIFT_24_BIT) | (G << SHIFT_16_BIT) | (R << SHIFT_8_BIT) | A);
89 }
90
FillRGB565(uint32_t R,uint32_t G,uint32_t B)91 static uint16_t FillRGB565(uint32_t R, uint32_t G, uint32_t B)
92 {
93 if (IS_LITTLE_ENDIAN) {
94 return ((B << SHIFT_11_BIT) | (G << SHIFT_5_BIT) | R);
95 }
96 return ((R << SHIFT_11_BIT) | (G << SHIFT_5_BIT) | B);
97 }
98
FillRGBAF16(float R,float G,float B,float A)99 static uint64_t FillRGBAF16(float R, float G, float B, float A)
100 {
101 uint64_t R16 = FloatToHalf(R);
102 uint64_t G16 = FloatToHalf(G);
103 uint64_t B16 = FloatToHalf(B);
104 uint64_t A16 = FloatToHalf(A);
105 if (IS_LITTLE_ENDIAN) {
106 return ((A16 << SHIFT_48_BIT) | (R16 << SHIFT_32_BIT) | (G16 << SHIFT_16_BIT) | B16);
107 }
108 return ((B16 << SHIFT_48_BIT) | (G16 << SHIFT_32_BIT) | (R16 << SHIFT_16_BIT) | A16);
109 }
110
111 constexpr uint8_t BYTE_BITS = 8;
112 constexpr uint8_t BYTE_BITS_MAX_INDEX = 7;
113 template<typename T>
BitConvert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t white,uint32_t black)114 static void BitConvert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t white,
115 uint32_t black)
116 {
117 destinationRow[0] = (*sourceRow & GET_8_BIT) ? white : black;
118 uint32_t bitIndex = 0;
119 uint8_t currentSource = 0;
120 /*
121 * 1 byte = 8 bit
122 * 7: 8 bit index
123 */
124 for (uint32_t i = 1; i < sourceWidth; i++) {
125 bitIndex = i % BYTE_BITS;
126 currentSource = *(sourceRow + i / BYTE_BITS);
127 if (BYTE_BITS_MAX_INDEX < bitIndex) {
128 continue;
129 }
130 destinationRow[i] = ((currentSource >> (BYTE_BITS_MAX_INDEX - bitIndex)) & GET_1_BIT) ? white : black;
131 }
132 }
133
BitConvertGray(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)134 static void BitConvertGray(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
135 const ProcFuncExtension &extension)
136 {
137 uint8_t *newDestinationRow = static_cast<uint8_t *>(destinationRow);
138 BitConvert(newDestinationRow, sourceRow, sourceWidth, GRAYSCALE_WHITE, GRAYSCALE_BLACK);
139 }
140
BitConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)141 static void BitConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
142 const ProcFuncExtension &extension)
143 {
144 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
145 BitConvert(newDestinationRow, sourceRow, sourceWidth, ARGB_WHITE, ARGB_BLACK);
146 }
147
BitConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)148 static void BitConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
149 const ProcFuncExtension &extension)
150 {
151 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
152 BitConvert(newDestinationRow, sourceRow, sourceWidth, RGB_WHITE, RGB_BLACK);
153 }
154
155 constexpr uint32_t BRANCH_GRAY_TO_ARGB8888 = 0x00000001;
156 constexpr uint32_t BRANCH_GRAY_TO_RGB565 = 0x00000002;
157 template<typename T>
GrayConvert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch)158 static void GrayConvert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch)
159 {
160 for (uint32_t i = 0; i < sourceWidth; i++) {
161 uint32_t R = sourceRow[i];
162 uint32_t G = sourceRow[i];
163 uint32_t B = sourceRow[i];
164 if (branch == BRANCH_GRAY_TO_ARGB8888) {
165 uint32_t A = ALPHA_OPAQUE;
166 destinationRow[i] = ((A << SHIFT_24_BIT) | (R << SHIFT_16_BIT) | (G << SHIFT_8_BIT) | B);
167 } else if (branch == BRANCH_GRAY_TO_RGB565) {
168 R = R >> SHIFT_3_BIT;
169 G = G >> SHIFT_2_BIT;
170 B = B >> SHIFT_3_BIT;
171 destinationRow[i] = ((R << SHIFT_11_BIT) | (G << SHIFT_5_BIT) | B);
172 } else {
173 break;
174 }
175 }
176 }
177
GrayConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)178 static void GrayConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
179 const ProcFuncExtension &extension)
180 {
181 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
182 GrayConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_GRAY_TO_ARGB8888);
183 }
184
GrayConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)185 static void GrayConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
186 const ProcFuncExtension &extension)
187 {
188 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
189 GrayConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_GRAY_TO_RGB565);
190 }
191
192 constexpr uint32_t BRANCH_ARGB8888 = 0x10000001;
193 constexpr uint32_t BRANCH_ALPHA = 0x10000002;
194 template<typename T>
GrayAlphaConvert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch,const ProcFuncExtension & extension)195 static void GrayAlphaConvert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch,
196 const ProcFuncExtension &extension)
197 {
198 for (uint32_t i = 0; i < sourceWidth; i++) {
199 uint32_t A = sourceRow[1];
200 uint32_t R = sourceRow[0];
201 uint32_t G = sourceRow[0];
202 uint32_t B = sourceRow[0];
203 AlphaTypeConvertOnRGB(A, R, G, B, extension);
204 if (branch == BRANCH_ARGB8888) {
205 destinationRow[i] = FillARGB8888(A, R, G, B);
206 } else if (branch == BRANCH_ALPHA) {
207 destinationRow[i] = A;
208 } else {
209 break;
210 }
211 sourceRow += SIZE_2_BYTE;
212 }
213 }
214
GrayAlphaConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)215 static void GrayAlphaConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
216 const ProcFuncExtension &extension)
217 {
218 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
219 GrayAlphaConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ARGB8888, extension);
220 }
221
GrayAlphaConvertAlpha(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)222 static void GrayAlphaConvertAlpha(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
223 const ProcFuncExtension &extension)
224 {
225 uint8_t *newDestinationRow = static_cast<uint8_t *>(destinationRow);
226 GrayAlphaConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ALPHA, extension);
227 }
228
229 constexpr uint32_t BRANCH_BGR888_TO_ARGB8888 = 0x20000001;
230 constexpr uint32_t BRANCH_BGR888_TO_RGBA8888 = 0x20000002;
231 constexpr uint32_t BRANCH_BGR888_TO_BGRA8888 = 0x20000003;
232 constexpr uint32_t BRANCH_BGR888_TO_RGB565 = 0x20000004;
233 constexpr uint32_t BRANCH_BGR888_TO_RGBAF16 = 0x20000005;
234 template<typename T>
BGR888Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch)235 static void BGR888Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch)
236 {
237 for (uint32_t i = 0; i < sourceWidth; i++) {
238 uint32_t R = sourceRow[2];
239 uint32_t G = sourceRow[1];
240 uint32_t B = sourceRow[0];
241 uint32_t A = ALPHA_OPAQUE;
242 if (branch == BRANCH_BGR888_TO_ARGB8888) {
243 destinationRow[i] = FillARGB8888(A, R, G, B);
244 } else if (branch == BRANCH_BGR888_TO_RGBA8888) {
245 destinationRow[i] = FillRGBA8888(R, G, B, A);
246 } else if (branch == BRANCH_BGR888_TO_BGRA8888) {
247 destinationRow[i] = FillBGRA8888(B, G, R, A);
248 } else if (branch == BRANCH_BGR888_TO_RGB565) {
249 R = R >> SHIFT_3_BIT;
250 G = G >> SHIFT_2_BIT;
251 B = B >> SHIFT_3_BIT;
252 destinationRow[i] = ((B << SHIFT_11_BIT) | (G << SHIFT_5_BIT) | R);
253 } else if (branch == BRANCH_BGR888_TO_RGBAF16) {
254 destinationRow[i] = FillRGBAF16(R, G, B, A);
255 } else {
256 break;
257 }
258 sourceRow += SIZE_3_BYTE;
259 }
260 }
261
BGR888ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)262 static void BGR888ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
263 const ProcFuncExtension &extension)
264 {
265 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
266 BGR888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGR888_TO_ARGB8888);
267 }
268
BGR888ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)269 static void BGR888ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
270 const ProcFuncExtension &extension)
271 {
272 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
273 BGR888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGR888_TO_RGBA8888);
274 }
275
BGR888ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)276 static void BGR888ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
277 const ProcFuncExtension &extension)
278 {
279 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
280 BGR888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGR888_TO_BGRA8888);
281 }
282
BGR888ConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)283 static void BGR888ConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
284 const ProcFuncExtension &extension)
285 {
286 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
287 BGR888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGR888_TO_RGB565);
288 }
289
BGR888ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)290 static void BGR888ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
291 const ProcFuncExtension &extension)
292 {
293 void* tmp = static_cast<void *>(destinationRow);
294 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
295 BGR888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGR888_TO_RGBAF16);
296 }
297
298 constexpr uint32_t BRANCH_RGB888_TO_ARGB8888 = 0x30000001;
299 constexpr uint32_t BRANCH_RGB888_TO_RGBA8888 = 0x30000002;
300 constexpr uint32_t BRANCH_RGB888_TO_BGRA8888 = 0x30000003;
301 constexpr uint32_t BRANCH_RGB888_TO_RGB565 = 0x30000004;
302 constexpr uint32_t BRANCH_RGB888_TO_RGBAF16 = 0x30000005;
303 template<typename T>
RGB888Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch)304 static void RGB888Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch)
305 {
306 for (uint32_t i = 0; i < sourceWidth; i++) {
307 uint32_t R = sourceRow[0];
308 uint32_t G = sourceRow[1];
309 uint32_t B = sourceRow[2];
310 uint32_t A = ALPHA_OPAQUE;
311 if (branch == BRANCH_RGB888_TO_ARGB8888) {
312 destinationRow[i] = FillARGB8888(A, R, G, B);
313 } else if (branch == BRANCH_RGB888_TO_RGBA8888) {
314 destinationRow[i] = FillRGBA8888(R, G, B, A);
315 } else if (branch == BRANCH_RGB888_TO_BGRA8888) {
316 destinationRow[i] = FillBGRA8888(B, G, R, A);
317 } else if (branch == BRANCH_RGB888_TO_RGB565) {
318 R = R >> SHIFT_3_BIT;
319 G = G >> SHIFT_2_BIT;
320 B = B >> SHIFT_3_BIT;
321 destinationRow[i] = FillRGB565(R, G, B);
322 } else if (branch == BRANCH_RGB888_TO_RGBAF16) {
323 destinationRow[i] = FillRGBAF16(R, G, B, A);
324 } else {
325 break;
326 }
327 sourceRow += SIZE_3_BYTE;
328 }
329 }
RGB888ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)330 static void RGB888ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
331 const ProcFuncExtension &extension)
332 {
333 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
334 RGB888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB888_TO_ARGB8888);
335 }
336
RGB888ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)337 static void RGB888ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
338 const ProcFuncExtension &extension)
339 {
340 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
341 RGB888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB888_TO_RGBA8888);
342 }
343
RGB888ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)344 static void RGB888ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
345 const ProcFuncExtension &extension)
346 {
347 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
348 RGB888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB888_TO_BGRA8888);
349 }
350
RGB888ConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)351 static void RGB888ConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
352 const ProcFuncExtension &extension)
353 {
354 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
355 RGB888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB888_TO_RGB565);
356 }
357
RGB888ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)358 static void RGB888ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
359 const ProcFuncExtension &extension)
360 {
361 void* tmp = static_cast<void *>(destinationRow);
362 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
363 RGB888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB888_TO_RGBAF16);
364 }
365 constexpr uint32_t BRANCH_RGBA8888_TO_RGBA8888_ALPHA = 0x40000001;
366 constexpr uint32_t BRANCH_RGBA8888_TO_ARGB8888 = 0x40000002;
367 constexpr uint32_t BRANCH_RGBA8888_TO_BGRA8888 = 0x40000003;
368 constexpr uint32_t BRANCH_RGBA8888_TO_RGB565 = 0x40000004;
369 constexpr uint32_t BRANCH_RGBA8888_TO_RGBAF16 = 0x40000005;
370 template<typename T>
RGBA8888Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch,const ProcFuncExtension & extension)371 static void RGBA8888Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch,
372 const ProcFuncExtension &extension)
373 {
374 for (uint32_t i = 0; i < sourceWidth; i++) {
375 uint32_t R = sourceRow[0];
376 uint32_t G = sourceRow[1];
377 uint32_t B = sourceRow[2];
378 uint32_t A = sourceRow[3];
379 AlphaTypeConvertOnRGB(A, R, G, B, extension);
380 if (branch == BRANCH_RGBA8888_TO_RGBA8888_ALPHA) {
381 destinationRow[i] = FillRGBA8888(R, G, B, A);
382 } else if (branch == BRANCH_RGBA8888_TO_ARGB8888) {
383 destinationRow[i] = FillARGB8888(A, R, G, B);
384 } else if (branch == BRANCH_RGBA8888_TO_BGRA8888) {
385 destinationRow[i] = FillBGRA8888(B, G, R, A);
386 } else if (branch == BRANCH_RGBA8888_TO_RGB565) {
387 R = R >> SHIFT_3_BIT;
388 G = G >> SHIFT_2_BIT;
389 B = B >> SHIFT_3_BIT;
390 destinationRow[i] = FillRGB565(R, G, B);
391 } else if (branch == BRANCH_RGBA8888_TO_RGBAF16) {
392 destinationRow[i] = FillRGBAF16(R, G, B, A);
393 } else {
394 break;
395 }
396 sourceRow += SIZE_4_BYTE;
397 }
398 }
399
RGBA8888ConvertRGBA8888Alpha(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)400 static void RGBA8888ConvertRGBA8888Alpha(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
401 const ProcFuncExtension &extension)
402 {
403 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
404 RGBA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA8888_TO_RGBA8888_ALPHA, extension);
405 }
406
RGBA8888ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)407 static void RGBA8888ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
408 const ProcFuncExtension &extension)
409 {
410 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
411 RGBA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA8888_TO_ARGB8888, extension);
412 }
RGBA8888ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)413 static void RGBA8888ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
414 const ProcFuncExtension &extension)
415 {
416 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
417 RGBA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA8888_TO_BGRA8888, extension);
418 }
419
RGBA8888ConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)420 static void RGBA8888ConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
421 const ProcFuncExtension &extension)
422 {
423 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
424 RGBA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA8888_TO_RGB565, extension);
425 }
426
RGBA8888ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)427 static void RGBA8888ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
428 const ProcFuncExtension &extension)
429 {
430 void* tmp = static_cast<void *>(destinationRow);
431 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
432 RGBA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA8888_TO_RGBAF16, extension);
433 }
434 constexpr uint32_t BRANCH_BGRA8888_TO_BGRA8888_ALPHA = 0x80000001;
435 constexpr uint32_t BRANCH_BGRA8888_TO_ARGB8888 = 0x80000002;
436 constexpr uint32_t BRANCH_BGRA8888_TO_RGBA8888 = 0x80000003;
437 constexpr uint32_t BRANCH_BGRA8888_TO_RGB565 = 0x80000004;
438 constexpr uint32_t BRANCH_BGRA8888_TO_RGBAF16 = 0x80000005;
439 template<typename T>
BGRA8888Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch,const ProcFuncExtension & extension)440 static void BGRA8888Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch,
441 const ProcFuncExtension &extension)
442 {
443 for (uint32_t i = 0; i < sourceWidth; i++) {
444 uint32_t B = sourceRow[0];
445 uint32_t G = sourceRow[1];
446 uint32_t R = sourceRow[2];
447 uint32_t A = sourceRow[3];
448 AlphaTypeConvertOnRGB(A, R, G, B, extension);
449 if (branch == BRANCH_BGRA8888_TO_BGRA8888_ALPHA) {
450 destinationRow[i] = FillBGRA8888(B, G, R, A);
451 } else if (branch == BRANCH_BGRA8888_TO_ARGB8888) {
452 destinationRow[i] = FillARGB8888(A, R, G, B);
453 } else if (branch == BRANCH_BGRA8888_TO_RGBA8888) {
454 destinationRow[i] = FillRGBA8888(R, G, B, A);
455 } else if (branch == BRANCH_BGRA8888_TO_RGB565) {
456 R = R >> SHIFT_3_BIT;
457 G = G >> SHIFT_2_BIT;
458 B = B >> SHIFT_3_BIT;
459 destinationRow[i] = FillRGB565(R, G, B);
460 } else if (branch == BRANCH_BGRA8888_TO_RGBAF16) {
461 destinationRow[i] = FillRGBAF16(R, G, B, A);
462 } else {
463 break;
464 }
465 sourceRow += SIZE_4_BYTE;
466 }
467 }
468
BGRA8888ConvertBGRA8888Alpha(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)469 static void BGRA8888ConvertBGRA8888Alpha(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
470 const ProcFuncExtension &extension)
471 {
472 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
473 BGRA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGRA8888_TO_BGRA8888_ALPHA, extension);
474 }
475
BGRA8888ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)476 static void BGRA8888ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
477 const ProcFuncExtension &extension)
478 {
479 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
480 BGRA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGRA8888_TO_ARGB8888, extension);
481 }
482
BGRA8888ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)483 static void BGRA8888ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
484 const ProcFuncExtension &extension)
485 {
486 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
487 BGRA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGRA8888_TO_RGBA8888, extension);
488 }
489
BGRA8888ConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)490 static void BGRA8888ConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
491 const ProcFuncExtension &extension)
492 {
493 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
494 BGRA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGRA8888_TO_RGB565, extension);
495 }
496
BGRA8888ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)497 static void BGRA8888ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
498 const ProcFuncExtension &extension)
499 {
500 void* tmp = static_cast<void *>(destinationRow);
501 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
502 BGRA8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_BGRA8888_TO_RGBAF16, extension);
503 }
504
505 constexpr uint32_t BRANCH_ARGB8888_TO_ARGB8888_ALPHA = 0x90000001;
506 constexpr uint32_t BRANCH_ARGB8888_TO_RGBA8888 = 0x90000002;
507 constexpr uint32_t BRANCH_ARGB8888_TO_BGRA8888 = 0x90000003;
508 constexpr uint32_t BRANCH_ARGB8888_TO_RGB565 = 0x90000004;
509 constexpr uint32_t BRANCH_ARGB8888_TO_RGBAF16 = 0x90000005;
510 template<typename T>
ARGB8888Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch,const ProcFuncExtension & extension)511 static void ARGB8888Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch,
512 const ProcFuncExtension &extension)
513 {
514 for (uint32_t i = 0; i < sourceWidth; i++) {
515 uint32_t A = sourceRow[0];
516 uint32_t R = sourceRow[1];
517 uint32_t G = sourceRow[2];
518 uint32_t B = sourceRow[3];
519 AlphaTypeConvertOnRGB(A, R, G, B, extension);
520 if (branch == BRANCH_ARGB8888_TO_ARGB8888_ALPHA) {
521 destinationRow[i] = FillARGB8888(A, R, G, B);
522 } else if (branch == BRANCH_ARGB8888_TO_RGBA8888) {
523 destinationRow[i] = FillRGBA8888(R, G, B, A);
524 } else if (branch == BRANCH_ARGB8888_TO_BGRA8888) {
525 destinationRow[i] = FillBGRA8888(B, G, R, A);
526 } else if (branch == BRANCH_ARGB8888_TO_RGB565) {
527 R = R >> SHIFT_3_BIT;
528 G = G >> SHIFT_2_BIT;
529 B = B >> SHIFT_3_BIT;
530 destinationRow[i] = FillRGB565(R, G, B);
531 } else if (branch == BRANCH_ARGB8888_TO_RGBAF16) {
532 destinationRow[i] = FillRGBAF16(R, G, B, A);
533 } else {
534 break;
535 }
536 sourceRow += SIZE_4_BYTE;
537 }
538 }
539
ARGB8888ConvertARGB8888Alpha(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)540 static void ARGB8888ConvertARGB8888Alpha(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
541 const ProcFuncExtension &extension)
542 {
543 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
544 ARGB8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ARGB8888_TO_ARGB8888_ALPHA, extension);
545 }
546
ARGB8888ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)547 static void ARGB8888ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
548 const ProcFuncExtension &extension)
549 {
550 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
551 ARGB8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ARGB8888_TO_RGBA8888, extension);
552 }
553
ARGB8888ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)554 static void ARGB8888ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
555 const ProcFuncExtension &extension)
556 {
557 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
558 ARGB8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ARGB8888_TO_BGRA8888, extension);
559 }
560
ARGB8888ConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)561 static void ARGB8888ConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
562 const ProcFuncExtension &extension)
563 {
564 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
565 ARGB8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ARGB8888_TO_RGB565, extension);
566 }
567
ARGB8888ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)568 static void ARGB8888ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
569 const ProcFuncExtension &extension)
570 {
571 void* tmp = static_cast<void *>(destinationRow);
572 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
573 ARGB8888Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_ARGB8888_TO_RGBAF16, extension);
574 }
575
576 constexpr uint32_t BRANCH_RGB161616_TO_ARGB8888 = 0x50000001;
577 constexpr uint32_t BRANCH_RGB161616_TO_ABGR8888 = 0x50000002;
578 constexpr uint32_t BRANCH_RGB161616_TO_RGBA8888 = 0x50000003;
579 constexpr uint32_t BRANCH_RGB161616_TO_BGRA8888 = 0x50000004;
580 constexpr uint32_t BRANCH_RGB161616_TO_RGB565 = 0x50000005;
581 constexpr uint32_t BRANCH_RGB161616_TO_RGBAF16 = 0x50000006;
582 template<typename T>
RGB161616Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch)583 static void RGB161616Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch)
584 {
585 for (uint32_t i = 0; i < sourceWidth; i++) {
586 uint32_t R = sourceRow[0];
587 uint32_t G = sourceRow[2];
588 uint32_t B = sourceRow[4];
589 uint32_t A = ALPHA_OPAQUE;
590 if (branch == BRANCH_RGB161616_TO_ARGB8888) {
591 destinationRow[i] = FillARGB8888(A, R, G, B);
592 } else if (branch == BRANCH_RGB161616_TO_ABGR8888) {
593 destinationRow[i] = FillABGR8888(A, B, G, R);
594 } else if (branch == BRANCH_RGB161616_TO_RGBA8888) {
595 destinationRow[i] = FillRGBA8888(R, G, B, A);
596 } else if (branch == BRANCH_RGB161616_TO_BGRA8888) {
597 destinationRow[i] = FillBGRA8888(B, G, R, A);
598 } else if (branch == BRANCH_RGB161616_TO_RGB565) {
599 R = R >> SHIFT_3_BIT;
600 G = G >> SHIFT_2_BIT;
601 B = B >> SHIFT_3_BIT;
602 destinationRow[i] = FillRGB565(R, G, B);
603 } else if (branch == BRANCH_RGB161616_TO_RGBAF16) {
604 destinationRow[i] = FillRGBAF16(R, G, B, A);
605 } else {
606 break;
607 }
608 sourceRow += SIZE_6_BYTE;
609 }
610 }
611
RGB161616ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)612 static void RGB161616ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
613 const ProcFuncExtension &extension)
614 {
615 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
616 RGB161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB161616_TO_ARGB8888);
617 }
618
RGB161616ConvertABGR8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)619 static void RGB161616ConvertABGR8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
620 const ProcFuncExtension &extension)
621 {
622 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
623 RGB161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB161616_TO_ABGR8888);
624 }
625
RGB161616ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)626 static void RGB161616ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
627 const ProcFuncExtension &extension)
628 {
629 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
630 RGB161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB161616_TO_RGBA8888);
631 }
632
RGB161616ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)633 static void RGB161616ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
634 const ProcFuncExtension &extension)
635 {
636 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
637 RGB161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB161616_TO_BGRA8888);
638 }
639
RGB161616ConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)640 static void RGB161616ConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
641 const ProcFuncExtension &extension)
642 {
643 uint16_t *newDestinationRow = static_cast<uint16_t *>(destinationRow);
644 RGB161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB161616_TO_RGB565);
645 }
646
RGB161616ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)647 static void RGB161616ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
648 const ProcFuncExtension &extension)
649 {
650 void* tmp = static_cast<void *>(destinationRow);
651 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
652 RGB161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB161616_TO_RGBAF16);
653 }
654
655 constexpr uint32_t BRANCH_RGBA16161616_TO_ARGB8888 = 0x60000001;
656 constexpr uint32_t BRANCH_RGBA16161616_TO_ABGR8888 = 0x60000002;
657 constexpr uint32_t BRANCH_RGBA16161616_TO_RGBA8888 = 0x60000003;
658 constexpr uint32_t BRANCH_RGBA16161616_TO_BGRA8888 = 0x60000004;
659 constexpr uint32_t BRANCH_RGBA16161616_TO_RGBAF16 = 0x60000005;
660 template<typename T>
RGBA16161616Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch,const ProcFuncExtension & extension)661 static void RGBA16161616Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch,
662 const ProcFuncExtension &extension)
663 {
664 for (uint32_t i = 0; i < sourceWidth; i++) {
665 uint32_t R = sourceRow[0];
666 uint32_t G = sourceRow[2];
667 uint32_t B = sourceRow[4];
668 uint32_t A = sourceRow[6];
669 AlphaTypeConvertOnRGB(A, R, G, B, extension);
670 if (branch == BRANCH_RGBA16161616_TO_ARGB8888) {
671 destinationRow[i] = FillARGB8888(A, R, G, B);
672 } else if (branch == BRANCH_RGBA16161616_TO_ABGR8888) {
673 destinationRow[i] = FillABGR8888(A, B, G, R);
674 } else if (branch == BRANCH_RGBA16161616_TO_RGBA8888) {
675 destinationRow[i] = FillRGBA8888(A, B, G, R);
676 } else if (branch == BRANCH_RGBA16161616_TO_BGRA8888) {
677 destinationRow[i] = FillBGRA8888(A, B, G, R);
678 } else if (branch == BRANCH_RGBA16161616_TO_RGBAF16) {
679 destinationRow[i] = FillRGBAF16(R, G, B, A);
680 } else {
681 break;
682 }
683 sourceRow += SIZE_8_BYTE;
684 }
685 }
686
RGBA16161616ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)687 static void RGBA16161616ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
688 const ProcFuncExtension &extension)
689 {
690 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
691 RGBA16161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA16161616_TO_ARGB8888, extension);
692 }
693
RGBA16161616ConvertABGR8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)694 static void RGBA16161616ConvertABGR8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
695 const ProcFuncExtension &extension)
696 {
697 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
698 RGBA16161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA16161616_TO_ABGR8888, extension);
699 }
700
RGBA16161616ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)701 static void RGBA16161616ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
702 const ProcFuncExtension &extension)
703 {
704 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
705 RGBA16161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA16161616_TO_RGBA8888, extension);
706 }
707
RGBA16161616ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)708 static void RGBA16161616ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
709 const ProcFuncExtension &extension)
710 {
711 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
712 RGBA16161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA16161616_TO_BGRA8888, extension);
713 }
714
RGBA16161616ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)715 static void RGBA16161616ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
716 const ProcFuncExtension &extension)
717 {
718 void* tmp = static_cast<void *>(destinationRow);
719 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
720 RGBA16161616Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBA16161616_TO_RGBAF16, extension);
721 }
722
723 constexpr uint32_t BRANCH_CMYK_TO_ARGB8888 = 0x70000001;
724 constexpr uint32_t BRANCH_CMYK_TO_ABGR8888 = 0x70000002;
725 constexpr uint32_t BRANCH_CMYK_TO_RGBA8888 = 0x70000003;
726 constexpr uint32_t BRANCH_CMYK_TO_BGRA8888 = 0x70000004;
727 constexpr uint32_t BRANCH_CMYK_TO_RGB565 = 0x70000005;
728 template<typename T>
CMYKConvert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch)729 static void CMYKConvert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch)
730 {
731 for (uint32_t i = 0; i < sourceWidth; i++) {
732 uint8_t C = sourceRow[0];
733 uint8_t M = sourceRow[1];
734 uint8_t Y = sourceRow[2];
735 uint8_t K = sourceRow[3];
736 uint32_t R = Premul255(C, K);
737 uint32_t G = Premul255(M, K);
738 uint32_t B = Premul255(Y, K);
739 uint32_t A = ALPHA_OPAQUE;
740 if (branch == BRANCH_CMYK_TO_ARGB8888) {
741 destinationRow[i] = FillARGB8888(A, R, G, B);
742 } else if (branch == BRANCH_CMYK_TO_ABGR8888) {
743 destinationRow[i] = FillABGR8888(A, B, G, R);
744 } else if (branch == BRANCH_CMYK_TO_RGBA8888) {
745 destinationRow[i] = FillRGBA8888(R, G, B, A);
746 } else if (branch == BRANCH_CMYK_TO_BGRA8888) {
747 destinationRow[i] = FillBGRA8888(B, G, R, A);
748 } else if (branch == BRANCH_CMYK_TO_RGB565) {
749 R = R >> SHIFT_3_BIT;
750 G = R >> SHIFT_2_BIT;
751 B = B >> SHIFT_3_BIT;
752 destinationRow[i] = FillRGB565(R, G, B);
753 } else {
754 break;
755 }
756 sourceRow += SIZE_4_BYTE;
757 }
758 }
759
CMYKConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)760 static void CMYKConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
761 const ProcFuncExtension &extension)
762 {
763 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
764 CMYKConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_CMYK_TO_ARGB8888);
765 }
766
CMYKConvertABGR8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)767 static void CMYKConvertABGR8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
768 const ProcFuncExtension &extension)
769 {
770 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
771 CMYKConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_CMYK_TO_ABGR8888);
772 }
773
CMYKConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)774 static void CMYKConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
775 const ProcFuncExtension &extension)
776 {
777 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
778 CMYKConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_CMYK_TO_RGBA8888);
779 }
780
CMYKConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)781 static void CMYKConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
782 const ProcFuncExtension &extension)
783 {
784 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
785 CMYKConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_CMYK_TO_BGRA8888);
786 }
787
CMYKConvertRGB565(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)788 static void CMYKConvertRGB565(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
789 const ProcFuncExtension &extension)
790 {
791 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
792 CMYKConvert(newDestinationRow, sourceRow, sourceWidth, BRANCH_CMYK_TO_RGB565);
793 }
794
795 constexpr uint32_t BRANCH_RGB565_TO_ARGB8888 = 0x11000001;
796 constexpr uint32_t BRANCH_RGB565_TO_RGBA8888 = 0x11000002;
797 constexpr uint32_t BRANCH_RGB565_TO_BGRA8888 = 0x11000003;
798 constexpr uint32_t BRANCH_RGB565_TO_RGBAF16 = 0x11000004;
799 template<typename T>
RGB565Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch)800 static void RGB565Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch)
801 {
802 for (uint32_t i = 0; i < sourceWidth; i++) {
803 uint32_t R = (sourceRow[0] >> SHIFT_3_BIT) & SHIFT_5_MASK;
804 uint32_t G = ((sourceRow[0] & SHIFT_3_MASK) << SHIFT_3_BIT) | ((sourceRow[1] >> SHIFT_5_BIT) & SHIFT_3_MASK);
805 uint32_t B = sourceRow[1] & SHIFT_5_MASK;
806 uint32_t A = ALPHA_OPAQUE;
807 if (branch == BRANCH_RGB565_TO_ARGB8888) {
808 destinationRow[i] = FillARGB8888(A, R, G, B);
809 } else if (branch == BRANCH_RGB565_TO_RGBA8888) {
810 destinationRow[i] = FillRGBA8888(R, G, B, A);
811 } else if (branch == BRANCH_RGB565_TO_BGRA8888) {
812 destinationRow[i] = FillBGRA8888(B, G, R, A);
813 } else if (branch == BRANCH_RGB565_TO_RGBAF16) {
814 destinationRow[i] = FillRGBAF16(R, G, B, A);
815 } else {
816 break;
817 }
818 sourceRow += SIZE_2_BYTE;
819 }
820 }
821
RGB565ConvertARGB8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)822 static void RGB565ConvertARGB8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
823 const ProcFuncExtension &extension)
824 {
825 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
826 RGB565Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB565_TO_ARGB8888);
827 }
828
RGB565ConvertRGBA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)829 static void RGB565ConvertRGBA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
830 const ProcFuncExtension &extension)
831 {
832 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
833 RGB565Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB565_TO_RGBA8888);
834 }
835
RGB565ConvertBGRA8888(void * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)836 static void RGB565ConvertBGRA8888(void *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
837 const ProcFuncExtension &extension)
838 {
839 uint32_t *newDestinationRow = static_cast<uint32_t *>(destinationRow);
840 RGB565Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB565_TO_BGRA8888);
841 }
842
RGB565ConvertRGBAF16(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)843 static void RGB565ConvertRGBAF16(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
844 const ProcFuncExtension &extension)
845 {
846 void* tmp = static_cast<void *>(destinationRow);
847 uint64_t *newDestinationRow = static_cast<uint64_t *>(tmp);
848 RGB565Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGB565_TO_RGBAF16);
849 }
850
851 constexpr uint32_t BRANCH_RGBAF16_TO_ARGB8888 = 0x13000001;
852 constexpr uint32_t BRANCH_RGBAF16_TO_RGBA8888 = 0x13000002;
853 constexpr uint32_t BRANCH_RGBAF16_TO_BGRA8888 = 0x13000003;
854 constexpr uint32_t BRANCH_RGBAF16_TO_ABGR8888 = 0x13000004;
855 constexpr uint32_t BRANCH_RGBAF16_TO_RGB565 = 0x13000005;
856 template<typename T>
RGBAF16Convert(T * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,uint32_t branch,const ProcFuncExtension & extension)857 static void RGBAF16Convert(T *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth, uint32_t branch,
858 const ProcFuncExtension &extension)
859 {
860 for (uint32_t i = 0; i < sourceWidth; i++) {
861 uint32_t R = HalfToUint32(sourceRow, IS_LITTLE_ENDIAN);
862 uint32_t G = HalfToUint32(sourceRow + 2, IS_LITTLE_ENDIAN);
863 uint32_t B = HalfToUint32(sourceRow + 4, IS_LITTLE_ENDIAN);
864 uint32_t A = HalfToUint32(sourceRow + 6, IS_LITTLE_ENDIAN);
865 AlphaTypeConvertOnRGB(A, R, G, B, extension);
866 if (branch == BRANCH_RGBAF16_TO_ARGB8888) {
867 destinationRow[i] = FillARGB8888(A, R, G, B);
868 } else if (branch == BRANCH_RGBAF16_TO_RGBA8888) {
869 destinationRow[i] = FillRGBA8888(R, G, B, A);
870 } else if (branch == BRANCH_RGBAF16_TO_BGRA8888) {
871 destinationRow[i] = FillBGRA8888(B, G, R, A);
872 } else if (branch == BRANCH_RGBAF16_TO_ABGR8888) {
873 destinationRow[i] = FillABGR8888(A, B, G, R);
874 } else if (branch == BRANCH_RGBAF16_TO_RGB565) {
875 R = R >> SHIFT_3_BIT;
876 G = G >> SHIFT_2_BIT;
877 B = B >> SHIFT_3_BIT;
878 destinationRow[i] = FillRGB565(R, G, B);
879 } else {
880 break;
881 }
882 sourceRow += SIZE_8_BYTE;
883 }
884 }
885
RGBAF16ConvertARGB8888(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)886 static void RGBAF16ConvertARGB8888(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
887 const ProcFuncExtension &extension)
888 {
889 void* tmp = static_cast<void *>(destinationRow);
890 uint32_t *newDestinationRow = static_cast<uint32_t *>(tmp);
891 RGBAF16Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBAF16_TO_ARGB8888, extension);
892 }
893
RGBAF16ConvertRGBA8888(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)894 static void RGBAF16ConvertRGBA8888(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
895 const ProcFuncExtension &extension)
896 {
897 void* tmp = static_cast<void *>(destinationRow);
898 uint32_t *newDestinationRow = static_cast<uint32_t *>(tmp);
899 RGBAF16Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBAF16_TO_RGBA8888, extension);
900 }
901
RGBAF16ConvertBGRA8888(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)902 static void RGBAF16ConvertBGRA8888(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
903 const ProcFuncExtension &extension)
904 {
905 void* tmp = static_cast<void *>(destinationRow);
906 uint32_t *newDestinationRow = static_cast<uint32_t *>(tmp);
907 RGBAF16Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBAF16_TO_BGRA8888, extension);
908 }
909
RGBAF16ConvertABGR8888(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)910 static void RGBAF16ConvertABGR8888(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
911 const ProcFuncExtension &extension)
912 {
913 void* tmp = static_cast<void *>(destinationRow);
914 uint32_t *newDestinationRow = static_cast<uint32_t *>(tmp);
915 RGBAF16Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBAF16_TO_ABGR8888, extension);
916 }
917
RGBAF16ConvertRGB565(uint8_t * destinationRow,const uint8_t * sourceRow,uint32_t sourceWidth,const ProcFuncExtension & extension)918 static void RGBAF16ConvertRGB565(uint8_t *destinationRow, const uint8_t *sourceRow, uint32_t sourceWidth,
919 const ProcFuncExtension &extension)
920 {
921 void* tmp = static_cast<void *>(destinationRow);
922 uint16_t *newDestinationRow = static_cast<uint16_t *>(tmp);
923 RGBAF16Convert(newDestinationRow, sourceRow, sourceWidth, BRANCH_RGBAF16_TO_RGB565, extension);
924 }
925
926 static map<string, ProcFuncType> g_procMapping;
927 static mutex g_procMutex;
928
MakeKey(uint32_t srcFormat,uint32_t dstFormat)929 static string MakeKey(uint32_t srcFormat, uint32_t dstFormat)
930 {
931 return to_string(srcFormat) + ("_") + to_string(dstFormat);
932 }
933
InitGrayProc()934 static void InitGrayProc()
935 {
936 g_procMapping.emplace(MakeKey(GRAY_BIT, ARGB_8888), &BitConvertARGB8888);
937 g_procMapping.emplace(MakeKey(GRAY_BIT, RGB_565), &BitConvertRGB565);
938 g_procMapping.emplace(MakeKey(GRAY_BIT, ALPHA_8), &BitConvertGray);
939
940 g_procMapping.emplace(MakeKey(ALPHA_8, ARGB_8888), &GrayConvertARGB8888);
941 g_procMapping.emplace(MakeKey(ALPHA_8, RGB_565), &GrayConvertRGB565);
942
943 g_procMapping.emplace(MakeKey(GRAY_ALPHA, ARGB_8888), &GrayAlphaConvertARGB8888);
944 g_procMapping.emplace(MakeKey(GRAY_ALPHA, ALPHA_8), &GrayAlphaConvertAlpha);
945 }
946
InitRGBProc()947 static void InitRGBProc()
948 {
949 g_procMapping.emplace(MakeKey(RGB_888, ARGB_8888), &RGB888ConvertARGB8888);
950 g_procMapping.emplace(MakeKey(RGB_888, RGBA_8888), &RGB888ConvertRGBA8888);
951 g_procMapping.emplace(MakeKey(RGB_888, BGRA_8888), &RGB888ConvertBGRA8888);
952 g_procMapping.emplace(MakeKey(RGB_888, RGB_565), &RGB888ConvertRGB565);
953
954 g_procMapping.emplace(MakeKey(BGR_888, ARGB_8888), &BGR888ConvertARGB8888);
955 g_procMapping.emplace(MakeKey(BGR_888, RGBA_8888), &BGR888ConvertRGBA8888);
956 g_procMapping.emplace(MakeKey(BGR_888, BGRA_8888), &BGR888ConvertBGRA8888);
957 g_procMapping.emplace(MakeKey(BGR_888, RGB_565), &BGR888ConvertRGB565);
958
959 g_procMapping.emplace(MakeKey(RGB_161616, ARGB_8888), &RGB161616ConvertARGB8888);
960 g_procMapping.emplace(MakeKey(RGB_161616, ABGR_8888), &RGB161616ConvertABGR8888);
961 g_procMapping.emplace(MakeKey(RGB_161616, RGBA_8888), &RGB161616ConvertRGBA8888);
962 g_procMapping.emplace(MakeKey(RGB_161616, BGRA_8888), &RGB161616ConvertBGRA8888);
963 g_procMapping.emplace(MakeKey(RGB_161616, RGB_565), &RGB161616ConvertRGB565);
964
965 g_procMapping.emplace(MakeKey(RGB_565, ARGB_8888), &RGB565ConvertARGB8888);
966 g_procMapping.emplace(MakeKey(RGB_565, RGBA_8888), &RGB565ConvertRGBA8888);
967 g_procMapping.emplace(MakeKey(RGB_565, BGRA_8888), &RGB565ConvertBGRA8888);
968 }
969
InitRGBAProc()970 static void InitRGBAProc()
971 {
972 g_procMapping.emplace(MakeKey(RGBA_8888, RGBA_8888), &RGBA8888ConvertRGBA8888Alpha);
973 g_procMapping.emplace(MakeKey(RGBA_8888, ARGB_8888), &RGBA8888ConvertARGB8888);
974 g_procMapping.emplace(MakeKey(RGBA_8888, BGRA_8888), &RGBA8888ConvertBGRA8888);
975 g_procMapping.emplace(MakeKey(RGBA_8888, RGB_565), &RGBA8888ConvertRGB565);
976
977 g_procMapping.emplace(MakeKey(BGRA_8888, RGBA_8888), &BGRA8888ConvertRGBA8888);
978 g_procMapping.emplace(MakeKey(BGRA_8888, ARGB_8888), &BGRA8888ConvertARGB8888);
979 g_procMapping.emplace(MakeKey(BGRA_8888, BGRA_8888), &BGRA8888ConvertBGRA8888Alpha);
980 g_procMapping.emplace(MakeKey(BGRA_8888, RGB_565), &BGRA8888ConvertRGB565);
981
982 g_procMapping.emplace(MakeKey(ARGB_8888, RGBA_8888), &ARGB8888ConvertRGBA8888);
983 g_procMapping.emplace(MakeKey(ARGB_8888, ARGB_8888), &ARGB8888ConvertARGB8888Alpha);
984 g_procMapping.emplace(MakeKey(ARGB_8888, BGRA_8888), &ARGB8888ConvertBGRA8888);
985 g_procMapping.emplace(MakeKey(ARGB_8888, RGB_565), &ARGB8888ConvertRGB565);
986
987 g_procMapping.emplace(MakeKey(RGBA_16161616, ARGB_8888), &RGBA16161616ConvertARGB8888);
988 g_procMapping.emplace(MakeKey(RGBA_16161616, RGBA_8888), &RGBA16161616ConvertRGBA8888);
989 g_procMapping.emplace(MakeKey(RGBA_16161616, BGRA_8888), &RGBA16161616ConvertBGRA8888);
990 g_procMapping.emplace(MakeKey(RGBA_16161616, ABGR_8888), &RGBA16161616ConvertABGR8888);
991 }
992
InitCMYKProc()993 static void InitCMYKProc()
994 {
995 g_procMapping.emplace(MakeKey(CMKY, ARGB_8888), &CMYKConvertARGB8888);
996 g_procMapping.emplace(MakeKey(CMKY, RGBA_8888), &CMYKConvertRGBA8888);
997 g_procMapping.emplace(MakeKey(CMKY, BGRA_8888), &CMYKConvertBGRA8888);
998 g_procMapping.emplace(MakeKey(CMKY, ABGR_8888), &CMYKConvertABGR8888);
999 g_procMapping.emplace(MakeKey(CMKY, RGB_565), &CMYKConvertRGB565);
1000 }
1001
InitF16Proc()1002 static void InitF16Proc()
1003 {
1004 g_procMapping.emplace(MakeKey(RGBA_F16, ARGB_8888),
1005 reinterpret_cast<ProcFuncType>(&RGBAF16ConvertARGB8888));
1006 g_procMapping.emplace(MakeKey(RGBA_F16, RGBA_8888),
1007 reinterpret_cast<ProcFuncType>(&RGBAF16ConvertRGBA8888));
1008 g_procMapping.emplace(MakeKey(RGBA_F16, BGRA_8888),
1009 reinterpret_cast<ProcFuncType>(&RGBAF16ConvertBGRA8888));
1010 g_procMapping.emplace(MakeKey(RGBA_F16, ABGR_8888),
1011 reinterpret_cast<ProcFuncType>(&RGBAF16ConvertABGR8888));
1012 g_procMapping.emplace(MakeKey(RGBA_F16, RGB_565),
1013 reinterpret_cast<ProcFuncType>(&RGBAF16ConvertRGB565));
1014
1015 g_procMapping.emplace(MakeKey(BGR_888, RGBA_F16),
1016 reinterpret_cast<ProcFuncType>(&BGR888ConvertRGBAF16));
1017 g_procMapping.emplace(MakeKey(RGB_888, RGBA_F16),
1018 reinterpret_cast<ProcFuncType>(&RGB888ConvertRGBAF16));
1019 g_procMapping.emplace(MakeKey(RGB_161616, RGBA_F16),
1020 reinterpret_cast<ProcFuncType>(&RGB161616ConvertRGBAF16));
1021 g_procMapping.emplace(MakeKey(ARGB_8888, RGBA_F16),
1022 reinterpret_cast<ProcFuncType>(&ARGB8888ConvertRGBAF16));
1023 g_procMapping.emplace(MakeKey(RGBA_8888, RGBA_F16),
1024 reinterpret_cast<ProcFuncType>(&RGBA8888ConvertRGBAF16));
1025 g_procMapping.emplace(MakeKey(BGRA_8888, RGBA_F16),
1026 reinterpret_cast<ProcFuncType>(&BGRA8888ConvertRGBAF16));
1027 g_procMapping.emplace(MakeKey(RGB_565, RGBA_F16),
1028 reinterpret_cast<ProcFuncType>(&RGB565ConvertRGBAF16));
1029 g_procMapping.emplace(MakeKey(RGBA_16161616, RGBA_F16),
1030 reinterpret_cast<ProcFuncType>(&RGBA16161616ConvertRGBAF16));
1031 }
1032
GetProcFuncType(uint32_t srcPixelFormat,uint32_t dstPixelFormat)1033 static ProcFuncType GetProcFuncType(uint32_t srcPixelFormat, uint32_t dstPixelFormat)
1034 {
1035 unique_lock<mutex> guard(g_procMutex);
1036 if (g_procMapping.empty()) {
1037 InitGrayProc();
1038 InitRGBProc();
1039 InitRGBAProc();
1040 InitCMYKProc();
1041 InitF16Proc();
1042 }
1043 guard.unlock();
1044 string procKey = MakeKey(srcPixelFormat, dstPixelFormat);
1045 map<string, ProcFuncType>::iterator iter = g_procMapping.find(procKey);
1046 if (iter != g_procMapping.end()) {
1047 return iter->second;
1048 }
1049 return nullptr;
1050 }
1051
PixelConvert(ProcFuncType funcPtr,ProcFuncExtension extension,bool isNeedConvert)1052 PixelConvert::PixelConvert(ProcFuncType funcPtr, ProcFuncExtension extension, bool isNeedConvert)
1053 : procFunc_(funcPtr), procFuncExtension_(extension), isNeedConvert_(isNeedConvert)
1054 {}
1055
1056 // caller need setting the correct pixelFormat and alphaType
Create(const ImageInfo & srcInfo,const ImageInfo & dstInfo)1057 std::unique_ptr<PixelConvert> PixelConvert::Create(const ImageInfo &srcInfo, const ImageInfo &dstInfo)
1058 {
1059 if (srcInfo.pixelFormat == PixelFormat::UNKNOWN || dstInfo.pixelFormat == PixelFormat::UNKNOWN) {
1060 HiLog::Error(LABEL, "source or destination pixel format unknown");
1061 return nullptr;
1062 }
1063 uint32_t srcFormat = static_cast<uint32_t>(srcInfo.pixelFormat);
1064 uint32_t dstFormat = static_cast<uint32_t>(dstInfo.pixelFormat);
1065 ProcFuncType funcPtr = GetProcFuncType(srcFormat, dstFormat);
1066 if (funcPtr == nullptr) {
1067 HiLog::Error(LABEL, "not found convert function. pixelFormat %{public}u -> %{public}u", srcFormat, dstFormat);
1068 return nullptr;
1069 }
1070 ProcFuncExtension extension;
1071 extension.alphaConvertType = GetAlphaConvertType(srcInfo.alphaType, dstInfo.alphaType);
1072 bool isNeedConvert = true;
1073 if ((srcInfo.pixelFormat == dstInfo.pixelFormat) && (extension.alphaConvertType == AlphaConvertType::NO_CONVERT)) {
1074 isNeedConvert = false;
1075 }
1076 return unique_ptr<PixelConvert>(new (nothrow) PixelConvert(funcPtr, extension, isNeedConvert));
1077 }
1078
GetAlphaConvertType(const AlphaType & srcType,const AlphaType & dstType)1079 AlphaConvertType PixelConvert::GetAlphaConvertType(const AlphaType &srcType, const AlphaType &dstType)
1080 {
1081 if (srcType == AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN || dstType == AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN) {
1082 HiLog::Debug(LABEL, "source or destination alpha type unknown");
1083 return AlphaConvertType::NO_CONVERT;
1084 }
1085 if ((srcType == AlphaType::IMAGE_ALPHA_TYPE_PREMUL) && (dstType == AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL)) {
1086 return AlphaConvertType::PREMUL_CONVERT_UNPREMUL;
1087 }
1088 if ((srcType == AlphaType::IMAGE_ALPHA_TYPE_PREMUL) && (dstType == AlphaType::IMAGE_ALPHA_TYPE_OPAQUE)) {
1089 return AlphaConvertType::PREMUL_CONVERT_OPAQUE;
1090 }
1091 if ((srcType == AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL) && (dstType == AlphaType::IMAGE_ALPHA_TYPE_PREMUL)) {
1092 return AlphaConvertType::UNPREMUL_CONVERT_PREMUL;
1093 }
1094 if ((srcType == AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL) && (dstType == AlphaType::IMAGE_ALPHA_TYPE_OPAQUE)) {
1095 return AlphaConvertType::UNPREMUL_CONVERT_OPAQUE;
1096 }
1097 return AlphaConvertType::NO_CONVERT;
1098 }
1099
Convert(void * destinationPixels,const uint8_t * sourcePixels,uint32_t sourcePixelsNum)1100 void PixelConvert::Convert(void *destinationPixels, const uint8_t *sourcePixels, uint32_t sourcePixelsNum)
1101 {
1102 if ((destinationPixels == nullptr) || (sourcePixels == nullptr)) {
1103 HiLog::Error(LABEL, "destinationPixel or sourcePixel is null");
1104 return;
1105 }
1106 if (!isNeedConvert_) {
1107 HiLog::Debug(LABEL, "no need convert");
1108 return;
1109 }
1110 procFunc_(destinationPixels, sourcePixels, sourcePixelsNum, procFuncExtension_);
1111 }
1112 } // namespace Media
1113 } // namespace OHOS
1114