gpt4 book ai didi

c++ - 在C/C++中从左到右应用透明渐变

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:20:37 25 4
gpt4 key购买 nike

我正在尝试使用 C/C++ 创建一种算法,该算法将从左到右对像素缓冲区应用统一的透明渐变。如下图所示:

enter image description here

接下来是我的实现。但是生成的图像甚至与我需要达到的效果相去甚远。任何人都可以发现我做错了什么?谢谢

void alphaGradient(uint32_t* pixelsBuffer, const int width, const int height)
{
const short OPAQUE = 255;
int pixelOffsetY, pixelIndex;
short A, R, G, B;

for (int y = 0; y < height; y++)
{
A = OPAQUE;

pixelOffsetY = y * height;

for (int x = 0; x < width; x++)
{
pixelIndex = pixelOffsetY + x;

A = (int)(OPAQUE - ((OPAQUE * x) / width));
R = (pixelsBuffer[pixelIndex] & 0x00FF0000) >> 16;
G = (pixelsBuffer[pixelIndex] & 0x0000FF00) >> 8;
B = (pixelsBuffer[pixelIndex] & 0x000000FF);

pixelsBuffer[pixelIndex] = (A << 24) + (R << 16) + (G << 8) + B;
}
}
}

最佳答案

我还没有尝试过这段代码,但像这样的东西应该可以工作:

void alphaGradient(uint32_t* pixelBuffer, const int width, const int height)
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
const DWORD src = pixelBuffer[i + j * width];
const DWORD dst = MYBACKGROUNDCOLOR;

const unsigned char src_A = (width - i) * 255 / width;
const unsigned char src_R = (src & 0x00FF0000) >> 16;
const unsigned char src_G = (src & 0x0000FF00) >> 8;
const unsigned char src_B = (src & 0x000000FF);

//const unsigned char dst_Alpha = (src & 0xFF000000) >> 24;
const unsigned char dst_R = (dst & 0x00FF0000) >> 16;
const unsigned char dst_G = (dst & 0x0000FF00) >> 8;
const unsigned char dst_B = (dst & 0x000000FF);

const unsigned char rlt_R = (src_R * src_A + dst_R * (255 - src_A)) / 255;
const unsigned char rlt_G = (src_G * src_A + dst_G * (255 - src_A)) / 255;
const unsigned char rlt_B = (src_B * src_A + dst_B * (255 - src_A)) / 255;
//pixelBuffer[i + j*width] = (DWORD)(((255) << 24) | (((rlt_R)& 0xff) << 16) | (((rlt_G)& 0xff) << 8) | ((rlt_B)& 0xff));
// or if you want to save the transparancy then
//pixelBuffer[i + j*width] = (DWORD)(((src_A) << 24) | (((src_R)& 0xff) << 16) | (((src_G)& 0xff) << 8) | ((src_B)& 0xff));
}

}
}

但就个人而言,我会为此尝试使用 DirectX 或 OpenGL 并编写一个好的 PixelShader。这会让这一切变得更快。

关于c++ - 在C/C++中从左到右应用透明渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com