反转 24 位颜色
Invert 24 bit color
给定一个图像结构中的像素结构数组,我想切换整个图像的红色和蓝色值。我不太确定如何移动它们。它们存储在一个结构中,使得值先是红色,然后是绿色,然后是蓝色。每种颜色都是 8 位,所以大小是 24。到目前为止,这就是我所拥有的。
for (int row = 0; i < image->height;row++) {
for (int column = 0; column < image->width; column++) {
image->pixels[column + row * image->width] = image->pixels[column + row * image->width].red & 0xFFFFFFFF;
image->pixels[column + row * image->width] = (image->pixels[column + row * image->width].blue>>16) & 0xFFFFFFFF;
}
}
你没有显示你的 image
数组的元素是什么类型,但如果它是一个具有 .red
和 .blue
的结构(并且可能是 .green
)字段,你根本不需要任何位移;您可以只交换这些字段中的值。
uint8_t temp = image->pixels[i + j * i].red;
image->pixels[i + j * i].red = image->pixels[i + j * i].blue;
image->pixels[i + j * i].blue = temp;
假设 pixels
是一个 long 数组并且 i + j * i
确实是正确的地址,这应该可以解决问题。使用 &
来隔离字节并移动以将这些部分组合成一个新的 long。
您自己的代码中的问题是您将中间结果分配回像素。通过这样做,您可以更改它,从而影响第二步。如果将中间结果分配给单独的变量,则可以分多个步骤执行。但我更愿意在一次作业中完成:
long pixel = i + j * image->width; // Pixel index
image->pixels[pixel] =
(image->pixels[pixel] & 0xFF) << 16 + // Red, shift to position of blue
(image->pixels[pixel] & 0xFF00) + // Green, stay in place
(image->pixels[pixel] & 0xFF0000) >> 16; // Blue, shift right to position of red.
给定一个图像结构中的像素结构数组,我想切换整个图像的红色和蓝色值。我不太确定如何移动它们。它们存储在一个结构中,使得值先是红色,然后是绿色,然后是蓝色。每种颜色都是 8 位,所以大小是 24。到目前为止,这就是我所拥有的。
for (int row = 0; i < image->height;row++) {
for (int column = 0; column < image->width; column++) {
image->pixels[column + row * image->width] = image->pixels[column + row * image->width].red & 0xFFFFFFFF;
image->pixels[column + row * image->width] = (image->pixels[column + row * image->width].blue>>16) & 0xFFFFFFFF;
}
}
你没有显示你的 image
数组的元素是什么类型,但如果它是一个具有 .red
和 .blue
的结构(并且可能是 .green
)字段,你根本不需要任何位移;您可以只交换这些字段中的值。
uint8_t temp = image->pixels[i + j * i].red;
image->pixels[i + j * i].red = image->pixels[i + j * i].blue;
image->pixels[i + j * i].blue = temp;
假设 pixels
是一个 long 数组并且 i + j * i
确实是正确的地址,这应该可以解决问题。使用 &
来隔离字节并移动以将这些部分组合成一个新的 long。
您自己的代码中的问题是您将中间结果分配回像素。通过这样做,您可以更改它,从而影响第二步。如果将中间结果分配给单独的变量,则可以分多个步骤执行。但我更愿意在一次作业中完成:
long pixel = i + j * image->width; // Pixel index
image->pixels[pixel] =
(image->pixels[pixel] & 0xFF) << 16 + // Red, shift to position of blue
(image->pixels[pixel] & 0xFF00) + // Green, stay in place
(image->pixels[pixel] & 0xFF0000) >> 16; // Blue, shift right to position of red.