FreeRDP
Loading...
Searching...
No Matches
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/* ------------------------------------------------------------------------- */
33static pstatus_t general_alphaComp_argb(const BYTE* WINPR_RESTRICT pSrc1, UINT32 src1Step,
34 const BYTE* WINPR_RESTRICT pSrc2, UINT32 src2Step,
35 BYTE* WINPR_RESTRICT pDst, UINT32 dstStep, UINT32 width,
36 UINT32 height)
37{
38 for (size_t y = 0; y < height; y++)
39 {
40 const UINT32* sptr1 = (const UINT32*)(pSrc1 + y * src1Step);
41 const UINT32* sptr2 = (const UINT32*)(pSrc2 + y * src2Step);
42 UINT32* dptr = (UINT32*)(pDst + y * dstStep);
43
44 for (size_t x = 0; x < width; x++)
45 {
46 const UINT32 src1 = *sptr1++;
47 const UINT32 src2 = *sptr2++;
48 UINT32 alpha = ALPHA(src1) + 1;
49
50 if (alpha == 256)
51 {
52 /* If alpha is 255+1, just copy src1. */
53 *dptr++ = src1;
54 }
55 else if (alpha <= 1)
56 {
57 /* If alpha is 0+1, just copy src2. */
58 *dptr++ = src2;
59 }
60 else
61 {
62 /* A perfectly accurate blend would do (a*src + (255-a)*dst)/255
63 * rather than adding one to alpha and dividing by 256, but this
64 * is much faster and only differs by one 16% of the time.
65 * I'm not sure who first designed the double-ops trick
66 * (Red Blue and Alpha Green).
67 */
68 UINT32 rb = 0;
69 UINT32 ag = 0;
70 UINT32 s2rb = src2 & 0x00FF00FFU;
71 UINT32 s2ag = (src2 >> 8) & 0x00FF00FFU;
72 UINT32 s1rb = src1 & 0x00FF00FFU;
73 UINT32 s1ag = (src1 >> 8) & 0x00FF00FFU;
74 UINT32 drb = s1rb - s2rb;
75 UINT32 dag = s1ag - s2ag;
76 drb *= alpha;
77 dag *= alpha;
78 rb = ((drb >> 8) + s2rb) & 0x00FF00FFU;
79 ag = (((dag >> 8) + s2ag) << 8) & 0xFF00FF00U;
80 *dptr++ = rb | ag;
81 }
82 }
83 }
84
85 return PRIMITIVES_SUCCESS;
86}
87
88/* ------------------------------------------------------------------------- */
89void primitives_init_alphaComp(primitives_t* WINPR_RESTRICT prims)
90{
91 prims->alphaComp_argb = general_alphaComp_argb;
92}
93
94void primitives_init_alphaComp_opt(primitives_t* WINPR_RESTRICT prims)
95{
96 primitives_init_alphaComp(prims);
97 primitives_init_alphaComp_sse3(prims);
98}