I’m using libnoise to generate Perlin noise on a 1024×1024 terrain grid. I want to convert its float output to a BYTE between 0 and 255. The question is ultimately a math one: how can I convert a value in the real interval (-1,1) to the integer one (0,255) minimizing loss?
Share
This formula will give you a number in [0, 255] if your input range excludes the endpoints:
If you are including the endpoints (and then it usually written [-1,1] rather than (-1,1)) you will need a special case for
x == 1.0to round that down to 255.