Java – a fast algorithm for inverting ARGB color values to ABGR?
I am using intbuffer to operate the pixels of bitmap, but the value in the buffer should be aabbggrr and the color constant is aarrggbb I know I can use color argb,Color. a. ... to reverse, but I don't think it's perfect
I need to manipulate a lot of pixels, so I need an algorithm that can execute this operator in a short time I thought of this bit expression, but it is incorrect:
0xFFFFFFFF ^ pSourceColor
If there is no better, maybe I will use the displacement operator (execute color. A,...) instead of calling the function to reduce the time
Edit:
This is the function I currently convert, although I think there should be a better algorithm (fewer operators) to execute it:
private int getBufferedColor(final int pSourceColor) {
return
((pSourceColor >> 24) << 24) | // Alpha
((pSourceColor >> 16) & 0xFF) | // Red -> Blue
((pSourceColor >> 8) & 0xFF) << 8 | // Green
((pSourceColor) & 0xFF) << 16; // Blue -> Red
}
Solution
Since a and G are already in place, you can do better by shielding B and R and then adding them Not tested, but should be 95% right:
private static final int EXCEPT_R_MASK = 0xFF00FFFF;
private static final int ONLY_R_MASK = ~EXCEPT_R_MASK;
private static final int EXCEPT_B_MASK = 0xFFFFFF00;
private static final int ONLY_B_MASK = ~EXCEPT_B_MASK;
private int getBufferedColor(final int pSourceColor) {
int r = (pSourceColor & ONLY_R_MASK) >> 16;
int b = pSourceColor & ONLY_B_MASK;
return
(pSourceColor & EXCEPT_R_MASK & EXCEPT_B_MASK) | (b << 16) | r;
}
