I will try to explain what the function does:
Fisrt the values transp, blue green red are 16bit and reversed eg
blue = 28 = 0b0000000000011100 is actually 0b0011100000000000
Lets assume RGB565 so blue length is 5:
val >> (16 - bf->length) shifts blue 11 places blue = 0b0000000000000111
Then masks 5 - 1 = 4bits,
(val >> (16 - bf->length) & mask) translates to 0b0000000000000111 & 0b0000000000001111
and lastly it shifts where blue is supposed to be in the R G B format
valter