I'm using IntBuffer
to manipulate pixels of a Bitmap, but the value in the buffer should be AABBGGRR, while the color constants are AARRGGBB. I know I can use Color.argb
, Color.a
, ... to invert, but I think it's not perfect.
I need to manipulate a very large number of pixels, so I need an algorithm that can perform this operator in short time. I think of this Bit Expression, but it's not correct:
0xFFFFFFFF ^ pSourceColor
If there's no better one, maybe I will use bit-shift operators (that performs Color.a
, ...) instead of calling the functions to reduce the time.
EDIT:
This is my current function to convert, though I think there shoul be a better algorithm (less operators) to perform 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
}
Since A and G are in place, you can probably do a little better by masking off the B and R and then adding them back. Haven't tested it but ought to 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;
}
In my opinion, the following function is fast enough to return the ABGR color while passing an ARGB color and vice-versa!
int argbToABGR(int argbColor) {
int r = (argbColor >> 16) & 0xFF;
int b = argbColor & 0xFF;
return (argbColor & 0xFF00FF00) | (b << 16) | r;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With