Sign in to follow this  

help: arithmetic on conversion between different color detph

This topic is 4395 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

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

This topic is 4395 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this