Skip to main content
GameDev.net gamedev.net
🔒 Locked

float > byte?

Started by barazor Jan 5, 2003 at 3:22 AM 1 replies 600+ views
Original Post
barazor
barazor
i''m using floats to work with RGB data, and i''d like to find out a way to convert them into bytes. the image data has a max of 1.0f and a minimum of -1.0f anybody have the formula to convert this value into an unsigned char? thanks in advance
Dean Harding
Dean Harding
unsigned char byte = (unsigned char)((f + 1.0f) * 127.5f);

You add one to make it go from 0 to 2, then multiply it by 127.5 to make it go from 0 to 255.

I recommend you don''t store your images like this, though. All the conversion between floats and integers will have a pretty big impact on performance. Image a 128x128 image, that''s 128x128x3 float->integer conversions, for a total of just under 50,000. That''s quite a bit for a relatively small image - image a 1024x1024 image...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Landsknecht
Landsknecht
Not to mention the massive precision loss every time you compute the values...


God was my co-pilot but we crashed in the mountains and I had to eat him...
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.