Skip to main content
GameDev.net gamedev.net
🔒 Locked

BMP Loading Problems in C++

Started by UBC_Wiskatos Jul 17, 2002 at 2:16 PM 7 replies 1.8k views
Original Post
UBC_Wiskatos
UBC_Wiskatos
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]
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]
Null and Void
Null and Void
Check that the structs aren''t being padded (check their ''sizeof'', first of all).

UBC_Wiskatos
UBC_Wiskatos
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.

[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]
Sneftel
Sneftel
Struct padding. Search the forums for a few dozen posts on this.



Don''t listen to me. I''ve had too much coffee.
Null and Void
Null and Void
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 .

UBC_Wiskatos
UBC_Wiskatos
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.
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]
slaveOne
slaveOne
Are classes padded as well or is this specific to structs?
Big Sassy
Big Sassy
UBC_Wiskatos: Just in case you run into trouble loading the rest of it, here''s a thread that explains how to do it without using windows functions.

***********************
          
V3rt3x
V3rt3x
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.