How does one convert 16-bit RGB565 to 24-bit RGB888?

You want to map each of these from a 5/6 bit space to an 8 bit space.

  • 5 bits = 32 values
  • 6 bits = 64 values
  • 8 bits = 256 values

The code you’re using is taking the naive approach that x5 * 256/32 = x8 where 256/32 = 8 and multiplying by 8 is left shift 3 but, as you say, this doesn’t necessarily fill the new number space “correctly”. 5 to 8 for max value is 31 to 255 and therein lies your clue to the solution.

x8 = 255/31 * x5
x8 = 255/63 * x6

where x5, x6 and x8 are 5, 6 and 8 bit values respectively.

Now there is a question about the best way to implement this. It does involve division and with integer division you will lose any remainder result (round down basically) so the best solution is probably to do floating point arithmetic and then round half up back to an integer.

This can be sped up considerably by simply using this formula to generate a lookup table for each of the 5 and 6 bit conversions.

Leave a Comment