Hello,

Ive been at this for hours and cant figure out where its going wrong. Im simply trying to open a bitmap read its header information into a struct and display it. Ive checked around and think this structure is correct for what im trying to do. On my system short is 2 bytes and long is 4 bytes. The only thing which is correct in my output is the Type at the top, after that the figures are not correct. Im compiling on a linux box using gcc and am trying to read a small bitmap made in ms paint.

Could someone have a look at this and advise where im going wrong. Ive really tried to figure out for myself and considered its something to do with padding maybe ? This is the first time ive messed around with file headers though so could use some advice.

Thank You.

Stan.

Code:
#include <stdio.h>
#include <stdlib.h>

struct BitMap
{
  short Type;
  long Size;
  short Reserve1;
  short Reserve2;
  long OffBits;
  long biSize;
  long biWidth;
  long biHeight;
  short biPlanes;
  short biBitCount;
  long biCompression;
  long biSizeImage;
  long biXPelsPerMeter;
  long biYPelsPerMeter;
  long biClrUsed;
  long biClrImportant;
} Header;

int main( void )
{
  FILE *BMPFile = fopen ("paint.bmp", "rb");

  if (BMPFile == NULL)
    return;
  
  memset(&Header, 0, sizeof(Header));
  
  fread(&Header, 54, 1, BMPFile);

  printf("\nType:%d\n", Header.Type);
  printf("Size:%d\n", Header.Size);  
  printf("Reserve1:%d\n", Header.Reserve1);  
  printf("Reserve2:%d\n", Header.Reserve2);
  printf("OffBits:%d\n", Header.OffBits); 
  printf("biSize:%d\n", Header.biSize);      
  printf("Width:%d\n", Header.biWidth);    
  printf("Height:%d\n", Header.biHeight);  
  printf("biPlanes:%d\n", Header.biPlanes);  
  printf("biBitCount:%d\n", Header.biBitCount);
  printf("biCompression:%d\n", Header.biCompression);  
  printf("biSizeImage:%d\n", Header.biSizeImage);  
  printf("biXPelsPerMeter:%d\n", Header.biXPelsPerMeter);  
  printf("biYPelsPerMeter:%d\n", Header.biYPelsPerMeter);  
  printf("biClrUsed:%d\n", Header.biClrUsed);  
  printf("biClrImportant:%d\n\n", Header.biClrImportant);  

  fclose(BMPFile);
  
  return 0;
}
Output :

Code:
Type:19778
Size:1
Reserve1:0
Reserve2:118
OffBits:2621440
biSize:33554432
Width:25165824
Height:65536
biPlanes:4
biBitCount:0
biCompression:-2147483648
biSizeImage:247726081
biXPelsPerMeter:247726080
biYPelsPerMeter:0
biClrUsed:0
biClrImportant:0