grichter opened this issue on Sep 11, 2007 · 15 posts
kuroyume0161 posted Wed, 12 September 2007 at 1:04 AM
All that should be happening in one direction is:
r = n/255 where n is in RGB (integer) to go to a real value between 0.0 and 1.0 (so 0 = 0.0 and 255 = 1.0)
and, obviously, in the other direction:
n = r * 255 where r is the real value between 0.0 and 1.0 and n is between 0 and 255.
It may be that they are not rounding properly. The default behaviour for n = r * 255 is to truncate when converting a real value to an integer. So, even 19.99999 = 19 in this scenario. The proper way to do it like this is to take the decimal part and add 1 to n if the decimal part is greater than or equal to 0.5.
float m = r * 255;
int n = (int)n;
float r = (float)(m - n);
if (r >= 0.5) ++n;
It's not as much a 'deep CPU thing' as an implementation thing with conversion. Standard methodologies are clear on rounding procedures when going from floating point to integer approximations or equivalents.
C makes it easy to shoot yourself in the
foot. C++ makes it harder, but when you do, you blow your whole leg
off.
-- Bjarne
Stroustrup
Contact Me | Kuroyume's DevelopmentZone