gpt4 book ai didi

c++ - 了解 Windows 中的 AlphaBlending

转载 作者:行者123 更新时间:2023-11-28 05:53:20 25 4
gpt4 key购买 nike

我有一个 32bpp 的位图图像,它有一个 alpha channel ,其值范围为 0-255。我正在尝试使用 win32 API 在窗口中显示它(附加了我用来显示的代码片段)。

我正在阅读文档,结果发现对于混合,在我想使用每像素值的情况下,Windows 使用以下公式:

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.Red     =  Src.Alpha*Src.Red    + (1 - Src.Alpha) * Dst.Red
Dst.Green = Src.Alpha*Src.Green + (1 - Src.Alpha) * Dst.Green
Dst.Blue = Src.Alpha*Src.Blue + (1 - Src.Alpha) * Dst.Blue

因为这就是产生叠加效果(半透明)的原因。

我的期望是否正确?如果是,为什么 Windows 以这种方式进行混合?我在这里缺少什么?

我用来绘制分层窗口的代码片段:

            bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 255;
bf.AlphaFormat = AC_SRC_ALPHA;
POINT ptOrigin = { 0, 0 };
SIZE windowSize = { 300, 300 };

POINT ptZero = { 0, 0 };

UpdateLayeredWindow(m_sThis->m_hWnd, dc, &ptOrigin, &windowSize,
hdc, &ptZero, RGB(255, 255, 255), &bf, ULW_ALPHA);

最佳答案

来自documentation :

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.

所以,你是对的,但你之前必须手工完成。

关于c++ - 了解 Windows 中的 AlphaBlending,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34736785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com