C++ 如何将BGRA缓冲区转换为RGBA缓冲区格式?
如何在C语言中将BGRA缓冲区转换为RGBA缓冲区格式++C++ 如何将BGRA缓冲区转换为RGBA缓冲区格式?,c++,video-streaming,rgba,C++,Video Streaming,Rgba,如何在C语言中将BGRA缓冲区转换为RGBA缓冲区格式++ void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width,int pixel_hight, unsigned char* output) { for (int y = 0; y < pixel_hight; y++) { for (int x = 0; x <
void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width,int pixel_hight,
unsigned char* output) {
for (int y = 0; y < pixel_hight; y++) {
for (int x = 0; x < pixel_width; x++) {
const unsigned char* pixel_in = &input[y * x * 4];
unsigned char* pixel_out = &output[y * x * 4];
pixel_out[0] = pixel_in[2];
pixel_out[1] = pixel_in[1];
pixel_out[2] = pixel_in[0];
pixel_out[3] = pixel_in[3];
}
}
无效GRA和RGBA之间的转换(无符号字符*输入,整数像素\u宽度,整数像素\u高度,
无符号字符*输出){
对于(int y=0;y
但我没有得到背景色
有人能帮我吗?这不是C,所以请适当地重新标记它
假设这是位图数据,首先,您需要计算图像的跨距。跨距是每行像素使用的字节数。这并不总是等于bytes\u per\u pixel*pixels\u per\u row
。它通常与四个字节对齐,因此在这种情况下(因为ARGB像素每像素使用四个字节),您应该可以
其次,您获取像素(x,y)地址的公式是错误的。像素是按行的主要顺序存储的。这意味着,从像素缓冲区中的偏移量0开始,您将看到一整行像素数据;然后是另一整行;依此类推。每行像素数据都有一整行字节
您可以这样做:
但是,如果您的步幅确实等于图像宽度,则无需每次计算地址,因为像素将按顺序存储:
void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width,
int pixel_height, unsigned char* output)
{
int offset = 0;
for (int y = 0; y < pixel_height; y++) {
for (int x = 0; x < pixel_width; x++) {
output[offset] = input[offset + 2];
output[offset + 1] = input[offset + 1];
output[offset + 2] = input[offset];
output[offset + 3] = input[offset + 3];
offset += 4;
}
}
}
void GRA和RGBA之间的转换(无符号字符*输入,整数像素\u宽度,
整数像素(高度,无符号字符*输出)
{
整数偏移=0;
对于(int y=0;y<像素高度;y++){
对于(int x=0;x
如果它仍然显示不正确,请确认正确的像素包装。它应该是ARGB或BGRA;我从来没有听说过像素包装为RGBA。这不是C,因此请适当地重新标记它
假设这是位图数据,首先,您需要计算图像的跨距。跨距是每行像素使用的字节数。这并不总是等于bytes\u per\u pixel*pixels\u per\u row
。它通常与四个字节对齐,因此在这种情况下(因为ARGB像素每像素使用四个字节),您应该可以
其次,您获取像素(x,y)地址的公式是错误的。像素是按行的主要顺序存储的。这意味着,从像素缓冲区中的偏移量0开始,您将看到一整行像素数据;然后是另一整行;依此类推。每行像素数据都有一整行字节
您可以这样做:
但是,如果您的步幅确实等于图像宽度,则无需每次计算地址,因为像素将按顺序存储:
void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width,
int pixel_height, unsigned char* output)
{
int offset = 0;
for (int y = 0; y < pixel_height; y++) {
for (int x = 0; x < pixel_width; x++) {
output[offset] = input[offset + 2];
output[offset + 1] = input[offset + 1];
output[offset + 2] = input[offset];
output[offset + 3] = input[offset + 3];
offset += 4;
}
}
}
void GRA和RGBA之间的转换(无符号字符*输入,整数像素\u宽度,
整数像素(高度,无符号字符*输出)
{
整数偏移=0;
对于(int y=0;y<像素高度;y++){
对于(int x=0;x
如果它仍然显示不正确,那么请确认正确的像素布局。它应该是ARGB或BGRA;我从来没有听说过将像素打包为RGBA。Jeff的awnser可以工作,但有一点细节您从未存储第一个偏移量。 只需添加一个临时值,它就会像为我做的一样发挥魅力:D
void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width, int pixel_height, unsigned char* output)
{
int offset = 0;
for (int y = 0; y < pixel_height; y++) {
for (int x = 0; x < pixel_width; x++) {
auto temp = output[offset];
output[offset] = input[offset + 2];
output[offset + 1] = input[offset + 1];
output[offset + 2] = temp;
output[offset + 3] = input[offset + 3];
offset += 4;
}
}
}
RGBA和GRBA之间的无效转换(无符号字符*输入、整数像素\宽度、整数像素\高度、无符号字符*输出)
{
整数偏移=0;
对于(int y=0;y<像素高度;y++){
对于(int x=0;x
Jeff的awnser可以工作,但有一点细节是你永远不会存储第一个偏移量的。
只需添加一个临时值,它就会像为我做的一样发挥魅力:D
void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width, int pixel_height, unsigned char* output)
{
int offset = 0;
for (int y = 0; y < pixel_height; y++) {
for (int x = 0; x < pixel_width; x++) {
auto temp = output[offset];
output[offset] = input[offset + 2];
output[offset + 1] = input[offset + 1];
output[offset + 2] = temp;
output[offset + 3] = input[offset + 3];
offset += 4;
}
}
}
RGBA和GRBA之间的无效转换(无符号字符*输入、整数像素\宽度、整数像素\高度、无符号字符*输出)
{
整数偏移=0;
对于(int y=0;y<像素高度;y++){
对于(int x=0;x
如果你没有得到背景色,那么你到底得到了什么?给出一个示例值输入和输出你得到的值。你是否验证了你的for循环是否达到了你想要的效果?如果你没有得到背景色,那么你到底得到了什么?给出一个示例值输入和输出你得到的值。有y吗你验证了你的for循环是否在做你想做的事情?