对BLENDFUNCTION的理解

本文详细介绍了BLENDFUNCTION结构在Windows API中的使用,包括BlendOp、BlendFlags、SourceConstantAlpha和AlphaFormat成员的作用,以及在UpdateLayeredWindow函数中的具体应用。通过实例解析了不同参数设置下的混合效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://msdn.microsoft.com/en-us/library/aa921335.aspx

基本结构:

typedef struct _BLENDFUNCTION {
  BYTE  BlendOp;
  BYTE  BlendFlags;
  BYTE  SourceConstantAlpha;
  BYTE  AlphaFormat;
}BLENDFUNCTION, *PBLENDFUNCTION, *LPBLENDFUNCTION;

This structure controls blending by specifyingthe blending functionsfor source and destination bitmaps.

这里的 the blending functions指的是哪些函数?

我所知道的有一个:

BOOL UpdateLayeredWindow(   
	HWND hwnd,
	HDC hdcDst,
	POINT *pptDst,
	SIZE *psize,
	HDC hdcSrc,
	POINT *pptSrc,
	COLORREF crKey,
	BLENDFUNCTION *pblend,
	DWORD dwFlags
	);
其中的hdcDst和hdcSrc就是 source and destination bitmaps。

——————————————————————————————

BlendOp

Specifies the source blend operation. Currently, the only source and destination blend operationthat has been defined is AC_SRC_OVER. 

目前只有一种混合模式,就是 AC_SRC_OVER。

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. When you only want to use per-pixel alpha values, set the SourceConstantAlpha value to 255 (opaque) .

这个参数设定的是源图像将以什么不透明度叠加在目标图像上。0则透明,255则不透明。
AlphaFormat

This member controls the way the source and destination bitmaps are interpreted. 

AC_SRC_ALPHA:

This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha). Because this API uses premultiplied alpha, 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 before the call.

以上结构体解释完毕,下面是使用事项。

When the AlphaFormat parameter is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.

32bpp是真彩色。

When the BlendOp parameter 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.

没有alpha通道的话,将按照SourceConstantAlpha 来确定混合效果。混合公式如下:

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)) 

这里的SCA指的是SourceConstantAlpha。

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 by the following equations.

如果源图像没有使用SCA,即默认255,叠加效果就只和他的alpha值有关。

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 equations 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 


UpdateLayeredWindow

UpdateLayeredWindow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值