FreeRDP
prim_alphaComp.c
1 /* FreeRDP: A Remote Desktop Protocol Client
2  * Alpha blending routines.
3  * vi:ts=4 sw=4:
4  *
5  * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may
7  * not use this file except in compliance with the License. You may obtain
8  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
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
12  * or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * Note: this code assumes the second operand is fully opaque,
16  * e.g.
17  * newval = alpha1*val1 + (1-alpha1)*val2
18  * rather than
19  * newval = alpha1*val1 + (1-alpha1)*alpha2*val2
20  */
21 
22 #include <freerdp/config.h>
23 
24 #include <freerdp/types.h>
25 #include <freerdp/primitives.h>
26 
27 #include "prim_internal.h"
28 #include "prim_alphaComp.h"
29 
30 #define ALPHA(_k_) (((_k_)&0xFF000000U) >> 24)
31 
32 /* ------------------------------------------------------------------------- */
33 static pstatus_t general_alphaComp_argb(const BYTE* pSrc1, UINT32 src1Step, const BYTE* pSrc2,
34  UINT32 src2Step, BYTE* pDst, UINT32 dstStep, UINT32 width,
35  UINT32 height)
36 {
37  for (size_t y = 0; y < height; y++)
38  {
39  const UINT32* sptr1 = (const UINT32*)(pSrc1 + y * src1Step);
40  const UINT32* sptr2 = (const UINT32*)(pSrc2 + y * src2Step);
41  UINT32* dptr = (UINT32*)(pDst + y * dstStep);
42 
43  for (size_t x = 0; x < width; x++)
44  {
45  const UINT32 src1 = *sptr1++;
46  const UINT32 src2 = *sptr2++;
47  UINT32 alpha = ALPHA(src1) + 1;
48 
49  if (alpha == 256)
50  {
51  /* If alpha is 255+1, just copy src1. */
52  *dptr++ = src1;
53  }
54  else if (alpha <= 1)
55  {
56  /* If alpha is 0+1, just copy src2. */
57  *dptr++ = src2;
58  }
59  else
60  {
61  /* A perfectly accurate blend would do (a*src + (255-a)*dst)/255
62  * rather than adding one to alpha and dividing by 256, but this
63  * is much faster and only differs by one 16% of the time.
64  * I'm not sure who first designed the double-ops trick
65  * (Red Blue and Alpha Green).
66  */
67  UINT32 rb = 0;
68  UINT32 ag = 0;
69  UINT32 s2rb = src2 & 0x00FF00FFU;
70  UINT32 s2ag = (src2 >> 8) & 0x00FF00FFU;
71  UINT32 s1rb = src1 & 0x00FF00FFU;
72  UINT32 s1ag = (src1 >> 8) & 0x00FF00FFU;
73  UINT32 drb = s1rb - s2rb;
74  UINT32 dag = s1ag - s2ag;
75  drb *= alpha;
76  dag *= alpha;
77  rb = ((drb >> 8) + s2rb) & 0x00FF00FFU;
78  ag = (((dag >> 8) + s2ag) << 8) & 0xFF00FF00U;
79  *dptr++ = rb | ag;
80  }
81  }
82  }
83 
84  return PRIMITIVES_SUCCESS;
85 }
86 
87 /* ------------------------------------------------------------------------- */
88 void primitives_init_alphaComp(primitives_t* WINPR_RESTRICT prims)
89 {
90  prims->alphaComp_argb = general_alphaComp_argb;
91 }
92 
93 void primitives_init_alphaComp_opt(primitives_t* WINPR_RESTRICT prims)
94 {
95  primitives_init_alphaComp_sse3(prims);
96 }