Archived

This topic is now archived and is closed to further replies.

UBC_Wiskatos

BMP Loading Problems in C++

Recommended Posts

Hi guys, I'm trying to load in a Windows Bitmap (BMP) file in for some processing (not displaying). However, code on the net that I've found all uses Windows, so I took on the task of coding the loader myself. I found a file specification for BMP and made two structures, like so:
        
struct BitmapHeader
{
	long bfType;
	short bfSize;
	long bfReserved1;
	long bfReserved2;
	short bfOffBits;
};

struct BitmapInfo
{
	long biSize;
	long biWidth;
	long biHeight;
	short biPlanes;
	short biBitCount;
	long biCompression;
	long biSizeImage;
	long biXPelsPerMeter;
	long biYPelsPerMeter;
	long biClrUsed;
	long biClrImportant;
};
    
This is used in a class called CBitmap, which has a LoadFile method, like so:
     
void LoadFile(char filename[])
	{
		ifstream bmpFile;
		bmpFile.open(filename, ios::in|ios::binary|ios::ate);
		bmpFile.seekg (0,ios::beg);
		bmpFile.read((char *)(&bmpHeader), sizeof(bmpHeader));
		bmpFile.read((char *)(&bmpInfo), sizeof(bmpInfo));
		bmpFile.close();
	}
        
bmpInfo and bmpHeader are private members of CBitmap, and hold the header and info header of the bitmap. Now, everything works fine, but the problem is, it isn't loading it correctly from the file. I'm absolutely positive I've messed up on my file types in the structs, but I'm not sure how, because a long is 4 bytes, and a short is 2 bytes, and I've followed the specification to the letter. So, if anyone can help me, it would be greatly appreciated. And yes, I realize I haven't loaded the whole BMP in, just the headers. I'm just trying to load these first before attempting to tackle the rest of it. No point in trying to load a whole file if you can't get the headers right, you know? [edited by - UBC_Wiskatos on July 17, 2002 3:20:27 PM]

Share this post


Link to post
Share on other sites
Wow, thanks, it seems this is the problem.

The first header struct BitmapHeader has a size of 20 bytes, which is strange. The second header struct has a size of 40 bytes, which is what is expected.

For some reason, 4 bytes are being added, but I don''t understand why, since they aren''t being added to the second struct.

Share this post


Link to post
Share on other sites
I would agree with the search, except that I feel most explainations of avoiding this issue are too compiler-specific. Therefore, I''ll post a peice of code from CPT that handles the disabling of struct padding.

To use the code (edited, a little):

#include "cpt_padoff_begin.h"

struct UnpaddedStruct {
char Alpha CPT_PADOFF_VAR;
long Beta CPT_PADOFF_VAR;
short Gamma CPT_PADOFF_VAR;
};

#include "cpt_padoff_end.h"


In cpt_padoff_begin.h:

#if defined(__GNUC__)
#define CPT_PADOFF_VAR __attribute__((packed))
#elif defined(_MSC_VER) || defined(__LCC__)
#pragma pack (push, 1)
#elif defined(__BORLANDC__)
#pragma option push -a1
#elif defined(__MWERKS__)
#pragma options align=packed
#endif

#ifndef CPT_PADOFF_VAR
#define CPT_PADOFF_VAR
#endif

In cpt_padoff_end.h:

#ifdef CPT_PADOFF_VAR
#undef CPT_PADOFF_VAR
#endif

#if defined(_MSC_VER) || defined(__LCC__)
#pragma pack (pop)
#elif defined(__BORLANDC__)
#pragma option pop
#elif defined(__MWERKS__)
#pragma options align=reset
#endif


There, now I''ve made this a thread I can link to in the future .

Share this post


Link to post
Share on other sites
Wow, thank you everyone for such a quick response!

It works fine now, thank you, especially Null and Void, I have a feeling I''ll be using those headers in the future.

I just was not aware of padding before, silly me.

Share this post


Link to post
Share on other sites
try this way :


#pragma pack(2)

struct BitmapHeader
{
long bfType;
short bfSize;
long bfReserved1;
long bfReserved2;
short bfOffBits;
};

#pragma pack(4)



it changes the alignment.

Share this post


Link to post
Share on other sites