PDA

View Full Version : Strange problem with bmp



Victor
04-04-2005, 02:35 PM
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:


#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:


#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

Perspective
04-04-2005, 02:40 PM
look here:
http://cboard.cprogramming.com/showthread.php?t=55620

You essentially need to #pragma pack(1)

Victor
04-04-2005, 02:48 PM
Thanks, it works!
:)
Now I must look for some info about #pragma pack(1) and learn what does it exactly do....

btw. your website is really nice :) but you should upload your projects source codes so I could learn more :>

Oh, and one more thing. Is/Should bitmap identifier (in structure it's type) always be equal to 19778?