Posted on 2010-09-07 14:39
S.l.e!ep.¢% 阅读(908)
评论(1) 编辑 收藏 引用 所属分类:
DirectUI
typedef struct _BLENDFUNCTION {
BYTE BlendOp;
BYTE BlendFlags;
BYTE SourceConstantAlpha;
BYTE AlphaFormat;
} BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;
The BLENDFUNCTION structure controls blending by specifying the blending functions for source and destination bitmaps.
BLENDFUNCTION这种结构的混合控制通过指定源和目标位图的混合功能
BlendOp
The source blend operation. Currently, the only source and destination blend operation that has been defined is AC_SRC_OVER. For details, see the following Remarks section.
指定源混合操作。目前,唯一的源和目标混合,已定义的AC_SRC_OVER; 即根据源alpha值把源图像叠加到目标图像上。OpenGL的alpha混合还支持其他的方式,如常量颜色源。
BlendFlags
Must be zero.
SourceConstantAlpha
Specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values in the source bitmap. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. Set the SourceConstantAlpha value to 255 (opaque) when you only want to use per-pixel alpha values.
指定一个alpha透明度值将用于整个源位图;该SourceConstantAlpha值与任何在源位图的每个像素的alpha值组合;如果设置为0,就会假定你的图片是透明的;如果需要使用每像素alpha值,设置SourceConstantAlpha值255(不透明);
AlphaFormat
This member controls the way the source and destination bitmaps are interpreted. AlphaFormat has the following value.
Value |
Meaning |
AC_SRC_ALPHA |
This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha). Note that the APIs use premultiplied alpha, which means that the red, green and blue channel values in the bitmap must be premultiplied with the alpha channel value. For example, if the alpha channel value is x, the red, green and blue channels must be multiplied by x and divided by 0xff prior to the call. |
AlphaFormat 此成员控制源和目标位图的解释;看以下值:
AC_SRC_ALPHA 这个标志被设置当那位图有Alpha通道(也就是说,每个像素的Alpha);
例如,如果Alpha通道值是x,红色,绿色和蓝色通道必须乘以x和由
0xff分开在调用之前
Remarks
When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.
When the BlendOp member is AC_SRC_OVER, the source bitmap is placed over the destination bitmap based on the alpha values of the source pixels.
If the source bitmap has no per-pixel alpha value (that is, AC_SRC_ALPHA is not set), the SourceConstantAlpha value determines the blend of the source and destination bitmaps, as shown in the following table. Note that SCA is used for SourceConstantAlpha here. Also, SCA is divided by 255 because it has a value that ranges from 0 to 255.
Dst.Red | = Src.Red * (SCA/255.0) | + Dst.Red * (1.0 - (SCA/255.0)) |
Dst.Green | = Src.Green * (SCA/255.0) | + Dst.Green * (1.0 - (SCA/255.0)) |
Dst.Blue | = Src.Blue * (SCA/255.0) | + Dst.Blue * (1.0 - (SCA/255.0)) |
If the destination bitmap has an alpha channel, then the blend is as follows.
Dst.Alpha | = Src.Alpha * (SCA/255.0) | + Dst.Alpha * (1.0 - (SCA/255.0)) |
If the source bitmap does not use SourceConstantAlpha (that is, it equals 0xFF), the per-pixel alpha determines the blend of the source and destination bitmaps, as shown in the following table.
Dst.Red | = Src.Red | + (1 - Src.Alpha) * Dst.Red |
Dst.Green | = Src.Green | + (1 - Src.Alpha) * Dst.Green |
Dst.Blue | = Src.Blue | + (1 - Src.Alpha) * Dst.Blue |
If the destination bitmap has an alpha channel, then the blend is as follows.
Dest.alpha | = Src.Alpha | + (1 - SrcAlpha) * Dst.Alpha |
If the source has both the SourceConstantAlpha (that is, it is not 0xFF) and per-pixel alpha, the source is pre-multiplied by the SourceConstantAlpha and then the blend is based on the per-pixel alpha. The following tables show this. Note that SourceConstantAlpha is divided by 255 because it has a value that ranges from 0 to 255.
Src.Red | = Src.Red | * SourceConstantAlpha / 255.0; |
Src.Green | = Src.Green | * SourceConstantAlpha / 255.0; |
Src.Blue | = Src.Blue | * SourceConstantAlpha / 255.0; |
Src.Alpha | = Src.Alpha | * SourceConstantAlpha / 255.0; |
Dst.Red | = Src.Red | + (1 - Src.Alpha) * Dst.Red |
Dst.Green | = Src.Green | + (1 - Src.Alpha) * Dst.Green |
Dst.Blue | = Src.Blue | + (1 - Src.Alpha) * Dst.Blue |
Dst.Alpha | = Src.Alpha | + (1 - Src.Alpha) * Dst.Alpha |
最后一个字段AlphaFormat有两个选择:0表示常量alpha值,AC_SRC_ALPHA表示每个像素有各自的alpha通道。如果AlphaFormat字段为0,源位图中的所有像素使用同样的常量alpha值,即SourceConstantAlpha字段中的值,该值实际上是0和255,而不是0和1。这里0表示完全透明,255表示完全不透明。目标像素以255-SourceConstantAlpha值作为alpha值。
如果AlphaFormat字段的值是AC_SRC_ALPHA,源设备表面的每个像素必须有各自的alpha通道。即,必须是32-bpp的物理设备上下文,或是选中了32-bpp DDB和DIB段的内存设备上下文。这些情况下,每个源像素有4个8位通道:红、绿、蓝和alpha。每个像素的alpha通道和SourceConstantAlpha字段一起用于把源和目标混合起来。实际用于计算的运算式如下:
Tmp.Red = Src.Red * SourceConstantAlpha / 255;
Tmp.Green = Src.Green * SourceConstantAlpha / 255;
Tmp.Blue = Src.Blue * SourceConstantAlpha / 255;
Tmp.Alpha = Src.Alpha * SourceConstantAlpha / 255;
Beta = 255 – Tmp.alpha;
Dst.Red = Tmp.Red + Round((Beta * Dst.Red )/255);
Dst.Green = Tmp.Green + Round((Beta * Dst.Green)/255);
Dst.Blue = Tmp.Blue + Round((Beta * Dst.Blue )/255);
Dst.Alpha = Tmp.Alpha + Round((Beta * Dst.Alpha)/255);