Loading BMP problem.

P

Pavlo Pelekh

I wroted this piece of code :
#include <stdio.h>
#include <malloc.h>

FILE * f;

typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
LONG width;
LONG height;
struct BITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} h1;
struct BITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
};

struct BITMAPINFO{
BITMAPINFOHEADER bmiHEADER;
//DWORD test;
} h2;
int main() {
// open file to read
f = fopen("1.bmp", "rb");

// read BITMAPFILEHEADER
fread( &h1 , sizeof(h1), 1, f);
char * string = & h1.bfType;
printf ("%s\n", string);

width = h1.bfSize;
printf("%ul \n", width);
}

it should print to stdout h1.bfType it's type of file: output is BM6;
and size of file. I see 91 or something like this, but should be 38386. If
anybody could help?

Thanks
 
K

Karl Heinz Buchegger

Pavlo said:
// read BITMAPFILEHEADER
fread( &h1 , sizeof(h1), 1, f);
char * string = & h1.bfType;
printf ("%s\n", string);

width = h1.bfSize;
printf("%ul \n", width);
}

it should print to stdout h1.bfType it's type of file: output is BM6;

If you printf something with %s then you better should make sure
that the thing you output is indeed a C-style string. That is:
a sequence of charcaters, terminated with a '\0' character. The
bfType field does not quilify for that.
and size of file. I see 91 or something like this, but should be 38386. If
anybody could help?

In this structure, the bfSize member denotes the entire file size. Most
bitmap files contain a wrong value or simply are 0. Thats mainly because
no one really needs that value for anything.
 
P

Pavlo Pelekh

Karl said:
In this structure, the bfSize member denotes the entire file size. Most
bitmap files contain a wrong value or simply are 0. Thats mainly because
no one really needs that value for anything.

I looked this file with editor in Hex and I definetlely can say, that there
was size in that field. I converted it to dec and it was the same as $file
1.bmp returned me.
 
K

Karl Heinz Buchegger

Pavlo said:
I looked this file with editor in Hex and I definetlely can say, that there
was size in that field. I converted it to dec and it was the same as $file
1.bmp returned me.

You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )




--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: (e-mail address removed) Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
 
K

Karl Heinz Buchegger

Karl said:
You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )

Sorry, hit send accidently

#pragma pack( push, 1 )

struct BITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;

....

struct BITMAPINFO{
BITMAPINFOHEADER bmiHEADER;
//DWORD test;
} h2;

#pragma pack( pop )

Note that #pragma is compiler dependent by definition
 
G

Gernot Frisch

Karl Heinz Buchegger said:
Sorry, hit send accidently

#pragma pack( push, 1 )

struct BITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;

....

struct BITMAPINFO{
BITMAPINFOHEADER bmiHEADER;
//DWORD test;
} h2;

#pragma pack( pop )

Note that #pragma is compiler dependent by definition

Yep, had exaclty the same problem once... <remembers a horrible night
of debugging>
-Gernot
 
M

Malte Starostik

Karl said:
You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )

As the OP mentioned file (1), I assume he's probably on a POSIX system.
FWIW, for gcc the corresponding syntax is:

struct Foo __attribute__ ((__packed__))
{
//...
};

Cheers,
Malte
 
P

Pavlo Pelekh

Thanks Karl for help. Now I see.:)
Karl Heinz Buchegger said:
You are fooled by padding.
Search for a compiler directive to turn of padding for that structure.
Eg. for VC++ this would be

#pragma pack( push, 1 )




--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: (e-mail address removed) Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

WAV to BMP 9
I can see the music! 2
Newbie read image help 6
Confused Newbie 3
What can cause a fopen call to lock up? 2
BitCount Help 1
Converting to ARGB and writing to a bitmap 2
screen capture problem 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top