Coverage Report

Created: 2025-07-01 06:46

/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
32
/* ------------------------------------------------------------------------- */
33
static 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
0
{
38
0
  for (size_t y = 0; y < height; y++)
39
0
  {
40
0
    const UINT32* sptr1 = (const UINT32*)(pSrc1 + y * src1Step);
41
0
    const UINT32* sptr2 = (const UINT32*)(pSrc2 + y * src2Step);
42
0
    UINT32* dptr = (UINT32*)(pDst + y * dstStep);
43
44
0
    for (size_t x = 0; x < width; x++)
45
0
    {
46
0
      const UINT32 src1 = *sptr1++;
47
0
      const UINT32 src2 = *sptr2++;
48
0
      UINT32 alpha = ALPHA(src1) + 1;
49
50
0
      if (alpha == 256)
51
0
      {
52
        /* If alpha is 255+1, just copy src1. */
53
0
        *dptr++ = src1;
54
0
      }
55
0
      else if (alpha <= 1)
56
0
      {
57
        /* If alpha is 0+1, just copy src2. */
58
0
        *dptr++ = src2;
59
0
      }
60
0
      else
61
0
      {
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
0
        UINT32 rb = 0;
69
0
        UINT32 ag = 0;
70
0
        UINT32 s2rb = src2 & 0x00FF00FFU;
71
0
        UINT32 s2ag = (src2 >> 8) & 0x00FF00FFU;
72
0
        UINT32 s1rb = src1 & 0x00FF00FFU;
73
0
        UINT32 s1ag = (src1 >> 8) & 0x00FF00FFU;
74
0
        UINT32 drb = s1rb - s2rb;
75
0
        UINT32 dag = s1ag - s2ag;
76
0
        drb *= alpha;
77
0
        dag *= alpha;
78
0
        rb = ((drb >> 8) + s2rb) & 0x00FF00FFU;
79
0
        ag = (((dag >> 8) + s2ag) << 8) & 0xFF00FF00U;
80
0
        *dptr++ = rb | ag;
81
0
      }
82
0
    }
83
0
  }
84
85
0
  return PRIMITIVES_SUCCESS;
86
0
}
87
88
/* ------------------------------------------------------------------------- */
89
void primitives_init_alphaComp(primitives_t* WINPR_RESTRICT prims)
90
0
{
91
0
  prims->alphaComp_argb = general_alphaComp_argb;
92
0
}
93
94
void primitives_init_alphaComp_opt(primitives_t* WINPR_RESTRICT prims)
95
0
{
96
0
  primitives_init_alphaComp(prims);
97
0
  primitives_init_alphaComp_sse3(prims);
98
0
}