color compression

Started by
4 comments, last by coderchris 16 years, 3 months ago
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..
Advertisement
This thread has good information on how to pack/unpack a float to/from an RGBA value.
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.
yeah, i really wish I had bit manipulation avaliable, but since im only using shader model 3 i cant..

I did find what I needed in that linked thread however, thanks for that
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;
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;

This topic is closed to new replies.

Advertisement