Alpha在C中混合2种RGBA颜色 [英] Alpha Blending 2 RGBA colors in C

查看:115
本文介绍了Alpha在C中混合2种RGBA颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何快速进行alpha混合?

什么alpha混合2种RGBA(整数)颜色的最快方法是什么?

What is the fastest way to alpha blend 2 RGBA (Integer) colors?

请注意,要混合的目标颜色始终是不透明的,只有第二种颜色可以具有不同的颜色

As a note, the target color where to blend is always opaque, only the second color can have different levels of transparency.

我正在尝试找到C语言中最快的方法,同时考虑到混合所产生的最终颜色必须最终不透明且完全不透明(alpha = 0xff)

I am trying to find the fastest way in C, taking into account that the final resulting color from the blend must end up with no transparency, fully opaque (alpha = 0xff)

推荐答案

int blend(unsigned char result[4], unsigned char fg[4], unsigned char bg[4])
{
    unsigned int alpha = fg[3] + 1;
    unsigned int inv_alpha = 256 - fg[3];
    result[0] = (unsigned char)((alpha * fg[0] + inv_alpha * bg[0]) >> 8);
    result[1] = (unsigned char)((alpha * fg[1] + inv_alpha * bg[1]) >> 8);
    result[2] = (unsigned char)((alpha * fg[2] + inv_alpha * bg[2]) >> 8);
    result[3] = 0xff;
}

我不知道它有多快,但是全都是整数。它通过将alpha(和inv_alpha)转换为8.8个定点表示而起作用。不必担心alpha的最小值为1的情况。在这种情况下,fg [3]为0,表示前景是透明的。混合将为1 * fg + 256 * bg,这意味着fg的所有位都将移出结果。

I don't know how fast it is, but it's all integer. It works by turning alpha (and inv_alpha) into 8.8 fixed-point representations. Don't worry about the fact that alpha's min value is 1. In that case, fg[3] was 0, meaning the foreground is transparent. The blends will be 1*fg + 256*bg, which means that all the bits of fg will be shifted out of the result.

您可以非常快地完成操作,实际上,如果将RGBA打包为64位整数。然后,您可以使用单个表达式并行计算所有三种结果颜色。

You could do it very fast, indeed, if you packed your RGBAs in 64 bit integers. You could then compute all three result colors in parallel with a single expression.

这篇关于Alpha在C中混合2种RGBA颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆