[Scummvm-devel] colorToRGB() implementation
yotam barnoy
yotambarnoy at gmail.com
Wed Mar 17 12:39:41 CET 2010
Elegant and very impressive!
On Wed, Mar 17, 2010 at 1:21 PM, Marcus Comstedt <marcus at mc.pp.se> wrote:
>
> 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
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scummvm.org/pipermail/scummvm-devel/attachments/20100317/16ce53ab/attachment.html>
More information about the Scummvm-devel
mailing list