我正在尝试使用 androids ndk 进行一些简单的图像过滤,并且似乎在获取和设置位图的 rgb 值时遇到了一些问题。
我已经剥离了所有实际处理,只是试图将位图的每个像素设置为红色,但我最终得到了一个蓝色图像。我认为我忽略了一些简单的事情,但感谢您提供任何帮助。
static void changeIt(AndroidBitmapInfo* info, void* pixels){
int x, y, red, green, blue;
for (y=0;y<info->height;y++) {
uint32_t * line = (uint32_t *)pixels;
for (x=0;x<info->width;x++) {
//get the values
red = (int) ((line[x] & 0xFF0000) >> 16);
green = (int)((line[x] & 0x00FF00) >> 8);
blue = (int) (line[x] & 0x0000FF);
//just set it to all be red for testing
red = 255;
green = 0;
blue = 0;
//why is the image totally blue??
line[x] =
((red << 16) & 0xFF0000) |
((green << 8) & 0x00FF00) |
(blue & 0x0000FF);
}
pixels = (char *)pixels + info->stride;
}
}
我应该如何获取然后设置每个像素的 rgb 值?
更新答案
如下所示,似乎使用了小端,所以在我的原始代码中,我只需要切换红色和蓝色变量:
static void changeIt(AndroidBitmapInfo* info, void* pixels){
int x, y, red, green, blue;
for (y=0;y<info->height;y++) {
uint32_t * line = (uint32_t *)pixels;
for (x=0;x<info->width;x++) {
//get the values
blue = (int) ((line[x] & 0xFF0000) >> 16);
green = (int)((line[x] & 0x00FF00) >> 8);
red = (int) (line[x] & 0x0000FF);
//just set it to all be red for testing
red = 255;
green = 0;
blue = 0;
//why is the image totally blue??
line[x] =
((blue<< 16) & 0xFF0000) |
((green << 8) & 0x00FF00) |
(red & 0x0000FF);
}
pixels = (char *)pixels + info->stride;
}
}