# color compression

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

## 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 on other sites
This thread has good information on how to pack/unpack a float to/from an RGBA value.

##### 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 on other sites
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

##### 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 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;

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×