Bitmap Loading

Started by
1 comment, last by SeanHowe 23 years, 10 months ago
I''m trying to write a program that converts Windows 24 bit bitmaps to my own SPR file format. (I''m creating a multiplatform gaming library, so I don''t want to use the Windows loading functions to get images into my game) The thing is, I''m having troubles loading the data. it''s getting messed up somehow. I created a bitmap structure to handle it for me, and here''s the code I have. I''m open to any suggestions as to why this might be (I mean, the data is messed up in a different way then the bitmap being flipped)

class ZAPGBITMAP
{
public:
	WORD identifier; // Identifies type of bitmap, must be BM

	DWORD fileSize;  // Size of the file

	DWORD reserved1; // Reserved - not used


	DWORD dataOffset; // Offset from start of file till data
	DWORD headerSize; // Size of bitmap info header. must be 28h
	
	DWORD width; // Width of bitmap in pixels
	DWORD height; // Height of bitmap in pixels

	WORD planes; // Number of planes in bitmap

	WORD bpp; // Bits per pixel

	DWORD compression; // Type of compresssion

	DWORD bitmapDataSize; // Size of bitmap in bytes
	DWORD hResolution; // Horizontal resolution
	DWORD vResolution; // Vertical resolution

	DWORD colors; // Number of colors, EG, 256, 65536, etc
	DWORD importantColors; // Number of important colours

	DWORD strangeSenselessPacking[4];
	// We skip palette, won''t be reading in any 256 colour bm''s!

	BYTE *bitmapData;
	


	void Release()
	{
		delete []bitmapData;
	}
};



void ReadBitmap(ZAPGBITMAP *bm, FILE *fp)
{
	fread( &bm->identifier, sizeof(WORD), 1, fp);
	fread( &bm->fileSize, sizeof(DWORD), 1, fp);
	fread( &bm->reserved1, sizeof(DWORD), 1, fp);
	fread( &bm->dataOffset, sizeof(DWORD), 1, fp);
	fread( &bm->headerSize, sizeof(DWORD), 1, fp);
	fread( &bm->width, sizeof(DWORD), 1, fp);
	fread( &bm->height, sizeof(DWORD), 1, fp);
	fread( &bm->planes, sizeof(WORD), 1, fp);
	fread( &bm->bpp, sizeof(WORD), 1, fp);
	fread( &bm->compression, sizeof(DWORD), 1, fp);
	fread( &bm->bitmapDataSize, sizeof(DWORD), 1, fp);
	fread( &bm->hResolution, sizeof(DWORD), 1, fp);
	fread( &bm->vResolution, sizeof(DWORD), 1, fp);
	fread( &bm->strangeSenselessPacking, sizeof(DWORD), 4, fp);
	

	bm->bitmapData=new BYTE [bm->bitmapDataSize];
	fread( bm->bitmapData, 1, bm->bitmapDataSize, fp);
}
 
Advertisement
Why are you using all those freads? Just read them all in at once.

Declare two structs:

BITMAPFILEHEADER bitmapfileheader; // ID,file size, pointer to the data
BITMAPINFOHEADER bitmapinfoheader; // image size,width,height,etc.

And read in the entire fileheader first, then the entire infoheader. The
BGR bits for a 24-bit bitmap will follow next. Allocate a buffer and read
all of this in too. Simple.

And remember that the bits are DWORD aligned per scanline if you plan to use
those bits in another format.
Thanx! I''m pretty sure it works now!

This topic is closed to new replies.

Advertisement