GDI 中的每像素每组件 Alpha 混合
我有一个带有 R、G、B 颜色通道的 24 位位图和一个带有 R、G、B Alpha 通道的 24 位位图。我想将第一个位图分别与 Alpha 通道进行 Alpha 混合到 GDI 中的 HDC 或 Direct2D 中的 RenderTarget。例如,假设对于一个像素,位图颜色为 (192, 192, 192),HDC 颜色为 (0, 255, 255),Alpha 通道为 (30, 40, 50)。最终的 HDC 颜色应为 (22, 245, 242)。我知道我可以先将 HDC BitBlt 转换为内存 HDC,然后通过手动计算每个像素的颜色进行 alpha 混合,最后再 BitBlt 返回。我只是想避免额外的位块传送并让 API 完成它们的工作(更快,因为它们位于内核空间中)。
我想到的第一个想法是将源位图拆分为 3 个仅红色、仅绿色和仅蓝色的 8 位位图,进行正常的 Alpha 混合,然后将 3 个输出位图合成到 HDC 中。但我找不到在 Windows 中本地进行分割和合成的方法(Direct2D 层有帮助吗?)。此外,分割和合成可能需要许多额外的复制。性能开销会太高。
或者也许可以分 3 次进行 alpha 混合。每一遍对一个通道应用混合,同时保持其他 2 个通道不变。
感谢您的任何评论。
编辑:我发现这个问题,答案应该很好地参考这个问题。然而,除了AC_SRC_OVER之外,不支持其他混合操作。为什么微软不改进他们的 API?
I have a 24-bit bitmaps with R, G, B color channels and a 24-bit bitmap with R, G, B alpha channels. I want to alpha blend the first bitmap to a HDC in GDI or RenderTarget in Direct2D with the alpha channels respectively. For example, suppose for one pixel, the bitmap color is (192, 192, 192), the HDC color is (0, 255, 255) and the alpha channels are (30, 40, 50). The final HDC color should be (22, 245, 242). I know I can BitBlt the HDC to a memory HDC first, do alpha blending by manually calculating the color of each pixel and finally BitBlt back. I just want to avoid the additional blitting and leave APIs do their job (faster since they are in kernel space).
The first idea comes to my mind is to split the source bitmap into 3 red-only, green-only and blue-only 8-bit bitmaps, do normal alpha blending, then composite the 3 output bitmaps into the HDC. But I don't find a way to do the splitting and composition natively in Windows (would Direct2D layer help?). Also, the splitting and compositing may require many additional copying. The performance overhead would be too high.
Or maybe do the alpha blending in 3 passes. Each pass apply the blending for one channel, while maintaining the other 2 unchanged.
Thanks for any comment.
EDIT: I found this question, and the answer should be good reference to this problem. However, besides AC_SRC_OVER, there is no other blending operation supported. Why don't Microsoft improve their API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我非常确定 Windows 或 OSX 上的标准库不支持每通道 alpha。您可能想要独立处理 3 个图层(将每个图层视为单独的 32 位位图),然后组合它们。
I am pretty sure that per-channel alpha is not supported in standard libraries on Windows or OSX. You might want to work with 3 layers independently (treat each layer as separate 32bit bitmap) and than compose them.