Problem with loading heightmaps

Hi,

I have my little terrain renderer up and running now.
Until now I calculated some perlin noise terrain to play
with, but now I wanted to load heightmaps from files (.raw).
Here’s the way I load 8 bit greyscale heightmaps:

    FileInputStream input = new FileInputStream (file);
    data = new byte[(int)file.length()];
    input.read (data);

After this I convert the values to floats and save them
in my heightmap array.
But this doesn’t work, because it seems as if Java handles those
byte as signed bytes. I tried to add 128.0f
if the read value is below 0.0f but that didn’t help either.

Any ideas?

Adam.

to read from a byte array like were they unsigned you have to use a trick:

short byte_val = ((int)bytes[i]) & 0xff;

that way, you will mask out the sign extension, and have an effectively unsigned byte value in the short. That is, to work with an unsigned type, you need the next “bigger” type. unsigned shorts need ints, unsigned ints need longs etc. After that you can convert it as normal:

float height = (float)byte_val;

Hope that helps

  • elias

I’ve found a solution…
I read a value v and do v = (float)Math.sqrt(v*v)

Just that simple :slight_smile:

You’re right elias, your mehtod also works…
I will keep that in mind.

Thanks, Adam.