Hi!
I'm trying to program my bmp file loader, but I've got strange problem under Linux. I compiled 2 nearly the same codes. One under Window$ and another under Linux. The problem is that in Linux there is no BITMAPFILEHEADER - it's in windows.h. So i've written it on my own -> I downloaded the description of bmp file format and written my own structure. However it didn't work...
Here are the codes:
For Windows:
Code:
#include <cstdlib>
#include <iostream>
#include "stdio.h"
#include "windows.h"
using namespace std;
int main(int argc, char *argv[])
{
FILE *bmp = fopen("test.bmp", "rb");
if(!bmp)
return 1;
BITMAPFILEHEADER bfh;
fread(&bfh, sizeof(BITMAPFILEHEADER), 1, bmp);
printf("Sizeof Type: %d ; %d\n", sizeof(bfh.bfType), bfh.bfType);
printf("Sizeof Size: %d ; %d\n", sizeof(bfh.bfSize), bfh.bfSize);
printf("Sizeof reserved1: %d ; %d\n", sizeof(bfh.bfReserved1), bfh.bfReserved1);
printf("Sizeof reserved2: %d ; %d\n", sizeof(bfh.bfReserved2), bfh.bfReserved2);
printf("Sizeof OffsetBits: %d ; %d\n", sizeof(bfh.bfOffBits), bfh.bfOffBits);
fclose(bmp);
system("PAUSE");
return EXIT_SUCCESS;
}
It works under dev-c++
Version for Linux:
Code:
#include "stdio.h"
typedef struct BITMAPFILEHEADER {
short bfType;
int bfSize;
short Reserved1;
short Reserved2;
int bfOffBits;
};
typedef struct BITMAPINFOHEADER {
int biSize;
long biWidth;
long biHeight;
};
int main()
{
FILE *bmp = fopen("test.bmp", "rb");
if(!bmp)
{
printf("Couldn't open file!\n");
return 1;
}
BITMAPFILEHEADER myBmp;
BITMAPINFOHEADER myH;
fread(&myBmp, sizeof(BITMAPFILEHEADER), 1, bmp);
fread(&myH, sizeof(BITMAPINFOHEADER), 1, bmp);
printf("bfType sizeof %d : %d\n", sizeof(myBmp.bfType), myBmp.bfType);
printf("bfSize sizeof %d: %d\n", sizeof(myBmp.bfSize), myBmp.bfSize);
printf("r1 sizeof %d: %d\n", sizeof(myBmp.Reserved1), myBmp.Reserved1);
printf("r2 sizeof %d: %d\n", sizeof(myBmp.Reserved2), myBmp.Reserved2);
printf("bfOffbits sizeof %d: %ul\n", sizeof(myBmp.bfOffBits), myBmp.bfOffBits);
printf("----------------------------\n");
printf("biSize: %d\n", myH.biSize);
printf("biWidth: %d\n", myH.biWidth);
printf("biHeight: %d\n", myH.biHeight);
fclose(bmp);
return 0;
}
(BITMAPINFOHEADER is not ended yet but it's not important now)
And the results for the same bmp file:
Windows:
Sizeof Type: 2 ; 19778
Sizeof Size: 4 ; 1179702
Sizeof reserved1: 2 ; 0
Sizeof reserved2: 2 ; 0
Sizeof OffsetBits: 4 ; 54
And that's ok.
But for Linux:
[victor@localhost bmp_loader]$ ./bmp
bfType sizeof 2 : 19778
bfSize sizeof 4: 18
r1 sizeof 2: 0
r2 sizeof 2: 54
bfOffbits sizeof 4: 2621440l
First values are ok (these are shorts) but the rest is wrong.
As you can see I also checked whether sizes of types in my structure are ok, and they are.
So could anyone help me and explain why it doesn't work?
I know it's a mistake somewhere in my structure but it should work if type sizes are same, shouldn't it?
thx
Victor