我试图在Java中做一些图像混合。在成功完成乘法和屏幕混合后,“正常”混合使我头痛。两个图像与alpha“正常”混合的问题
我在维基百科发现,对于具有alpha混合两个像素的公式为: 钴是得到的彩色,顶部图像的钙色,底图像和AA的Cb彩色 - >顶部的α,抗体 - >底部的阿尔法。
我试过这个翻译到Java,现在有:
(t_X ==从顶部图像组件,B_X ==组件从下图)
float t_a = (topPixels[i] >> 24) & 0xff, t_r = (topPixels[i] >> 16) & 0xff, t_g = (topPixels[i] >> 8) & 0xff, t_b = topPixels[i] & 0xff;
float b_a = (bottomPixels[i] >> 24) & 0xff, b_r = (bottomPixels[i] >> 16) & 0xff, b_g = (bottomPixels[i] >> 8) & 0xff, b_b = bottomPixels[i] & 0xff;
destPixels[i] =
(255 << 24 |
(int)((t_r * t_a) + ((b_r * b_a) * (((255 - t_a)/255)))) << 16 |
(int)((t_g * t_a) + ((b_g * b_a) * (((255 - t_a)/255)))) << 8 |
(int)((t_b * t_a) + ((b_b * b_a) * (((255 - t_a)/255)))) << 0
);
但这似乎是错误的,因为最终的图像出错了。
任何想法,我失踪,或者是我缺乏数学技能再次将公式翻译成代码时获得最好的结果?
t_r,t_a是浮点数,整数(0-255)或...?数学看起来不错,但所有的演员看起来都很怪异('(255 - t_a)/ 255'对于整数不是零)?我想念一些'&0xff'?没有专家,但也许你可以尝试3个整数,检查它们,然后将它们结合在dest中。 – Ishtar 2010-10-04 19:30:57
他们是整数。 – TuomasR 2010-10-04 19:37:01
你可以让JRE为你做好工作。只要将一个图像混合在另一个图像上,一个简单的Graphics.drawImage()就可以实现 - 只要图像是支持alpha的图像(例如BufferedImage.TYPE_INT_ARGB)。还要看看Graphics2D.setComposite()和AlphaComposite.getInstance(),大部分内容已经存在,并且可能会硬件加速。 – Durandal 2010-10-04 19:45:40