Sign in to follow this  
gcmonk

BMP loader

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this