Reputation: 1913
I am trying to do some simple image filtering using androids ndk and seem to be having some issues with getting and setting the rgb values of the bitmap.
I have stripped out all the actual processing and am just trying to set every pixel of the bitmap to red, but I end up with a blue image instead. I assume there is something simple that I have overlooked but any help is appreciated.
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;
}
}
How should I both get and then set the rgb values for each pixel??
Update with answer
As pointed out below it seems that little endian is used, so in my original code I just had to switch the red and blue variables:
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;
}
}
Upvotes: 2
Views: 3997
Reputation:
It depends on the pixel format. Presumably your bitmap is in RGBA. So 0x00FF0000 corresponds to the byte sequence 0x00, 0x00, 0xFF, 0x00 (little endian), that is blue with transparency 0.
I am not an Android developer so I don't know if there are helper functions to get/set color components or if you have to do it yourself, based on the AndroidBitmapInfo.format field. You'll have to read the API documentation.
Upvotes: 2