1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Display Management (Display)                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 #define GX_SOURCE_CODE
21 
22 
23 #include "gx_api.h"
24 #include "gx_display.h"
25 
26 #define ALPHAVAL(_c) (GX_UBYTE)((_c) >> 24)
27 #define REDVAL(_c)   (GX_UBYTE)((_c) >> 16)
28 #define GREENVAL(_c) (GX_UBYTE)((_c) >> 8)
29 #define BLUEVAL(_c)  (GX_UBYTE)(_c)
30 
31 #define ASSEMBLECOLOR(_a, _r, _g, _b) \
32     (((_a) << 24)   |                 \
33      ((_r) << 16)   |                 \
34      ((_g) << 8)    |                 \
35      (_b))
36 
37 
38 /**************************************************************************/
39 /*                                                                        */
40 /*  FUNCTION                                               RELEASE        */
41 /*                                                                        */
42 /*    _gx_display_driver_32argb_pixel_blend               PORTABLE C      */
43 /*                                                           6.1.3        */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    Kenneth Maxwell, Microsoft Corporation                              */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    Pixel blend function for 32argb color format.                       */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    context                               Drawing context               */
55 /*    x                                     X coordinate                  */
56 /*    y                                     Y coordinate                  */
57 /*    color                                 Color of line to write        */
58 /*    alpha                                 blending value 0 to 255       */
59 /*                                                                        */
60 /*  OUTPUT                                                                */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    None                                                                */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    GUIX Internal Code                                                  */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
77 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*  12-31-2020     Kenneth Maxwell          Modified comment(s),          */
80 /*                                            fix pixel blend logic,      */
81 /*                                            resulting in version 6.1.3  */
82 /*                                                                        */
83 /**************************************************************************/
_gx_display_driver_32argb_pixel_blend(GX_DRAW_CONTEXT * context,INT x,INT y,GX_COLOR fcolor,GX_UBYTE alpha)84 VOID _gx_display_driver_32argb_pixel_blend(GX_DRAW_CONTEXT *context, INT x, INT y, GX_COLOR fcolor, GX_UBYTE alpha)
85 {
86 
87 GX_UBYTE falpha, fred, fgreen, fblue;
88 GX_UBYTE balpha, bred, bgreen, bblue;
89 GX_UBYTE oalpha;
90 ULONG    bcolor;
91 ULONG   *put;
92 INT      combined_alpha;
93 
94 
95     falpha = ALPHAVAL(fcolor);
96     falpha = (GX_UBYTE)((falpha * alpha) / 255);
97 
98     /* Is the pixel non-transparent? */
99     if (falpha > 0)
100     {
101         /* calculate address of pixel */
102         put = (ULONG *)context -> gx_draw_context_memory;
103         put += context -> gx_draw_context_pitch * y;
104         put += x;
105 
106         /* No need to blend if alpha value is 255. */
107         if (falpha == 255)
108         {
109             *put = (ULONG)fcolor;
110             return;
111         }
112 
113         /* split foreground into alpha, red, green, and blue components */
114 
115         fred = REDVAL(fcolor);
116         fgreen = GREENVAL(fcolor);
117         fblue = BLUEVAL(fcolor);
118 
119         /* read background color */
120         bcolor = *put;
121 
122         /* split background color into red, green, and blue components */
123         balpha = ALPHAVAL(bcolor);
124         bred = REDVAL(bcolor);
125         bgreen = GREENVAL(bcolor);
126         bblue = BLUEVAL(bcolor);
127 
128         /* background alpha is inverse of foreground alpha */
129         combined_alpha = (falpha * balpha) / 0xff;
130 
131         /* blend foreground and background, each color channel */
132         oalpha = (GX_UBYTE)(falpha + balpha - combined_alpha);
133         fred = (GX_UBYTE)((fred * falpha + bred * balpha - bred * combined_alpha) / oalpha);
134         fgreen = (GX_UBYTE)((fgreen * falpha + bgreen * balpha - bgreen * combined_alpha) / oalpha);
135         fblue = (GX_UBYTE)((fblue * falpha + bblue * balpha - bblue * combined_alpha) / oalpha);
136 
137         /* re-assemble into 16-bit color and write it out */
138         *put = (ULONG)ASSEMBLECOLOR(oalpha, fred, fgreen, fblue);
139     }
140 }
141 
142