Hmmmm, bug in heightmap...

My heightmap loader is giving me crap… It is giving out negative numbers and so on… here is the code, im using 24 bit bitmap heightmap even though i should be using greyscale…
Anyway, code:

// Structure for holding bitmap info
struct Image{
long SizeX;
long SizeY;
char *data;
};
typedef struct Image Image;

/*****************************

  •                       *
    
  •  LOADBMP FUNCTION     *
    
  •                       *
    

*****************************/
void LoadBMP(char *filename, Image *image){
FILE *file;
short int bpp;
short int planes;
long i;
long size;
char temp;

// Open the file
file = fopen(filename, “rb”);
// Seek to the 18th byte in the file
fseek(file, 18, SEEK_CUR);
// Read the size of the texture
i = fread(&image->SizeX, 4, 1, file);
i = fread(&image->SizeY, 4, 1, file);
size = image->SizeX * image->SizeY * 3;
i = fread(&bpp, 2, 1, file);
i = fread(&planes, 2, 1, file);
fseek(file, 24, SEEK_CUR);
image->data = (char *)malloc(size);
// Read the pixel information from the file
i = fread(image->data, size, 1, file);
// Convert BRG pixels into RGB pixels
for(i = 0; i < size; i+=3){
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
fclose(file);
free(image);
}

void generateHeightmapTerrain(void){
int i, i2;
Image *map;
map = (Image *)malloc(sizeof(Image));
LoadBMP(heightmap, map);

for(i = 0; i < TERRAIN_WIDTH; i++){
for(i2 = 0; i2 < TERRAIN_LENGTH; i2++){
terrain_height[i][i2] = (float)map->data[(iTERRAIN_WIDTH3) + i2*3]/HEIGHT_MODIFIER;
}
}
}

Now, im getting negative numbers for some of my heights, and some are larger than 255… ummm, this shouldnt be happening since the bitmap pizel colours are from 0 to 255 only… Can anyone help?

well, the negative numbers problem is probably because you are using chars for your data. try using unsigned char and see if that helps, [or] try casting to unsigned char before casting to float.

[This message has been edited by Indiana (edited 04-21-2002).]

Try changing your
fseek( file, offset, SEEK_CUR )

to
fseek( file, offset, SEEK_SET )

And as Indiana said you have to make your data unsigned char…

If you use grayscale you have 8 or 4 bit data files… This is recomended because you have a file that is three times smaller than a 24 bit pic… A lot faster to load!

What are you making your files with, Photoshop? If you are, then when you create a new pic, in the mode box you select grayscale.