Jump to content
  • Advertisement

Archived

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

vbisme

Have a look

This topic is 6238 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 trying to load a .bmp file into memory. What''s wrong with my code? It doesn''t crash but loads wrong memory.
  
DWORD CBmpFileReader::LoadFile24(const char *szFilename)
{
	HANDLE	hFile;
	DWORD	BytesRead;

	///Open the file to the handle

	hFile = CreateFile(szFilename, GENERIC_READ, 0, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return(1);
	}//end if

	///


	///Read some information from the file

	// Read BITMAPFILEHEADER

	if (!(ReadFile(hFile, &FileHeader, sizeof(BITMAPFILEHEADER),
		&BytesRead, NULL)))
	{
		return(2);
	}//end if

	// Read BITMAPINFOHEADER

	if (!(ReadFile(hFile, &InfoHeader, sizeof(BITMAPINFOHEADER),
		&BytesRead, NULL)))
	{
		return(3);
	}//end if

	///


	///Test For BPP.  If 24 or 32 bit don''t need to read color table

	if (InfoHeader.biBitCount == 24)
	{
		// Allocate memory for pixel data

		int DataSize;
		DataSize = InfoHeader.biWidth * InfoHeader.biHeight * (COLOR_DISPLAY / 8);
		pbPixelData = new BYTE[DataSize];
		if (!pbPixelData)
		{
			CloseHandle(hFile);
			return(5);
		}
		// Make sure actual width is multiple of 4, round up

		
		int iRemainder, iActualWidth, iSkipBytes;
		iRemainder = InfoHeader.biWidth % 4;
		iSkipBytes = 4 - iRemainder;
		iActualWidth = InfoHeader.biWidth + iSkipBytes;
		
		for (int i = 0; i < InfoHeader.biHeight; i++)
		{
			// Read pixel data

			if (!(ReadFile(hFile, (BYTE*)(pbPixelData + (i * InfoHeader.biWidth)),
				InfoHeader.biWidth, &BytesRead, NULL)))
			{
				CloseHandle(hFile);
				delete [] pbPixelData;
				return(6);
			}//end if

			// Skip padded bytes

			SetFilePointer(hFile, iSkipBytes, NULL, FILE_CURRENT);
		}
	}
	else
	{
		return(4);
	}//end if

	///

	
	CloseHandle(hFile);

	//success

	return(0);

}
  

Share this post


Link to post
Share on other sites
Advertisement

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