手动将 RGBA 像素与 RGB 像素进行 Alpha 混合
我正在尝试对 RGBA 图像(前景图像)和 RGB 图像(背景图像)进行 alpha 混合操作。然而,在这样做时,我认为我可能做错了 alpha 混合操作或者做错了。例如,我的 RGB 图像的像素是灰色的 (127, 127, 127)。我的 RGBA 图像的像素为 (0, 0, 255)。完成混合操作后,最终颜色将为 (127, 0, 255)。然而,我认为这更多的是一种添加剂混合,与我正在做的操作不同。
对于如何设置我的值,请查看此
incPixelColor[0] = 0; (red)
incPixelColor[1] = 0; (green)
incPixelColor[2] = 255; (blue)
incPixelColor[3] = 255; (alpha)
currentPixelColor[0] = 127; (red)
currentPixelColor[1] = 127; (green)
currentPixelColor[2] = 127; (blue)
对于如何设置我的计算,请查看此
float incAlpha = (currentPixelColor[3]/255.0f);
float red = ((float)incPixelColor[0]/255.0f * incAlpha) + ((float)currentPixelColor[0]/255.0f);
float green = ((float)incPixelColor[1]/255.0f * incAlpha) + ((float)currentPixelColor[1]/255.0f);
float blue = ((float)incPixelColor[2]/255.0f * incAlpha) + ((float)currentPixelColor[2]/255.0f);
currentPixelColor[0] = min(red * 255, 255.0f);
currentPixelColor[1] = min(green * 255, 255.0f);
currentPixelColor[2] = min(blue * 255, 255.0f);
对于没有 alpha 的像素,我希望该值为 (0, 0, 255),然后对于具有 alpha 的图像我希望它能够融入其中。在上面的操作结束时,它将是 (127, 127, 255)。我应该检查每个像素是否都有 alpha,如果有,则进行混合,或者是否有其他方法可以做到这一点?
I am trying to do an alpha blend operation of an RGBA image (foreground image), on to a RGB image (background image). However, while doing so I think I may be doing the wrong alpha blending operation or doing it wrong. For example, the pixel of my RGB image is a grayish color of (127, 127, 127). The pixel of my RGBA image for the pixel will be (0, 0, 255). After I do my blending operation, the final color will be (127, 0, 255). However, I thought that was more of an additive blend and different than the operation I am doing.
For how my values are set, take a look at this
incPixelColor[0] = 0; (red)
incPixelColor[1] = 0; (green)
incPixelColor[2] = 255; (blue)
incPixelColor[3] = 255; (alpha)
currentPixelColor[0] = 127; (red)
currentPixelColor[1] = 127; (green)
currentPixelColor[2] = 127; (blue)
For how my calculation is set, take a look at this
float incAlpha = (currentPixelColor[3]/255.0f);
float red = ((float)incPixelColor[0]/255.0f * incAlpha) + ((float)currentPixelColor[0]/255.0f);
float green = ((float)incPixelColor[1]/255.0f * incAlpha) + ((float)currentPixelColor[1]/255.0f);
float blue = ((float)incPixelColor[2]/255.0f * incAlpha) + ((float)currentPixelColor[2]/255.0f);
currentPixelColor[0] = min(red * 255, 255.0f);
currentPixelColor[1] = min(green * 255, 255.0f);
currentPixelColor[2] = min(blue * 255, 255.0f);
For pixels with no alpha, I would like for the value to be (0, 0, 255) then for images with alpha I would like for it to blend in. At the end of the operation above, it will be (127, 127, 255). Should I check to see if there is alpha for every pixel, and if so, then do the blend or is there another way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
典型的“过度”混合按以下方式完成:
然后对蓝色和绿色通道重复。对每个像素都执行此操作。
A typical "Over" blend is done in the following way:
And then repeating for the blue and green channels. Do this for every pixel.
看来您错过了背景(currentPixelColor)的(1-Alpha)乘数
It seems that you have missed out (1-Alpha) multiplier for background (currentPixelColor)