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