Sign in to follow this  

color compression

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

Iv search around but havent been able to turn up anything; basically, I have a standard 8-bit 3-component RGB color value in the range (0-255) or (0-1) however you want to think of it Then i have a single 32-bit float I need to put those 3 color values into this float, and be able to get it back out again. It doesnt even need to be perfect compression; I will accept some loss. Preferably (if there is a way to do this) it doesnt use bit manipulation (&, |, ^) because it all needs to happen in a shader model 3 pixel shader.. Does anyone have any ideas of how this can be compressed? One of the ideas i had of how to compress it is: compressed = red + 255 * green + 65535 * blue But, i dont know how to decompress this..

Share this post


Link to post
Share on other sites
I was going to say use bitshift operators which you didn't mention (<<, >>)

float compressed = R << 24 | (G & 0xff) << 16 | (B & 0xff)

and then for decompression:

R = (compressed >> 24)
G = (compressed >> 16)
B = compressed

(I think thats right)

but I suppose this is still bit manipulation.

Share this post


Link to post
Share on other sites
You actually had the right idea in your original post with this:

compressed = red + 256 * green + 65536 * blue;

Unpacking is slightly trickier since you need to use the fractional part of the floating point for extraction:

red = frac(compressed / 256) * 256;
green = frac(compressed / 65536) * 256;
blue = frac(compressed / 16777216) * 256;

Share this post


Link to post
Share on other sites
Thanks a bunch for that uncompression!!

Thats much more efficient than the crazy thing i had come up with lol


r = (int)((compressed - 256.0f*256.0f) / 256.0f*256.0f);
compressed -= r * 256.0f*256.0f;
g = (int)((compressed - 256.0f) / 256.0f);
compressed -= g * 256.0f;
b = (int)f;

r = (r + 1) / 256.0f;
g = (g + 1) / 256.0f - 1.0f;
b = (b + 1) / 256.0f - 1.0f;

Share this post


Link to post
Share on other sites

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