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

Bitmap Loading

Started by DanMan May 24, 2002 at 4:25 PM 6 replies 600+ views
Original Post
DanMan
DanMan
I''m trying to load a bitmap into memory without using any MFC or WIN32 functions. I''ve created a structure to hold the header information as follows:
  
struct BITMAPHEADER
{
	char signature[2];		//Should be "BM" if the file is a bitmap

	int fileSize;			//The size of the file in bytes

	int reserved;			//Should be 0

	int dataOffset;			//the data offset (in bytes) to the actual bitmap data

	int size;				//The size of the info header (the next 40 bytes in the file); should always be 40

	int width;				//The width of the bitmap in pixels

	int height;				//The height of the bitmap in pixels

	short planes;			//Should always be 1

	short bitCount;			//Bits per pixel

	int compression;		//The compression used, 0 means no compression (only one we care about)

	int compressedSize;		//The compressed size of the image, 0 if no compression

	int xPixelsPerMeter;	//Horizontal resoultion in pixels/meter

	int yPixelsPerMeter;	//Vertical resolution in pixels/meter

	int colorsUsed;			//The number of colos actually used in the image

	int numImptColors;		//The number of important colors

};
  
I load the header with this code fragement:
  
	bitmapFile.open(mapfile,ios::in | ios::binary );

	//if the file could not be opened

	if(!bitmapFile.is_open())
		return false;

	//read the header in

	bitmapFile.read((char*)&bitmapHeader,sizeof(BITMAPHEADER));

	//if an error occured while reading the header

	if(bitmapFile.fail())
		return false;
  
The signature is loaded fine for every bitmap I''ve tested, but after that, all the values in the header are way waaaaay off. I get widths and heights that are completely different for square bitmaps, reports of a compression type of 2594389 (valid values, as far as I know, are only 0, 1, or 2), etc. Anyone see what I''ve done wrong? ---DanMan
IndirectX
IndirectX
Probably packing gets you. Try this for MSVC:

  
#pragma pack(push, 1)
struct BITMAPHEADER { ... };
#pragma pack(pop)

MSVC will pad structures by default so that all data members occupying <= 8 bytes in memory are aligned on the respective boundaries. For example, fileSize will be aligned on a DWORD boundary, effectively by inserting two pad bytes between signature and fileSize.
---visit #directxdev on afternet <- not just for directx, despite the name
DanMan
DanMan
Yup, you''re exactly right. After I posted, I noticed that the structure was actually occupying 56 bytes at runtime instead of 54. I would have had no clue on how to solve that problem without your help, thanks!

felonius
felonius
I quick glance at your struct shows that you have not done it right. For instance there both a reserved1 and resevered2 field and this may explain the messing up you get.

I have written a bitmap loader for this format that you might want to check out. It doesn''t use any win32 or MFC either. Look at it here (with source and example):

http://www.rolemaker.dk/nonRoleMaker/loader/

Jacob Marner, M.Sc.Console Programmer, Deadline Games
DanMan
DanMan
Actually, mine is working fine now that I solved the alignment problem. According to the file descriptions I downloaded (and I downloaded several while trying to fix this), there should only be one reserved value that is 4 bytes long... Its possible that there should be two different 2 byte reserved fields, but thats not how its listed on the formats that I have. Are you loading an OS/2 bitmap or some other format?

As for not using BITMAPFILEHEADER and BITMAPINFOHEADER, I really wanted to, but I couldn''t find the file they were defined in by looking through the VS.NET documentation. Aside from a single line saying that BITMAPFILEHEADER existed, I couldn''t find anything about it...

---Dan
IndirectX
IndirectX
quote:
Original post by DanMan
As for not using BITMAPFILEHEADER and BITMAPINFOHEADER, I really wanted to, but I couldn''t find the file they were defined in


They live in wingdi.h, at least on my system.
---visit #directxdev on afternet <- not just for directx, despite the name
Rickmeister
Rickmeister
wingdi.h is included by windows.h, so you probably have it already.. Nice feature in VC++ 6.0 (bet it''s there in VC++ .NET too).. Just place your mouse cursor over some variable, right click on it and choose "Go to definition of..." or "Go to reference of..." Makes it easy to explore the windows GUI.. You where using MS VC++ right??

#define Email Lex & Yacc Function Pointers Virtual Terrain Knowledge Base Real Programmers
"Strong typing is for the weak minded!"

Topic Locked

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

Sign in to reply to this topic.