Alpha Blending

Alpha blending is the process of combining a translucent foreground color with a background color, thereby producing a new blended color. The degree of the foreground color's translucency may range from completely transparent to completely opaque. If the foreground color is completely transparent, the blended color will be the background color. Conversely, if it is completely opaque, the blended color will be the foreground color. Of course, the translucency can range between these extremes, in which case the blended color is computed as a weighted average of the foreground and background colors. Fastgraph provides alpha blending functions that work on RGB color values, on direct color bitmaps, and on direct color virtual buffers. The alpha blending functions do not work when a 256-color virtual buffer is active.

The simplest alpha blending function is fg_blend50(). This function computes a 50% alpha-blended color value from the specified foreground and background colors. Its first parameter is the foreground color, and its second parameter is the background color. The fg_blend50() return value is the 50% alpha-blended color. Both the parameters and the function return value use the RGB color encoding scheme associated with the active virtual buffer's color depth. This means you can compute the foreground and background colors with fg_maprgb(), and you can pass the fg_blend50() return value to fg_setcolor(). For example, this code snippet

C/C++:

int BlendedColor, PureGreen, PureBlue;
PureGreen = fg_maprgb(0,255,0);
PureBlue = fg_maprgb(0,0,255);
BlendedColor = fg_blend50(PureGreen,PureBlue);
fg_setcolor(BlendedColor);

Delphi:

BlendedColor, PureGreen, PureBlue : integer;
PureGreen := fg_maprgb(0,255,0);
PureBlue := fg_maprgb(0,0,255);
BlendedColor := fg_blend50(PureGreen,PureBlue);
fg_setcolor(BlendedColor);

Visual Basic:

Dim BlendedColor As Long, PureGreen As Long, PureBlue As Long
PureGreen = fg_maprgb(0, 255, 0)
PureBlue = fg_maprgb(0, 0, 255)
BlendedColor = fg_blend50(PureGreen, PureBlue)
Call fg_setcolor(BlendedColor)

will set the current color to a 50% blend of pure green and pure blue.

The fg_blend() function is similar to fg_blend50(), but it lets you define the foreground color's translucency (called the opacity). The fg_opacity() function takes an integer parameter that defines the opacity for fg_blend() and other alpha blending functions. The opacity can range from 0 to 255, with 0 corresponding to a completely transparent foreground, and 255 to a completely opaque foreground. Hence

C/C++:

fg_opacity(63);
BlendedColor = fg_blend(PureGreen,PureBlue);
fg_setcolor(BlendedColor);

Delphi:

fg_opacity(63);
BlendedColor := fg_blend(PureGreen,PureBlue);
fg_setcolor(BlendedColor);

Visual Basic:

Call fg_opacity(63)
BlendedColor = fg_blend(PureGreen, PureBlue)
Call fg_setcolor(BlendedColor)

will set the current color to a blending of 25% (63/255) pure green and 75% pure blue. Note that defining an opacity of 127 or 128 makes fg_blend() behave approximately the same as fg_blend50().

We may sometimes need to blend individual color values, but in practice it is more common to blend two images. This allows a foreground image to appear with a degree of translucency over a background image. We can alpha blend images to simulate fog, to gradually morph one image into another, or to fade an image to a target color (similar to a 256-color palette fade). We achieve these last two effects by iteratively blending the same two images while gradually increasing or decreasing the opacity.

The fg_blenddcb() function computes an alpha-blended direct color bitmap from the specified foreground and background direct color bitmaps using the current fg_opacity() setting. The first fg_blenddcb() parameter is the name of the array containing the foreground bitmap, and the second parameter is the name of the array containing the background bitmap. The third parameter is the name of the array that will receive the blended bitmap. The final parameter specifies the size (width x height) of each direct color bitmap in pixels.

Suppose we're using a high color virtual buffer and have two 640x480 direct color bitmaps that we wish to blend. We can use fg_blenddcb() to accomplish this:

C/C++:

BYTE Foreground[640*480*2];
BYTE Background[640*480*2];
BYTE Blended[640*480*2];
fg_opacity(127);
fg_blenddcb(Foreground,Background,Blended,640*480);

Delphi:

Foreground : array [1..640*480*2] of byte;
Background : array [1..640*480*2] of byte;
Blended : array [1..640*480*2] of byte;
fg_opacity(127);
fg_blenddcb(Foreground,Background,Blended,640*480);

Visual Basic:

Option Base 1
Dim Foreground(CLng(640) * CLng(480) * 2) As Byte
Dim Background(CLng(640) * CLng(480) * 2) As Byte
Dim Blended(CLng(640) * CLng(480) * 2) As Byte
Call fg_opacity(127)
Call fg_blenddcb(Foreground(1), Background(1), Blended(1),
                 CLng(640) * CLng(480))

We could then display the blended bitmap with fg_putdcb() or another direct color bitmap display function. Each direct color bitmap is 640x480x2 bytes because each high color pixel requires two bytes.

The fg_blenddcb() function uses the same opacity value (as defined in the most recent fg_opacity() call) to blend each pixel. Another function, fg_blendvar(), blends two images with variable intensities for each pixel. Its first two parameters are the same as for fg_blenddcb(), but the third parameter is the name of a 256-color bitmap containing the opacity values for each pixel. The fourth fg_blendvar() parameter is the name of the array that will receive the blended bitmap, and its final parameter is the size of each bitmap in pixels. For example, the following statements compute the same blended bitmap with fg_blendvar() as we did above with fg_blenddcb():

C/C++:

BYTE Opacity[640*480];
memset(Opacity,127,640*480);
fg_blendvar(Foreground,Background,Opacity,Blended,640*480);

Delphi:

Opacity : array [1..640*480] of byte;
FillChar(Opacity,640*480,127);
fg_blendvar(Foreground,Background,Opacity,Blended,640*480);

Visual Basic:

Option Base 1
Dim Opacity(CLng(640) * CLng(480)) As Byte
Call FillMemory(Opacity(1), CLng(640) * CLng(480), 127)
Call fg_blendvar(Foreground(1), Background(1), Opacity(1),
                 Blended(1), CLng(640) * CLng(480))

Of course, the above code snippet doesn't illustrate a typical use of fg_blendvar(). More realistically, we would vary the opacity values in the Opacity array.

The last two alpha blending functions are fg_blendvb() and fg_blendvbv(). These functions are respectively analogous to fg_blenddcb() and fg_blendvar(), but the resulting blended image is instead written to the active virtual buffer; the current graphics position defines the lower left corner of the blending region. The first two fg_blendvb() parameters are the names of the foreground and background bitmap arrays. The last two parameters define the width and height (in pixels) of each bitmap. For example, the code snippet

C/C++ and Delphi:

fg_move(0,479);
fg_blendvb(Foreground,Background,640,480);

Visual Basic:

Call fg_move(0, 479)
Call fg_blendvb(Foreground(1), Background(1), 640, 480)

would write the blended image directly into a 640x480 virtual buffer. The fg_blendvbv() function's first two parameters are also the foreground and background bitmap arrays. Its third parameter is a 256-color opacity array, as used with fg_blendvar(). Its final two parameters define the width and height of each bitmap. This code snippet

C/C++ and Delphi:

fg_move(0,479);
fg_blendvbv(Foreground,Background,Opacity,640,480);

Visual Basic:

Call fg_move(0, 479)
Call fg_blendvbv(Foreground(1),Background(1),Opacity(1),640,480)

would write a variable opacity blended image directly into a 640x480 virtual buffer.

The fg_blendvb() and fg_blendvbv() background parameter can also be NULL (pass nil^ for Delphi, ByVal 0 for Visual Basic, or BYVAL %NULL for PowerBASIC). In this case, these functions get the background pixels from the same area of the active virtual buffer where the blended pixels will be written. This effectively saves an fg_getdcb() call.

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.