Jump to content
  • Advertisement
Sign in to follow this  
gcmonk

BMP loader

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble reading in the BMP file header. Also, I'm trying not to use windows.h pre-made structures. When I fread() in to my structures, it seems like it skips a few bytes. I read in the initial bytes 0x4D42, but after that, it's way off. My guess was that I was using the wrong data types for the variables. Is there something I'm missing? GBitmap.cpp
#include "GBitmap.h"
#include <iostream>

#define BITMAP_ID 0x4D42

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
GBitmap::GBitmap(char *aFilename) {
	mIsLoaded = true;
	FILE *file;
	file = fopen(aFilename, "rb");
	if(!file) {
		mIsLoaded = false;
		fclose(file);
		return;
	}

	fread(&mBitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, file);
	if(mBitmapFileHeader.type != BITMAP_ID) {
		fclose(file);
		mIsLoaded = false;
		return; 
	}	

	fread(&mBitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, file);

	fseek(file, mBitmapFileHeader.offBits, SEEK_SET);

	mBitmapData = (unsigned char*)malloc(mBitmapInfoHeader.imageSize);

	if(!mBitmapData) {
		fclose(file);
		mIsLoaded = false;
		return;
	}

	fread(mBitmapData, 1, mBitmapInfoHeader.imageSize, file);

	if(!mBitmapData) {
		fclose(file);
		mIsLoaded = false;
		return;
	}
}
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------

GTypes.h
#ifndef GTYPES_H
#define GTYPES_H

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
typedef __int16   USHORT;  // WORD
typedef __int32   UINT;     // DWORD
typedef __int64   LONG;   // LONG
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#endif

GBitmap.h
#ifndef GBITMAP_H
#define GBITMAP_H

#include "GTypes.h"
//#include <windows.h>

typedef struct _BITMAPFILEHEADER {
	USHORT  type;
	UINT		size;
  USHORT  reserved1;
  USHORT  reserved2;
  UINT    offBits;
} BITMAPFILEHEADER;

typedef struct _BITMAPINFOHEADER {
  USHORT  size;
  LONG    width;
  LONG    height;
  USHORT  planes;
  USHORT  bitCount;

  UINT    compression;
  UINT    imageSize;
  LONG    XPelsPerMeter;
  LONG    YPelsPerMeter;
  UINT    clrUsed;
  UINT    clrImportant;
} BITMAPINFOHEADER;

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
/**
 *
 */
class GBitmap {
public:
	GBitmap(char *aFilename);
	~GBitmap();
	bool IsLoaded() { return mIsLoaded; }
//protected:
	bool							mIsLoaded;
	BITMAPFILEHEADER	mBitmapFileHeader;
	BITMAPINFOHEADER	mBitmapInfoHeader;
	unsigned char			*mBitmapData;
};
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#endif

Share this post


Link to post
Share on other sites
Advertisement
Nevermind, solved the problem. To anyone else who may encounter this, the answer was due to byte alignment. Just put:

#pragma pack(1)

somewhere in your code. -_- Now I can sleeeep.....

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!