【发布时间】:2021-08-26 09:12:34
【问题描述】:
我有一个混合像素的功能,但是新像素的 alpha 值是错误的。 Iv 过去也尝试了几种不同的方法来解决此问题,但始终无法找出生成正确 alpha 值的正确算法。
我正在尝试做的一个例子。在 testImage1 上绘制 testImage2。 根据背景颜色在stackoverflow上查看图像时,问题可能并不明显。尝试使用实际的图像查看器查看图像。
希望有一个不使用 float 或 double 类型的解决方案。第一个参数是指向背景像素的指针,而第二个参数是前像素的实际像素值。第三个参数只是调整整个像素的 alpha 值。
testImage1:
testImage2:
预期结果:
实际结果:
代码:
struct PIXEL
{
unsigned char Blue;
unsigned char Green;
unsigned char Red;
unsigned char Alpha;
};
void RenderAlphaPixel(PIXEL* pDestination, U32 Color, unsigned char Alpha)
{
signed int Alpha2, Remainder, Red, Green, Blue, Alpha3;
Alpha2 = (Color >> (8 * offsetof(PIXEL, Alpha)));
Alpha2 -= 255;
Alpha2 += Alpha;
if (Alpha2 < 0) Alpha2 = 0;
Remainder = 255 - Alpha2;
Red = (unsigned char)(Color >> (8 * offsetof(PIXEL, Red)));
Green = (unsigned char)(Color >> (8 * offsetof(PIXEL, Green)));
Blue = (unsigned char)(Color >> (8 * offsetof(PIXEL, Blue)));
Alpha3 = (unsigned char)(Color >> (8 * offsetof(PIXEL, Alpha)));
pDestination->Red = (unsigned char)(((pDestination->Red * Remainder) + (Red * Alpha2)) / 255);
pDestination->Green = (unsigned char)(((pDestination->Green * Remainder) + (Green * Alpha2)) / 255);
pDestination->Blue = (unsigned char)(((pDestination->Blue * Remainder) + (Blue * Alpha2)) / 255);
// todo. Fix me!
pDestination->Alpha = (unsigned char)(((pDestination->Alpha * Remainder) + (Alpha3 * Alpha2)) / 255);
}
为 WENDYN 编辑
当降低正面图像的 Alpha 时,图像会变得非常暗。在 windows 照片查看器中查看时很容易看到问题。
预期结果:
实际结果:
【问题讨论】:
-
两张图片的 alpha 不都是用 Alpha 插值的吗?我认为您不应该将 Color 的 alpha 添加到那里的计算中。
标签: c graphics alphablending