22#include <freerdp/config.h>
23#include <winpr/assert.h>
24#include <winpr/cast.h>
26#include <freerdp/types.h>
27#include <freerdp/primitives.h>
28#include <freerdp/codec/color.h>
30#include "prim_internal.h"
31#include "prim_colors.h"
34#define MINMAX(_v_, _l_, _h_) ((_v_) < (_l_) ? (_l_) : ((_v_) > (_h_) ? (_h_) : (_v_)))
46static const INT32 ycbcr_constants[][4] = { { 1, 1, 0, 2 },
54 { 359, 183, 88, 453 },
55 { 718, 366, 176, 906 },
56 { 1436, 732, 352, 1812 },
57 { 2872, 1463, 704, 3625 },
58 { 5745, 2926, 1408, 7250 },
59 { 11489, 5852, 2816, 14499 },
60 { 22979, 11705, 5632, 28998 },
61 { 45958, 23409, 11263, 57996 },
62 { 91916, 46819, 22527, 115992 },
63 { 183832, 93638, 45053, 231985 },
64 { 367664, 187276, 90107, 463970 },
65 { 735327, 374552, 180214, 927940 },
66 { 1470654, 749104, 360427, 1855880 },
67 { 2941308, 1498207, 720854, 3711760 },
68 { 5882616, 2996415, 1441708, 7423520 },
69 { 11765232, 5992830, 2883416, 14847039 },
70 { 23530465, 11985660, 5766832, 29694078 },
71 { 47060930, 23971320, 11533665, 59388157 },
72 { 94121859, 47942640, 23067330, 118776314 },
73 { 188243719, 95885279, 46134660, 237552628 },
74 { 376487438, 191770558, 92269319, 475105256 },
75 { 752974876, 383541116, 184538639, 950210512 },
76 { 1505949752, 767082233, 369077277, 1900421023 } };
78static pstatus_t general_yCbCrToRGB_16s8u_P3AC4R_BGRX(
const INT16* WINPR_RESTRICT pSrc[3],
79 UINT32 srcStep, BYTE* WINPR_RESTRICT pDst,
80 UINT32 dstStep, UINT32 DstFormat,
84 const INT16* pY = pSrc[0];
85 const INT16* pCb = pSrc[1];
86 const INT16* pCr = pSrc[2];
87 const size_t srcPad = (srcStep - (roi->width * 2)) / 2;
88 const size_t dstPad = (dstStep - (roi->width * 4));
89 const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
91 for (UINT32 y = 0; y < roi->height; y++)
93 for (UINT32 x = 0; x < roi->width; x++)
95 const INT32 divisor = 16;
96 const INT32 Y = (INT32)((UINT32)((*pY++) + 4096) << divisor);
97 const INT32 Cb = (*pCb++);
98 const INT32 Cr = (*pCr++);
101 const UINT32 CrR = (UINT32)Cr * (UINT32)ycbcr_constants[divisor][0];
102 const UINT32 CrG = (UINT32)Cr * (UINT32)ycbcr_constants[divisor][1];
103 const UINT32 CbG = (UINT32)Cb * (UINT32)ycbcr_constants[divisor][2];
104 const UINT32 CbB = (UINT32)Cb * (UINT32)ycbcr_constants[divisor][3];
105 const INT32 R = (INT32)(CrR + (UINT32)Y) >> (divisor + 5);
106 const INT32 G = (INT32)((UINT32)Y - CbG - CrG) >> (divisor + 5);
107 const INT32 B = (INT32)(CbB + (UINT32)Y) >> (divisor + 5);
108 pRGB = writePixelBGRX(pRGB, formatSize, DstFormat, CLIP(R), CLIP(G), CLIP(B), 0);
117 return PRIMITIVES_SUCCESS;
120static pstatus_t general_yCbCrToRGB_16s8u_P3AC4R_general(
const INT16* WINPR_RESTRICT pSrc[3],
121 UINT32 srcStep, BYTE* WINPR_RESTRICT pDst,
122 UINT32 dstStep, UINT32 DstFormat,
126 const INT16* pY = pSrc[0];
127 const INT16* pCb = pSrc[1];
128 const INT16* pCr = pSrc[2];
129 const size_t srcPad = (srcStep - (roi->width * 2)) / 2;
130 const size_t dstPad = (dstStep - (roi->width * 4));
131 const fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
132 const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
134 for (UINT32 y = 0; y < roi->height; y++)
136 for (UINT32 x = 0; x < roi->width; x++)
138 const INT32 divisor = 16;
139 const INT32 Y = (INT32)((UINT32)((*pY++) + 4096) << divisor);
140 const INT32 Cb = (*pCb++);
141 const INT32 Cr = (*pCr++);
143 const UINT32 CrR = (UINT32)Cr * (UINT32)ycbcr_constants[divisor][0];
144 const UINT32 CrG = (UINT32)Cr * (UINT32)ycbcr_constants[divisor][1];
145 const UINT32 CbG = (UINT32)Cb * (UINT32)ycbcr_constants[divisor][2];
146 const UINT32 CbB = (UINT32)Cb * (UINT32)ycbcr_constants[divisor][3];
147 const INT32 R = (INT32)(CrR + (UINT32)Y) >> (divisor + 5);
148 const INT32 G = (INT32)((UINT32)Y - CbG - CrG) >> (divisor + 5);
149 const INT32 B = (INT32)(CbB + (UINT32)Y) >> (divisor + 5);
150 pRGB = writePixel(pRGB, formatSize, DstFormat, CLIP(R), CLIP(G), CLIP(B), 0);
159 return PRIMITIVES_SUCCESS;
162static pstatus_t general_yCbCrToRGB_16s8u_P3AC4R(
const INT16* WINPR_RESTRICT pSrc[3],
163 UINT32 srcStep, BYTE* WINPR_RESTRICT pDst,
164 UINT32 dstStep, UINT32 DstFormat,
169 case PIXEL_FORMAT_BGRA32:
170 case PIXEL_FORMAT_BGRX32:
171 return general_yCbCrToRGB_16s8u_P3AC4R_BGRX(pSrc, srcStep, pDst, dstStep, DstFormat,
175 return general_yCbCrToRGB_16s8u_P3AC4R_general(pSrc, srcStep, pDst, dstStep, DstFormat,
183general_yCbCrToRGB_16s16s_P3P3(
const INT16* WINPR_RESTRICT pSrc[3], INT32 srcStep,
184 INT16* WINPR_RESTRICT pDst[3], INT32 dstStep,
198 const INT16* yptr = pSrc[0];
199 const INT16* cbptr = pSrc[1];
200 const INT16* crptr = pSrc[2];
201 INT16* rptr = pDst[0];
202 INT16* gptr = pDst[1];
203 INT16* bptr = pDst[2];
204 UINT32 srcbump = (WINPR_ASSERTING_INT_CAST(uint32_t, srcStep) - (roi->width *
sizeof(UINT16))) /
206 UINT32 dstbump = (WINPR_ASSERTING_INT_CAST(uint32_t, dstStep) - (roi->width *
sizeof(UINT16))) /
209 for (UINT32 y = 0; y < roi->height; y++)
211 for (UINT32 x = 0; x < roi->width; ++x)
216 INT32 cy = (INT32)(*yptr++);
217 INT32 cb = (INT32)(*cbptr++);
218 INT32 cr = (INT32)(*crptr++);
242 cy = (INT32)((UINT32)(cy + 4096) << 16);
244 r = 1LL * cy + 1LL * cr * ycbcr_constants[16][0];
245 g = 1LL * cy - 1LL * cb * ycbcr_constants[16][1] - 1LL * cr * ycbcr_constants[16][2];
246 b = 1LL * cy + 1LL * cb * ycbcr_constants[16][3];
247 *rptr++ = CLIP(r >> 21);
248 *gptr++ = CLIP(g >> 21);
249 *bptr++ = CLIP(b >> 21);
260 return PRIMITIVES_SUCCESS;
265general_RGBToYCbCr_16s16s_P3P3(
const INT16* WINPR_RESTRICT pSrc[3], INT32 srcStep,
266 INT16* WINPR_RESTRICT pDst[3], INT32 dstStep,
279 const INT16* rptr = pSrc[0];
280 const INT16* gptr = pSrc[1];
281 const INT16* bptr = pSrc[2];
282 INT16* yptr = pDst[0];
283 INT16* cbptr = pDst[1];
284 INT16* crptr = pDst[2];
285 UINT32 srcbump = (WINPR_ASSERTING_INT_CAST(uint32_t, srcStep) - (roi->width *
sizeof(UINT16))) /
287 UINT32 dstbump = (WINPR_ASSERTING_INT_CAST(uint32_t, dstStep) - (roi->width *
sizeof(UINT16))) /
290 for (UINT32 y = 0; y < roi->height; y++)
292 for (UINT32 x = 0; x < roi->width; ++x)
297 INT32 r = (INT32)(*rptr++);
298 INT32 g = (INT32)(*gptr++);
299 INT32 b = (INT32)(*bptr++);
312 INT32 cy = (r * 9798 + g * 19235 + b * 3735) >> 10;
313 INT32 cb = (r * -5535 + g * -10868 + b * 16403) >> 10;
314 INT32 cr = (r * 16377 + g * -13714 + b * -2663) >> 10;
315 *yptr++ = (INT16)MINMAX(cy - 4096, -4096, 4095);
316 *cbptr++ = (INT16)MINMAX(cb, -4096, 4095);
317 *crptr++ = (INT16)MINMAX(cr, -4096, 4095);
328 return PRIMITIVES_SUCCESS;
331static inline void writeScanlineGeneric(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
332 const INT16* r,
const INT16* g,
const INT16* b, DWORD width)
334 fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
336 for (UINT32 x = 0; x < width; x++)
338 const INT16 pr = *r++;
339 const INT16 pg = *g++;
340 const INT16 pb = *b++;
343 writePixel(dst, formatSize, DstFormat, WINPR_ASSERTING_INT_CAST(UINT8, pr),
344 WINPR_ASSERTING_INT_CAST(UINT8, pg), WINPR_ASSERTING_INT_CAST(UINT8, pb), 0);
348static inline void writeScanlineRGB(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
const INT16* r,
349 const INT16* g,
const INT16* b, DWORD width)
351 WINPR_UNUSED(formatSize);
352 WINPR_UNUSED(DstFormat);
354 for (UINT32 x = 0; x < width; x++)
356 const BYTE R = CLIP(*r++);
357 const BYTE G = CLIP(*g++);
358 const BYTE B = CLIP(*b++);
365static inline void writeScanlineBGR(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
const INT16* r,
366 const INT16* g,
const INT16* b, DWORD width)
368 WINPR_UNUSED(formatSize);
369 WINPR_UNUSED(DstFormat);
371 for (UINT32 x = 0; x < width; x++)
373 const BYTE R = CLIP(*r++);
374 const BYTE G = CLIP(*g++);
375 const BYTE B = CLIP(*b++);
382static inline void writeScanlineBGRX(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
const INT16* r,
383 const INT16* g,
const INT16* b, DWORD width)
385 WINPR_UNUSED(formatSize);
386 WINPR_UNUSED(DstFormat);
388 for (UINT32 x = 0; x < width; x++)
390 const BYTE R = CLIP(*r++);
391 const BYTE G = CLIP(*g++);
392 const BYTE B = CLIP(*b++);
400static inline void writeScanlineRGBX(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
const INT16* r,
401 const INT16* g,
const INT16* b, DWORD width)
403 WINPR_UNUSED(formatSize);
404 WINPR_UNUSED(DstFormat);
406 for (UINT32 x = 0; x < width; x++)
408 const BYTE R = CLIP(*r++);
409 const BYTE G = CLIP(*g++);
410 const BYTE B = CLIP(*b++);
418static inline void writeScanlineXBGR(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
const INT16* r,
419 const INT16* g,
const INT16* b, DWORD width)
421 WINPR_UNUSED(formatSize);
422 WINPR_UNUSED(DstFormat);
424 for (UINT32 x = 0; x < width; x++)
426 const BYTE R = CLIP(*r++);
427 const BYTE G = CLIP(*g++);
428 const BYTE B = CLIP(*b++);
436static inline void writeScanlineXRGB(BYTE* dst, DWORD formatSize, UINT32 DstFormat,
const INT16* r,
437 const INT16* g,
const INT16* b, DWORD width)
439 WINPR_UNUSED(formatSize);
440 WINPR_UNUSED(DstFormat);
442 for (UINT32 x = 0; x < width; x++)
444 const BYTE R = CLIP(*r++);
445 const BYTE G = CLIP(*g++);
446 const BYTE B = CLIP(*b++);
454typedef void (*fkt_writeScanline)(BYTE*, DWORD, UINT32,
const INT16*,
const INT16*,
const INT16*,
457static inline fkt_writeScanline getScanlineWriteFunction(DWORD format)
461 case PIXEL_FORMAT_ARGB32:
462 case PIXEL_FORMAT_XRGB32:
463 return writeScanlineXRGB;
465 case PIXEL_FORMAT_ABGR32:
466 case PIXEL_FORMAT_XBGR32:
467 return writeScanlineXBGR;
469 case PIXEL_FORMAT_RGBA32:
470 case PIXEL_FORMAT_RGBX32:
471 return writeScanlineRGBX;
473 case PIXEL_FORMAT_BGRA32:
474 case PIXEL_FORMAT_BGRX32:
475 return writeScanlineBGRX;
477 case PIXEL_FORMAT_BGR24:
478 return writeScanlineBGR;
480 case PIXEL_FORMAT_RGB24:
481 return writeScanlineRGB;
484 return writeScanlineGeneric;
489static pstatus_t general_RGBToRGB_16s8u_P3AC4R_general(
490 const INT16* WINPR_RESTRICT pSrc[3],
492 BYTE* WINPR_RESTRICT pDst,
494 UINT32 DstFormat,
const prim_size_t* WINPR_RESTRICT roi)
496 const INT16* r = pSrc[0];
497 const INT16* g = pSrc[1];
498 const INT16* b = pSrc[2];
499 const DWORD srcAdd = srcStep /
sizeof(INT16);
500 fkt_writeScanline writeScanline = getScanlineWriteFunction(DstFormat);
501 const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
503 for (UINT32 y = 0; y < roi->height; ++y)
505 (*writeScanline)(pDst, formatSize, DstFormat, r, g, b, roi->width);
512 return PRIMITIVES_SUCCESS;
515static pstatus_t general_RGBToRGB_16s8u_P3AC4R_BGRX(
516 const INT16* WINPR_RESTRICT pSrc[3],
518 BYTE* WINPR_RESTRICT pDst,
520 UINT32 DstFormat,
const prim_size_t* WINPR_RESTRICT roi)
522 const INT16* r = pSrc[0];
523 const INT16* g = pSrc[1];
524 const INT16* b = pSrc[2];
525 const DWORD srcAdd = srcStep /
sizeof(INT16);
526 const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
528 for (UINT32 y = 0; y < roi->height; ++y)
530 writeScanlineBGRX(pDst, formatSize, DstFormat, r, g, b, roi->width);
537 return PRIMITIVES_SUCCESS;
541general_RGBToRGB_16s8u_P3AC4R(
const INT16* WINPR_RESTRICT pSrc[3],
543 BYTE* WINPR_RESTRICT pDst,
550 case PIXEL_FORMAT_BGRA32:
551 case PIXEL_FORMAT_BGRX32:
552 return general_RGBToRGB_16s8u_P3AC4R_BGRX(pSrc, srcStep, pDst, dstStep, DstFormat, roi);
555 return general_RGBToRGB_16s8u_P3AC4R_general(pSrc, srcStep, pDst, dstStep, DstFormat,
560void primitives_init_colors(
primitives_t* WINPR_RESTRICT prims)
562 prims->yCbCrToRGB_16s8u_P3AC4R = general_yCbCrToRGB_16s8u_P3AC4R;
563 prims->yCbCrToRGB_16s16s_P3P3 = general_yCbCrToRGB_16s16s_P3P3;
564 prims->RGBToYCbCr_16s16s_P3P3 = general_RGBToYCbCr_16s16s_P3P3;
565 prims->RGBToRGB_16s8u_P3AC4R = general_RGBToRGB_16s8u_P3AC4R;
569void primitives_init_colors_opt(
primitives_t* WINPR_RESTRICT prims)
571 primitives_init_colors(prims);
572 primitives_init_colors_sse2(prims);
573 primitives_init_colors_neon(prims);