Archived

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

vbisme

Have a look

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