简体   繁体   English

将ARGB颜色值反转为ABGR的快速算法?

[英]Fast algorithm to invert an ARGB color value to ABGR?

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 . 我正在使用IntBuffer来操作Bitmap的像素,但缓冲区中的值应该是AABBGGRR ,而颜色常量是AARRGGBB I know I can use Color.argb , Color.a , ... to invert, but I think it's not perfect. 我知道我可以使用Color.argbColor.a ,...来反转,但我认为它并不完美。

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: 我想到了这个Bit Expression,但它不正确:

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. 如果没有更好的,也许我将使用位移操作符(执行Color.a ,...)而不是调用函数来减少时间。

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. 由于A和G已经到位,你可以通过屏蔽B和R然后再添加它们来做得更好。 Haven't tested it but ought to be 95% right: 没有测试过,但应该是95%的权利:

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! 在我看来,以下功能足够快,可以在传递ARGB颜色时返回ABGR颜色, 反之亦然!

int argbToABGR(int argbColor) {
    int r = (argbColor >> 16) & 0xFF;
    int b = argbColor & 0xFF;
    return (argbColor & 0xFF00FF00) | (b << 16) | r;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM