Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
Alpha Blitting is a combination of alpha blending and color keying performed with the IDirectDrawSurface5::AlphaBltmethod. It is primarily used to composite a source foreground image onto a destination background image while preserving alpha channel information.
The source surface for the blit may be either palette-indexed or in a non-palettized format such as RGB or YUV. If the source is palettized, each source pixel is an index into a palette (or color-lookup table) that contains the RBG values for the pixel. These values are retrieved from the palette and blended with the color components of the appropriate destination pixel.
Only non-palettized destination surfaces are supported.
The driver always performs all necessary clipping of the destination rectangle. If a clipper is attached to the destination surface, the driver clips to the clipper's visibility region, otherwise the driver clips to the boundaries of the destination surface.
If the effective source and destination rectangles are different sizes, the image in the source rectangle is stretched or shrunk, as necessary, to fit the destination rectangle. Passing the DDABLT_FILTERENABLE flag to the IDirectDrawSurface5::AlphaBltmethod enables bilinear filtering of the source image. If this flag is not set, no filtering occurs and nearest-neighbor source pixels are copied to the destination.
Two general mechanisms are provided for blending source and destination pixels: color keying and alpha blending. A source color key can be used to designate fully transparent pixels within the source image. A destination color key can be used to designate the regions of the destination surface that can be modified by the blit. Alpha blending is a more sophisticated blending mechanism because it can designate partially transparent source pixels, in contrast to the all-or-none transparency of color keying.
The only alpha-blending operation currently supported by this call is the Src over Destoperation, which is a very useful operation for compositing a source image over a destination image. The operation can be repeated to composite a series of source images in back-to-front order.
A surface that includes per-pixel opacity information in the form of an alpha channel can encode the alpha and color information in one of two formats: premultiplied alphaor non-premultiplied alpha. Alpha-blending operations can use surfaces with premultiplied alpha for both the source and destination, but a non-premultiplied-alpha surface can be specified only as a source. A non-premultiplied-alpha surface can be specified as the destination only if the DDABLT_NOBLEND flag is set.
If the source pixels are in a premultiplied alpha format, the formulas for blending the source and destination components are:
c
D
'
= c
S+ (1 – α
S)*c
D
α
D
'
= α
S+ (1 – α
S)* α
D
where c Sand c Dare the source and destination color components, and α Sand α Dare the source and destination alpha values. These formulas rely on the fact that each color component c Sthat is stored in the source pixel has been premultiplied by the pixel's alpha component α S. If the destination surface contains no alpha channel (or the channel is disabled), only the color components are updated.
If the source pixels are in a non-premultiplied-alpha format, meaning that the source color components have not been premultiplied, they must be multiplied by the source alpha value at the time that the source pixel is blended with the destination pixel. The blending formulas are then:
cD' = αS*cS + (1 – αS)*cD
α
D
'
= α
S+ (1 – α
S)* α
D
The assumption is that only the source surface uses non-premultiplied-alpha. The destination surface must use premultiplied alpha. (Otherwise, c Dwould have to be replaced in the above formula by α D*c Dand then the resulting destination color components would have to be divided by the destination alpha value.)
Additional alpha-blending capabilities are provided in the form of RGBA-scaling factors, which are used to scale the color and alpha components read from a source pixel before blending them with the destination pixel. The RGBA-scaling factors can perform all three of the unary alpha operations: darken, fade, and opaque, which are described in the original paper on alpha blending by Porter & Duff (Siggraph '84).
The RGBA-scaling factors are four 8-bit, fixed-point values in the range 0.0 to 1.0 (represented as 0x00 and 0xff). The first three factors, the color factors: M R, M G, and M B,scale the R, G, and B components read from the source pixel before blending them with the destination. The fourth factor, the alpha factor: Mα,, scales the alpha component read from the source pixel before blending with the destination.
The RGBA-scaling factors are specified in premultiplied alpha format. This means that the application is responsible for scaling the color factor values by the alpha factor value. Special effects are sometimes achieved by violating the rule that a color factor must not exceed the value of the alpha factor. However, this violation is permitted only if the driver is capable of using saturation arithmetic to add the color components to prevent them from overflowing.
The RGBA-scaling factors can be used to perform alpha-blending operations with surfaces whose pixels contain no alpha information. In this case, each source pixel is assumed to be fully opaque, which corresponds to an alpha value of 1.0. After scaling this value by alpha factor Mα, the effective source alpha value is just Mα.
The RGBA-scaling factors are always enabled. Each factor can effectively be disabled, though, by setting it to 1.0, in which case the original value of the corresponding source component is unaltered before it is blended with the destination. All four scaling factors can be disabled by specifying a 0 for the lpDDAlphaBltFX-> ddargbScaleFactorsargument.
The RGBA-scaling factors can be used to control blending for non-RGB surfaces as well. With a YUV pixel format, for instance, the effective YUV factors are calculated from the specified RGB factor values by means of the usual RGB-to-YUV conversion formulas. (The alpha factor is used without modification.) See Converting between YUV and RGB.
By default, the DirectDraw runtime will permit a driver to perform a blit operation only if the driver is capable of performing the specified operation.. For example, if the application specifies distinct values for the red, green, and blue members of the DDALPHABLTFX structure that contains the RGBA scaling factors, the driver must be capable of four-multiplier RGBA scaling. Also, if any color factor is specified to be larger than the alpha factor, the driver must be capable of performing saturated addition during alpha blending to keep the resulting color components from overflowing.
Passing the DDABLT_DEGRADERGBASCALINGflag to IDirectDrawSurface5::AlphaBltoverrides this default behavior. When this flag is set and the driver is unable to perform the operation represented by the specified RGBA-scaling factors, the runtime will degrade the specified RGBA-scaling operation to one the driver can handle. For example, if the driver cannot perform saturated addition, the runtime will clamp the color factors to the value of the alpha factor. Similarly, if the three color factors are not all the same value and the driver is capable of performing only two-factor RGBA scaling, the runtime will set the three color factors to the same value, that is, a weighted average of the color factors specified by the application.
If a null pointer value is specified for the AlphaBltcall's lpDDSrcSurfaceargument, a color-fill operation is performed on the destination surface. The fill color is specified in the dwFillValue member of the DDALPHABLTFXstructure. In addition, if a value other than 255 (fixed-point 1.0) is specified for the alphabits of this value, an alpha-blended fill operation is performed. The alpha value controls the opacity of the source fill color. The driver assumes that the RGB and alpha values have been pre-multiplied by the source alpha. If the driver is capable of performing saturated addition during alpha blending, an application can achieve special effects by setting the color factors larger than the alpha factor. In the case of a destination surface with a non-RGB pixel format (YUV, for example), the source RGB values are automatically transformed to the destination color format for the color-fill operation.
When comparing a pixel to a color key, the driver masks off any unused bits in the pixel before performing the comparison. For example, in a 16-bit pixel in 5-5-5 RGB format, bit 15 is unused. The values written to the unused bits in a destination pixel are undefined.
Source color keying cannot be used with a source surface that has an alpha channel. Similarly, destination color keying cannot be used with a destination surface that has an alpha channel.
Passing the DDABLT_NOBLENDflag to IDirectDrawSurface5::AlphaBltdisables blending and provides a way to initialize the destination surface's alpha and color components. Note that when blending is enabled, blitting a source pixel with alpha and color components that are zero, for example, has no effect on the destination because the pixel is totally transparent. The DDABLT_NOBLENDflag permits zeros (or any other values) to be loaded into the destination alpha and color components.
While the DDABLT_NOBLENDflag is useful for converting between various alpha and non-alpha pixel formats, one combination is not allowed: a premultiplied-alpha source surface and a non-premultiplied destination surface.
When the DDABLT_NOBLENDflag is used with a fill operation (the source surface is NULL), no blending is performed with the fill color. The specified alpha and color values are converted to the destination pixel format and written to the destination surface.
Overlapping rectangles are not supported. In order to perform a blit operation when the source and destination rectangles overlap each other and are from the same surface, the IDirectDrawSurface5::Bltmethod must be used instead.
Last updated on Tuesday, May 18, 2004