I have an image (of size 1024x1024) in an int array (int[] pixels) and I am inverting one channel using the following loop...
int i = 0;
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
int color = pixels[i];
pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color));
i++;
}
}
This takes more than 1 second on my new Galaxy S4 phone. Similar loop runs in a blink of an eye even on an older iPhone. Is there something I am doing wrong here?
If I replace "Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color))" with "Color.BLUE", it gets much faster.
Found a workaround.
If I use my own bitwise operators instead of Color functions, it gets much faster...
int i = 0;
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
int color = pixels[i];
int red = ((color & 0x00ff0000) >> 16);
pixels[i] = (color & 0xff00ffff) | ((255 - red) << 16);
//pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color));
i++;
}
}
You may want to consider using the ColorMatrix class to get this done: http://developer.android.com/reference/android/graphics/ColorMatrix.html
There is most likely considerable overhead involved when manipulating individual pixels the way you are doing it. The ColorMatrix class is your friend.
I think if you replace this piece of code by this, it will be relatively fast
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int pixels[] = new int[w * h];
bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
int n = w * h;
for (int i = 0; i < n; i++) {
int color = pixels[i];
pixels[i] = Color.argb(Color.alpha(color), 255 - Color.red(color), Color.green(color), Color.blue(color));
}
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.