I have a long array of bytes which represents abgr32
pixel data (0xAABBGGRR
) of a big picture.
Is there an efficient way to change the endiness of this long byte array in java?
For example:
Source byte array is [FF,11,22,33,FF,12,34,11,..........
]
Is it possible to convert it to something like this [33,22,11,FF,11,34,12,FF,....
] ?
Efficiency is important.
I have tryed to use ByteBuffer.order(ByteOrder.LITTLE_ENDIAN
) and ByteBuffer.order(ByteOrder.BIG_ENDIAN)
conversion but it didn't help.
Thank you for your help.
You can try doing it manually by swapping the bytes in each group of adjacent 4 bytes. This code assumes that the length of the array is a multiple of 4.
for (int i = 0; i < arr.length; i += 4) {
int tmp;
// swap 0 and 3
tmp = arr[i];
arr[i] = arr[i + 3];
arr[i + 3] = tmp;
// swap 1 and 2
tmp = arr[i + 1];
arr[i + 1] = arr[i + 2];
arr[i + 2] = tmp;
}
Though this is technically a solution to the problem, it may be wiser to find a solution which is not endian-ness sensitive (like some of the comments above).