I’d have to see the exact SDL_GetRGB() routine to fix
the code, but I’ve mentioned this before… it
basically stems from the descrepency of dividing by
2^N and not 2^N-1… this is a slight gyration of
that…
16-bit color is 565 meaning 5 red bits, 6 green bits,
and 5 blue bits… so, white is really 31,63,31 (not
255,255,255)… to normalize it back to 24 bit, the
current function is shifting the most significant bits
into the their correct positions …
if the source range is 0-31 and the dest range is
0-255, then you should multiply by 255, and divide by
31… to optimize this, this is converted into a
multiply by 256, divide by 32, which is the same as a
SHIFT left 8 and a SHIFT right of 5… (or a net shift
of 3)… Doing the actual multiply and divide would
be extremely slow. what I do recommend is if you look
at the bit pattern on the real divide, you’ll notice
that the bits repeat:
Lets take the bit pattern 10101 (or 21 decimal)…
(21*255)/31 is 172.72, so let’s round to 173… 173 is
10101101… (notice the 10101 that we shifted left 3
is right… but the 101 is the 3 MSBs from the original
number…) so… to convert from 0-31 to 0-255
quickly, we can shift left 3, and or that with
shifting right 2…
Appling that to the extremes 00000 (0) becomes
00000000 (0) and 11111(31) becomes 11111111(255)… It
might not be a perfect approximation, but it’s damn
close…
The approach should be easy to generalize at least
down to a 1:2 ratio of bits, and a loop can take care
of the remaining bits…
Email me (offlist) the existing SDL_GetRGB() function
(last I checked it was external to the SDL API in
several incarnations), and I can post the adjusted
function back to the list… (Not sure which FAQ you
found this incarnation of the function in, or who the
maintainer of that FAQ is, but they might want to
update the function there…)
Hope this helped,
-Loren Osborn
Software Engineer
— david roguin wrote:>
I’m tying to get the pixel in red, green, blue
components, using the
getpixel function explain in the examples and using
SDL_GetRGB() to convert
from pixel to r, g, b.
My problem is this: in 16 bpp mode the value
returned from white( 255, 255,
255 ) is r:248, g:252, b:248.
the exact values that say it “wouldn’t” return.
why is that?
Tired of spam? Get advanced junk mail protection
with MSN 8.
http://join.msn.com/?page=features/junkmail
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software