[Scummvm-devel] colorToRGB() implementation

Marcus Comstedt marcus at mc.pp.se
Wed Mar 17 12:21:51 CET 2010


yotam barnoy <yotambarnoy at gmail.com> writes:

> If it's efficiency we're after, then the multiplication is a small part of
> the cost - division is much more expensive (about 4 times more on MIPS).
>
> The problem is that we need to multiply by more correct ratios than our
> shifting simplifications allow. For example to convert a 5 bit channel to 8
> bits, we need to multiply by 255/31 = 8.225 instead of 8, which is what we
> currently do.

Don't get too hooked up on these fractions.  There's a much simpler
way.  What we're doing now is inserting 0 bits in the "loss"
positions.  What we should do, to get the correct result, is to repeat
the most significant bits in those positions.  So in the case of 5 bit
to 8 bit, we should take the top 3 bits of the 5 bit value and insert
to the bottom 3 bit positions. 

  r = (r << 3)|(r >> 2);

No multiplications, divisions or conditionals.  What you get is
actually a multiplication with binary 100.01, or 8.25 in decimal.
This is more than your computed 8.225, but due to the fact that we
always get rounding downwards, we actually get the right result anyway
(the 8.225 value assumes rounding towards nearest integer).


  // Marcus






More information about the Scummvm-devel mailing list