Skip to main content
gamedev.net
🔒 Locked

Reading a 16bit heightmap

Started by VanKurt 1 reply 350+ views
Original Post
VanKurt
VanKurt
Reading a 8bit grayscale image is easy: You have an array of width*height unsigned chars and read them in. ( fread( chars, sizeof(char), 1, file ) Yesterday I tried to do the same with a 16bit grayscale image but failed. I thought I culd just replace the unsigned char with an unsigned int. Here''s the code for reading a 3 pixel image:
  

FILE* mf = fopen("3pixels.raw","rb");

unsigned int number1 = 0;
unsigned int number2 = 0;
unsigned int number3 = 0;

fread( &number1, sizeof(unsigned int), 1, mf );
fread( &number2, sizeof(unsigned int), 1, mf );
fread( &number3, sizeof(unsigned int), 1, mf );

fclose(mf);
  
But the result of this is not 65535, 32768 and 0 (Pixels were white, gray, black), but 2.15587e+009, 0 and 0. Really strange values!!! Can anyone give me a hint on what I''m doing wrong?
Willm
Willm
Assuming this is on a PC, sizeof( unsigned int ) is 4, not 2. I think you want an ''unsigned short''!
VanKurt
VanKurt
Ouch! That could be true ;-)

(I looked that up in a book, where it said that the size of an int is 2, and I believed the author...)


Thanks!

Topic Locked

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

Sign in to reply to this topic.