Skip to main content
gamedev.net
🔒 Locked

loading bitmap, weeiiiird....

Started by pag May 14, 2003 at 4:29 PM 1 replies 300+ views
Original Post
pag
pag
Im going nuts on this, im writing a converter of some sorts(not telling ) anyways I can''t get it to read a bitmap properly or at all..grr.. my code:>
  

typedef unsigned short WORD;
typedef unsigned long  DWORD;
typedef long           LONG;

struct BITMAPFILEHEADER
{
	WORD	bfType;
	DWORD	bfSize;
	WORD	bfReserved1;
	WORD	bfReserved2;
	DWORD	bfOffBits;
};



struct BITMAPINFOHEADER
{
	DWORD	biSize;
	LONG	biWidth;
	LONG	biHeight;
	WORD	biPlanes;
	WORD	biBitCount;
	DWORD	biCompression;
	DWORD	biSizeImage;
	LONG	biXPelsPerMeter;
	LONG	biYPelsPerMeter;
	DWORD	biClrUsed;
	DWORD	biClrImportant;
};

void LoadBitmap(const char* pfileName)
{
	FILE*				pfile;
	BITMAPFILEHEADER	bf;
	BITMAPINFOHEADER	bi;
	int len = 0;

	pfile = fopen(pfileName, "rb");

	fread(&bf, sizeof(bf), 1, pfile);
	fread(&bi, sizeof(bi), 1, pfile);
	
	printf("%i", bi.biWidth);;
	printf("\nWhata fuck!!!!!");
	
	
	fclose(pfile);
}

int main(int, char**)
{
   LoadBitmap("test.bmp");
   return 0;
}
  
And the output i get is:> 4194304 Whata fuck!!!! This is so annoying, what kind of width is 4194304??? I mean the image is only 64 pixels wide... And theres nothing wrong with the image as I can open it in other programs... Can anyone help me? In the next 80 years 6 billion people will die...
I am a signature virus. Please add me to your signature so that I may multiply.
Burning_Ice
Burning_Ice
the reason you are getting an error here is that your compiler is propably aligning structures on a 4 byte boundary. your BITMAPFILEHEADER is actually only 14 bytes in size (2*DWORD+3*WORD), but the compiler will use 16 for it, and when reading it will read 16 bytes, so everything in the BITMAPINFOHEADER you are reading afterwards is 2 bytes off and thus gives invalid results.

to correct this you have to enable 2-byte aligning for structures in your compiler options
(in Visual C++ you can just put #pragma pack(2) before you declare your structures) then it should work.


pag
pag
thanks it works

In the next 80 years 6 billion people will die...
I am a signature virus. Please add me to your signature so that I may multiply.

Topic Locked

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

Sign in to reply to this topic.