Coverage Report

Created: 2024-05-20 06:11

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