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