FreeRDP
prim_YCoCg.c
1 /* FreeRDP: A Remote Desktop Protocol Client
2  * YCoCg<->RGB Color conversion operations.
3  * vi:ts=4 sw=4:
4  *
5  * (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include <freerdp/config.h>
21 
22 #include <freerdp/types.h>
23 #include <freerdp/primitives.h>
24 
25 #include "prim_internal.h"
26 #include "prim_YCoCg.h"
27 
28 /* helper function to convert raw 8 bit values to signed 16bit values.
29  */
30 static INT16 convert(UINT8 raw, int shift)
31 {
32  const int cll = shift - 1; /* -1 builds in the /2's */
33  return (INT16)((INT8)(raw << cll));
34 }
35 
36 /* ------------------------------------------------------------------------- */
37 static pstatus_t general_YCoCgToRGB_8u_AC4R(const BYTE* pSrc, INT32 srcStep, BYTE* pDst,
38  UINT32 DstFormat, INT32 dstStep, UINT32 width,
39  UINT32 height, UINT8 shift, BOOL withAlpha)
40 {
41  const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
42  fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, TRUE);
43 
44  for (size_t y = 0; y < height; y++)
45  {
46  const BYTE* sptr = &pSrc[y * srcStep];
47  BYTE* dptr = &pDst[y * dstStep];
48  for (size_t x = 0; x < width; x++)
49  {
50  /* Note: shifts must be done before sign-conversion. */
51  const INT16 Cg = convert(*sptr++, shift);
52  const INT16 Co = convert(*sptr++, shift);
53  const INT16 Y = *sptr++; /* UINT8->INT16 */
54  const INT16 T = (INT16)(Y - Cg);
55  const INT16 B = (INT16)(T + Co);
56  const INT16 G = (INT16)(Y + Cg);
57  const INT16 R = (INT16)(T - Co);
58  BYTE A = *sptr++;
59 
60  if (!withAlpha)
61  A = 0xFFU;
62 
63  dptr = writePixel(dptr, formatSize, DstFormat, CLIP(R), CLIP(G), CLIP(B), A);
64  }
65  }
66 
67  return PRIMITIVES_SUCCESS;
68 }
69 
70 /* ------------------------------------------------------------------------- */
71 void primitives_init_YCoCg(primitives_t* WINPR_RESTRICT prims)
72 {
73  prims->YCoCgToRGB_8u_AC4R = general_YCoCgToRGB_8u_AC4R;
74 }
75 
76 void primitives_init_YCoCg_opt(primitives_t* WINPR_RESTRICT prims)
77 {
78  primitives_init_YCoCg_ssse3(prims);
79  primitives_init_YCoCg_neon(prims);
80 }