Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AbsolutDew

Reading bitmaps

This topic is 6136 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

Can anyone explain to me why this isn''t working?
  
BITMAPFILEHEADER	bmFileHeader;
	BITMAPINFOHEADER	bmInfoHeader;
	char*				szFileName = "MYBITMAP.BMP";	// Name of bitmap file

	HANDLE				hBitmap = 0;					// The handle to the opened file

	DWORD				dwBytesRead = 0;				// Number of bytes read from ReadFile

	int					iResult	= FALSE;				// Holds results to function calls


	// Clear the headers

	ZeroMemory( &bmFileHeader, sizeof(bmFileHeader) );
	ZeroMemory( &bmInfoHeader, sizeof(bmInfoHeader) );

	// Open the file

	hBitmap = CreateFile( szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
	if( hBitmap == INVALID_HANDLE_VALUE )
	{
		printf( "Failed to open file: %s. \n", szFileName );
		exit( 1 );
	}

	iResult = ReadFile( hBitmap, &bmFileHeader, sizeof(bmFileHeader), &dwBytesRead, NULL );
	if( iResult == 0 )
	{
		printf( "ReadFile failed. \n" );
		CloseHandle( hBitmap );
		exit( 1 );
	}

	if( bmFileHeader.bfType == ''BM'' )
		printf( "Bitmap found. \n" );
	else
		printf( "File is not a bitmap, Type == %s. \n", &bmFileHeader.bfType );
  
when i print out bmFileHeader.bfType i get BM6 with some garbage characters after it. it seems like it''s reading in to many characters or something. what am i doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
try to use this:

char bmpCompare[2] = "BM";
if(strcmp(bmFileHeader.bfType, bmpCompare, 2)) != 0)
{
// invalid bmp
}

Dunno if it''s 100% correct syntax, but something like that should work. Remember to include "string"

Share this post


Link to post
Share on other sites
What you should do is compare the bfType field to 0x4d42. The bfType field is actually declared as a WORD and the 0x4d42 value comes from the letters ''B'' and ''M''.

So, simply replace the line

if( bmFileHeader.bfType == ''BM'' )

with

if( bmFileHeader.bfType == 0x4d42 )

Share this post


Link to post
Share on other sites
No worries !

The 0x4d42 comes from the ''B'' and ''M'' that you''re going to compare the bfType field to. You have to use the ascii values of B and M (0x42 and 0x4d respectively) to represent them as a WORD. The reverse order of the byte comes from the byte ordering on Intel processors.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!