Advertisement

Reading the Buffer a .bmp

Started by November 15, 2000 02:46 PM
1 comment, last by vbisme 24 years, 1 month ago
  
#include <fstream.h>
#include <windows.h>
#include <winbase.h>
#include <io.h>

class BitmapFile
{
public:
	BITMAPFILEHEADER	BmpFileHeader;
	BITMAPINFOHEADER	BmpInfoHeader;
	UCHAR				*Buffer;
};

int main()
{
	HANDLE		File, File2, File3;	//file handle

	LPDWORD		NumBytesWritten, NumBytesRead;	//indications

	BitmapFile	myBmpFile, myBmp;
	DWORD		FileDataSize;	//the bitmap size in bytes

	DWORD		ActualDataSize;
	LONG		AjustedWidth;
	LONG		WidthOver = 0;	
//open file for reading

	File2 = CreateFile("digits.bmp", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	//read the file

	ReadFile(File2, &myBmpFile.BmpFileHeader, sizeof(BITMAPFILEHEADER), NumBytesRead, NULL);
	ReadFile(File2, &myBmpFile.BmpInfoHeader, sizeof(BITMAPINFOHEADER), NumBytesRead, NULL);

	if (((myBmpFile.BmpInfoHeader.biWidth * (myBmpFile.BmpInfoHeader.biBitCount / 8)) % 4) != 0)
	{
		WidthOver = 4 - ((myBmpFile.BmpInfoHeader.biWidth * (myBmpFile.BmpInfoHeader.biBitCount / 8)) % 4);
	}
	AjustedWidth = myBmpFile.BmpInfoHeader.biWidth + WidthOver;
	ActualDataSize = myBmpFile.BmpInfoHeader.biWidth * myBmpFile.BmpInfoHeader.biHeight * (myBmpFile.BmpInfoHeader.biBitCount / 8);
	FileDataSize = AjustedWidth * myBmpFile.BmpInfoHeader.biHeight * (myBmpFile.BmpInfoHeader.biBitCount / 8);

	//Set the file pointer to point to the data

	//SetFilePointer(File2, -(FileDataSize), NULL, FILE_END);

	SetFilePointer(File2, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), NULL, FILE_BEGIN);
	
	//I get an error when I run this line...so I commented it out,

		//could this be the reason for my program failure?

	//if (myBmpFile.Buffer) { free(myBmpFile.Buffer); }

	
	if (!(myBmpFile.Buffer = (UCHAR *)malloc(FileDataSize)))
	{
		CloseHandle(File2);
		return(0);
	}//end if

	
	ReadFile(File2, &myBmpFile.Buffer, FileDataSize, NumBytesRead, NULL);
	//cout << myBmpFile.Buffer << "\n";

	
	CloseHandle(File2);

	//Create a new file and write what''s on the .bmp file

	File = CreateFile("test.bmp", GENERIC_WRITE, 0, NULL,
		CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
	WriteFile(File, &myBmpFile.BmpFileHeader, sizeof(BITMAPFILEHEADER), NumBytesWritten, NULL);
	WriteFile(File, &myBmpFile.BmpInfoHeader, sizeof(BITMAPINFOHEADER), NumBytesWritten, NULL);
	WriteFile(File, &myBmpFile.Buffer, FileDataSize, NumBytesWritten, NULL);

	CloseHandle(File);

	//open file for reading

	File3 = CreateFile("test.bmp", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	//read the file

	ReadFile(File3, &myBmp.BmpFileHeader, sizeof(BITMAPFILEHEADER), NumBytesRead, NULL);
	ReadFile(File3, &myBmp.BmpInfoHeader, sizeof(BITMAPINFOHEADER), NumBytesRead, NULL);

	SetFilePointer(File3, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), NULL, FILE_BEGIN);
	
	//I get an error when I run this line...so I commented it out,

		//could this be the reason for my program failure?

	//if (myBmpFile.Buffer) { free(myBmpFile.Buffer); }

	
	if (!(myBmp.Buffer = (UCHAR *)malloc(FileDataSize)))
	{
		CloseHandle(File2);
		return(0);
	}//end if

	
	ReadFile(File3, &myBmp.Buffer, FileDataSize, NumBytesRead, NULL);

	if (myBmp.Buffer == myBmpFile.Buffer)
	{
		cout << "Yes, they''re equal.\n";
	}//end if

	
	CloseHandle(File3);

	return(0);
}
  
I could read the fileheader and the infoheader, but I could not get the buffer. What is the problem?
I don''t know, but whats wrong with LoadImage?
Advertisement
the starting point of the color data is in bfOffBits in the BITMAPFILEHEADER structure (or the dword 10 bytes into the file). Use this value to offset into the file not the structure sizes. The amount of bytes to read: _filesize(File2) - bfOffBits.
cmaker- I do not make clones.

This topic is closed to new replies.

Advertisement