How can I do replace the following manual logic by http://programming.sirrida.de/perm_fn.html#bit_permute_step ?
unsigned int ConvertRGBAToBGRA(unsigned int v) {
unsigned char r = (v)& 0xFF;
unsigned char g = (v >> 8) & 0xFF;
unsigned char b = (v >> 16) & 0xFF;
unsigned char a = (v >> 24) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
};
Is there a better way to do the above using http://programming.sirrida.de/perm_fn.html#bit_permute_step ?
Yes, namely:
return bit_permute_step(v, 0x000000ff, 16);
The bits of v
indicated by the mask 0x000000ff
contain the r
component, bit_permute_step
will exchange them with the corresponding bits 16 (the distance parameter) places to the left, which corresponds to the b
component of v
. So bit_permute_step(v, 0x000000ff, 16)
will swap the r
with the b
, and hence turn RGBA into BGRA (and also BGRA into RGBA, because a swap is its own inverse).
This could also be found via the permutation calculator: http://programming.sirrida.de/calcperm.php
Use the indices 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31
(source indices) and disable bit-group moving.
A C++ implementation (also usable as C code) of bit_permute_step
for 32-bit integers could be:
uint32_t bit_permute_step(uint32_t x, uint32_t m, uint32_t shift) {
uint32_t t;
t = ((x >> shift) ^ x) & m;
x = (x ^ t) ^ (t << shift);
return x;
}