GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Feb-2008, 10:40
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 94
Algar will become famous soon enough

Trying to read BMP files, header information is wrong / offset


I am trying to load BMP file information and im using the following structs:

CPP / C++ / C Code:
typedef struct tagBitmapFileHeader 
{
	unsigned short bfType;
	unsigned int bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned int bfOffBits;
} BitmapFileHeader;

typedef struct tagBitmapInfoHeader 
{
	unsigned int biSize;
	int biWidth;
	int biHeight;
	unsigned short biPlanes;
	unsigned short biBitCount;
	unsigned int biCompression;
	unsigned int biSizeImage;
	int biXPelsPerMeter;
	int biYPelsPerMeter;
	unsigned int biClrUsed;
	unsigned int biClrImportant;
} BitmapInfoHeader;

typedef struct tagRGBQuad 
{
	char rgbBlue;
	char rgbGreen;
	char rgbRed;
	char rgbReserved;
} RGBQuad;

I've checked many pages of documentation and they are all consistent (of course) and I've done a sizeof() for each element and they're all the right sizes.

I read the 2 headers with these 2 lines:

CPP / C++ / C Code:
fread(&fHeader, sizeof(BitmapFileHeader), 1, inStream);

fread(&iHeader, sizeof(BitmapInfoHeader), 1, inStream);

But the information is wrong. The type is 19778 which is correct.
The filesize is wrong, I tried on 2 files, one made by MSPaint and the other by photshop. One had a rediculously large number, the other one was 0.

Typically I get 0's for the 2 reserved words in the file header, and the data offset is again usualy rediculously large.

In the image header, the bits per pixel is always stored in the number of planes field that should be before it.

So it kinda looks like im jumping ahead 2 extra words somewhere, but can't figure it out, does anyone have any nice debugging ideas ?
  #2  
Old 22-Feb-2008, 10:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Trying to read BMP files, header information is wrong / offset


Quote:
Originally Posted by Algar
I...and I've done a sizeof() for each element and they're all the right sizes....
DId you do a sizeof() for the structs themselves? Were the structs the right sizes?


Now, different compilers have different ways of letting you try to tell the compiler how to pack a struct. This is implementation-dependent, but I have found the following works for all of my recent GNU, Microsoft and Borland compilers:

CPP / C++ / C Code:
#pragma pack(push, 1)
typedef struct tagBitmapFileHeader 
{
	unsigned short bfType;
	unsigned int bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned int bfOffBits;
} BitmapFileHeader;
.
. /* other headers that you want to pack */
.
#pragma pack(pop)

Look at the sizes of the headers with the pragma in place.

For my compilers without the pragma:
Code:
sizeof(BitmapFileHeader) = 16

With the pragma:
Code:
sizeof(BitmapFileHeader) = 14

(The correct size is 14.)


Regards,

Dave
  #3  
Old 22-Feb-2008, 11:50
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 94
Algar will become famous soon enough

Re: Trying to read BMP files, header information is wrong / offset


Great, works now !

Never worked with structs much. Didn't know it would randomly pack stuff for me withought asking

Thanks a lot
  #4  
Old 22-Feb-2008, 12:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Trying to read BMP files, header information is wrong / offset


Quote:
Originally Posted by Algar
Great, works now !

Didn't know it would randomly pack stuff...

Well, it may be arbitrary, but it isn't necessarily random.

Compilers are free to pick whatever they think is the "best" way to pack structs. (Usually they pick word boundaries that make it faster at the expense of a few bytes extra storage space. That mayb OK "most of the time," but plays havoc when you need to read headers all at once like you have done.)

A reminder: using the #pragma may not work the same with all compilers, so if you have to supply source code for outside (customer) use, there is a risk that it won't work.

However, once you have found that it works for you, then it's OK until and unless you change compilers. Then you test again. (Or, maybe, you could put sizeof() tests at the very beginning of your code and leave them there so that the program would abort if it couldn't pack them correctly.)

The Standard way (not implementation-dependent) is to read each item a byte at a time and pack them into the individual variables as you go. I don't have any qualms about using these pragmas for my own stuff or stuff for internal use, since I always test them against any new or updated compiler that I install.

Regards,

Dave
  #5  
Old 22-Feb-2008, 12:34
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 94
Algar will become famous soon enough

Re: Trying to read BMP files, header information is wrong / offset


greats thanks for the tips. Sound advice
  #6  
Old 24-Feb-2008, 03:00
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Trying to read BMP files, header information is wrong / offset


Not benefit so much from this discussion because i don't know what is pack a struct.

I know structure.

A billion thanks for your help.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pcap programming in C++ goal86sg C++ Forum 2 25-Jan-2006 14:02
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 17:23
Writing good header files mercury C++ Forum 2 15-May-2005 10:41
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:36.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.