by MirekCz » Dec 18, 2001 @ 4:38pm
well, you're close, but still you don't understand it completely.<br>R=29,G=50 and B=8 , but you don't add it like R+G+B. 16bpp pixel looks like this<br>RRRRRGGGGGGBBBBB - as you can see R,G and B color values aren't mixed but they take separate bits.<br>example:(I used | to separate R/G/B bits)<br>00001|000010|00001<br>means R=1,G=2,B=1<br>and<br>00110|000000|00000<br>means R=6,G=0,B=0;<br>and<br>11111|111111|00000<br>means R=31,G=63,B=0<br>note color G has 6 bits while R and B have 5 bits, as far as I know it's chosen so because human eye recognizes green color more exact then R/B therefore this color component is most important.<br>and now back to your color, you have got<br>R=29,G=50,B=8<br>that's in binary<br>R=11101 (5 bits)<br>G=110010 (6 bits)<br>B=01000 (5 bits)<br>and now you have to add those values and shift them some bits to have them placed at correct places<br>16bpp R+G+B:(separated with |)<br>11101|110010|01000<br>and how to make such a calculation fast?<br>you use >>/<< bit shifts, << means move one bit left while >> means move one bit right (in practice it means mul by 2/div by 2)<br>so your calculation looks like<br>(R<<11)+(G<<5)+(B) - this makes all the bits appear in correct positions.<br>to fully convert a 24bpp RGB color to 16bpp you make this: (RGB is 24bpp color, RGB2 is 16bpp color)<br>R=RGB>>16; (get R value)<br>G=(RGB>>8)&255; (get G value)<br>B=RGB&255; (get B value)<br>R=R>>3;(8bits->5bits)<br>G=G>>2;(8bits->6bits)<br>B=B>>3;(8bits->5bits)<br>RGB2=(R<<11)+(G<<5)+(B);<br>the above code would be slow... so here'a a much better version<br>RGB2=((RGB>>8)&0xF800)+((RGB>>5)&0x07E0)+((RGB>>3)&0x001F) <br><br>the 0x001F is a hex number, you can use windows calculator to change it into dec/binary value.<br>to show you shortly how the last calculation works and why it uses so little steps I will show you how you convert the G value(do the same for R/B value to understand it better)<br>you start with 24bpp color like this(I use 32bits to describe our number in every stage)<br>00000000RRRRRRRRGGGGGGGGBBBBBBBB<br>now you move it >>5 (5 bits right)<br>0000000000000RRRRRRRRGGGGGGGGBBB<br>perform an AND operation with 0x07E0<br>0x07e0 in binary is:<br>000000000000000000000GGGGGG00000<br>(note, we are left with the most important G bits)<br>now that's a 32bit number, because we're only interested in lower 16bits we will "hide" the other 16bits so we have something like this:<br>00000GGGGGG00000<br>which is what we need for 16bpp RGB color<br>RRRRRGGGGGGBBBBB<br>so you do quite the same for R and B values and you have your 16bpp color. In PPC development you should save all your bitmaps in 16bpp color to save space, because the display is 16bpp anyway so it doesn't make much sense to keep 24bpp images and perform additional calculations to convert it to 16bpp at the end (unless you're doing A LOT of alpha-blending and similar things and you want to have top-quality...)<br><br>hope it helps, I have wrote too much already, there are tons of docs on the net so if you still don't get it just search on google or so. start with byte/bits and hex/dec/binary values conversion and work your way up from there. It's pretty simple once you understand it.Last modification: Mirek Czerwinski - 12/18/01 at 13:38:06
With best regards,
Mirek Czerwinski