Discussion:
Some quirks with sizeof(BITMAPFILEHEADER)
(too old to reply)
Jeremy
2004-01-02 03:10:37 UTC
Permalink
This may be in the wrong newsgroup, so let me apologize first.

I would like to ask if anyone else has had a problem attempting to load bitmap files without the aid of the VCL. This is code taken directly from my project, and the output beneath it: (in is defined as an ifstream)

BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;

in.read((char *)&bfh,sizeof(BITMAPFILEHEADER));
in.read((char *)&bih,sizeof(BITMAPINFOHEADER));

cout << "Width: " << bih.biWidth << endl;
cout << "Height: " << bih.biHeight << endl;
cout << "sizeof(BITMAPFILEHEADER) = " << sizeof(BITMAPFILEHEADER) << endl;
cout << "sizeof(BITMAPINFOHEADER) = " << sizeof(BITMAPINFOHEADER) << endl;

Output:
Width: 1141120
Height: 65536
sizeof(BITMAPFILEHEADER) = 16
sizeof(BITMAPINFOHEADER) = 40

That's quite a huge bitmap! The main problem is with sizeof(BITMAPFILEHEADER) though. In wingdi.h (line 769), BITMAPFILEHEADER is defined as such:

typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;

Assuming that a word is 2 bytes and a dword is 4 bytes, this comes to 14, not 16. I'm guessing the compiler is padding everything so that it aligns correctly; however, the information is still wrong, and causes serious problems. I'm wondering, has anyone else run into this problem and knows a fix for it? I'm not adverse to using the actual sizes instead of sizeof(), but it's a hassle. Thanks!

BTW, when I use 14 instead of sizeof(BITMAPFILEHEADER), everything works. :)
Jeremy
2004-01-02 03:19:18 UTC
Permalink
"Jeremy" <***@yahoo.com> wrote:

<question clipped>

Oh, sorry...I should've said this. I've got Borland C++ Builder 6 running on Windows XP Professional.
nicolasr
2004-01-04 02:55:23 UTC
Permalink
Hi,

I had similar problems with BCB6. There seems to be
something wrong with a few header files. The pshpack*
headers seem to disturb structure alignment.

Try this link:

http://www.bcbdev.com/articles/bcb6headers.htm

hth,
Nick
Post by Jeremy
<question clipped>
Oh, sorry...I should've said this. I've got Borland C++ Builder 6 running
on Windows XP Professional.
Tomasz Piasecki
2004-01-02 12:13:14 UTC
Permalink
Post by Jeremy
I would like to ask if anyone else has had a problem attempting to load bitmap files without the aid of the VCL. This is code taken directly from my project, and the output beneath it: (in is defined as an ifstream)
Yes, I've had problems with this too.

It was some time ago so I am nto 100% sure now that I remember results
of my ivestigation well, but I think the problem is that if you include
vcl.h or windows.h for definitions of BITMAPFILEINFOHEADER the 4-bytes
align is used.

I've resolved this by defining my own structure for both
BITMAPFILEHEADER and BITMAPINFOHEADER forcing 1-byte align first.

TP.
--
| _ _ _ |
| _____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | \ | | |o \| \| |/o |/ _\| \ |
| |_|_|_| \_| |__/|_| |_|\__|\__||_| Tomasz Piasecki |
Jeremy
2004-01-02 19:33:22 UTC
Permalink
Post by Tomasz Piasecki
Post by Jeremy
I would like to ask if anyone else has had a problem attempting to load bitmap files without the aid of the VCL. This is code taken directly from my project, and the output beneath it: (in is defined as an ifstream)
Yes, I've had problems with this too.
It was some time ago so I am nto 100% sure now that I remember results
of my ivestigation well, but I think the problem is that if you include
vcl.h or windows.h for definitions of BITMAPFILEINFOHEADER the 4-bytes
align is used.
I've resolved this by defining my own structure for both
BITMAPFILEHEADER and BITMAPINFOHEADER forcing 1-byte align first.
Well, I don't have vcl.h included in my source code. The only two files I have included are windows.h (which includes other windows header files) and gl/gl.h. (This is for a texture mapper, so stuff has to be accurate.)

And yes, when I defined my own structure and forced 1 byte alignment as you said, the sizeof() worked fine. However, I'd rather not do this. Alignment tends to make programs faster, and I need all the speed I can get. :) Maybe I'll just write some #define's or const int's for the structure sizes.

Thanks!

-- JT
Tomasz Piasecki
2004-01-02 21:57:57 UTC
Permalink
Post by Jeremy
And yes, when I defined my own structure and forced 1 byte alignment
as you said, the sizeof() worked fine. However, I'd rather not do
this. Alignment tends to make programs faster, and I need all the
speed I can get. :)
So change alignment only for type definition of this particular structure:

#pragma pack(push,1) //push default alignment and set it to 1
struct TMyBitmapFileHeader
{
...
};
#pragma pack(pop) //pop default alignment

TP.
--
| _ _ _ |
| _____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | \ | | |o \| \| |/o |/ _\| \ |
| |_|_|_| \_| |__/|_| |_|\__|\__||_| Tomasz Piasecki |
Damon Chandler (TeamB)
2004-01-10 06:32:17 UTC
Permalink
Jeremy,
Post by Jeremy
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
Width: 1141120
Height: 65536
I had the same problems a few months back; cost me a several hours of
needless debugging. Download and install Update 4 for BCB6--that should
fix the problem.

Good luck,
Damon (TeamB)

Loading...