Jump to content
  • Advertisement
Sign in to follow this  
kenliktron

help: arithmetic on conversion between different color detph

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Color depths come in a lot of varieties. You've got 8-bit indexed (we'll ignore this one), r5g6b5, r5g5b5a1, a8r8g8b8, x8r8g8b8, etc. etc. The only ones where you have to do actual conversion instead of just rearranging the bytes is going from 24- or 32-bit to 16-bit.

To convert from 8-bit to lower bits, shift right:
char r = 128; //half intensity, 8 bits.
r >> 3; //r == 16. Half intensity, 5 bits.
//OR
r >> 2; //r == 32. Half intensity, 6 bits.



~BenDilts( void );

Share this post


Link to post
Share on other sites
Generally, converting from a color depth of m bits to n bits is done like this:
    Cn = Cm * ( 2n-1 ) / ( 2m-1 ) 
However, since you are usually dealing with integers, you must also include rounding:
    Cn = ( Cm * ( 2n-1 ) + ( 2m-1 ) / 2 ) / ( 2m-1 ) 
For example, to convert from 8 bits to 4 bits:
    C4 = ( C8 * 15 + 127 ) / 255 
That gives you the following conversions:
      0    0
8 0
16 1
32 2
64 4
128 8
191 11
219 13
239 14
247 15
255 15
Note: simply shifting as BeanDog suggests will give you poor results (though they may be adequate).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!