BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Some quirks with sizeof(BITMAPFILEHEADER)

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Graphics)
View previous topic :: View next topic  
Author Message
Jeremy
Guest





PostPosted: Fri Jan 02, 2004 3:10 am    Post subject: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote




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. Smile
Back to top
Jeremy
Guest





PostPosted: Fri Jan 02, 2004 3:19 am    Post subject: Re: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote




"Jeremy" <nychold (AT) yahoo (DOT) com> wrote:

<question clipped>

Oh, sorry...I should've said this. I've got Borland C++ Builder 6 running on Windows XP Professional.
Back to top
Tomasz Piasecki
Guest





PostPosted: Fri Jan 02, 2004 12:13 pm    Post subject: Re: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote



Jeremy wrote:

Quote:
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.

--
Quote:
_ _ _ |
_____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | | |o | | |/o |/ _| |
|_|_|_| _| |__/|_| |_|__|__||_| Tomasz Piasecki |


Back to top
Jeremy
Guest





PostPosted: Fri Jan 02, 2004 7:33 pm    Post subject: Re: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote


Tomasz Piasecki <mtbrider (AT) _-nospam-_ (DOT) poczta.onet.pl> wrote:
Quote:
Jeremy wrote:

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. Smile Maybe I'll just write some #define's or const int's for the structure sizes.

Thanks!

-- JT

Back to top
Tomasz Piasecki
Guest





PostPosted: Fri Jan 02, 2004 9:57 pm    Post subject: Re: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote

Jeremy wrote:
Quote:
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. Smile
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.

--
Quote:
_ _ _ |
_____ _| |_| | __ (o) | | __ __ @poczta.onet.pl |
| | | |o | | |/o |/ _| |
|_|_|_| _| |__/|_| |_|__|__||_| Tomasz Piasecki |


Back to top
nicolasr
Guest





PostPosted: Sun Jan 04, 2004 2:55 am    Post subject: Re: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote

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

Quote:
"Jeremy" <nychold (AT) yahoo (DOT) com> wrote:

question clipped

Oh, sorry...I should've said this. I've got Borland C++ Builder 6 running
on Windows XP Professional.




Back to top
Damon Chandler (TeamB)
Guest





PostPosted: Sat Jan 10, 2004 6:32 am    Post subject: Re: Some quirks with sizeof(BITMAPFILEHEADER) Reply with quote

Jeremy,
Quote:
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)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Graphics) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.