I'm having trouble finding the bitmap offset info in the file header, and the size info is a bit unreliable at the moment.
I've made a 10x10 pixel 24-bit image in paint in WinXP and that's the file I'm testing with.
If I increase the size so it goes over the size of an unsigned short int it doesn't show the correct size and I was wondering how to fix this. If I increase the size of bmpFileHeader.size to unsigned long int it doesn't work at all.
The second problem is the bitmap offset, it doesn't show any meaningful value at all.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct bmpFileHeader {
unsigned short int type; //specifies the file type
unsigned short int size; //specifies the size in bytes of the bitmap file
unsigned short int reserved1; //reserved; must be 0
unsigned short int reserved2; //reserved; must be 0
unsigned long int bitmapOffset; //specifies the offset in bytes from the bitmapfileheader to the bitmap bits
};
int main(int argc, char *argv[])
{
bmpFileHeader fh;
//Open file as read only, binary
ifstream bmpstream("test.bmp", ios::in | ios:: binary);
//Verifying that open file succeeded
if(!bmpstream) {
cout << "Error opening file.\n";
system("PAUSE");
return EXIT_FAILURE;
}
//Read bitmap file header
bmpstream.read((char *) &fh, sizeof(bmpFileHeader));
//Temporary debug feedback
cout << "Filetype: " << hex << fh.type << endl;
cout << "Filesize: " << dec << fh.size << endl;
cout << "Reserved 1: " << fh.reserved1 << endl;
cout << "Reserved 2: " << fh.reserved2 << endl;
cout << "Bitmap Offset: " << fh.bitmapOffset << endl;
//Verifying that file is .BMP by checking first 2 bytes in the file header.
if (fh.type !=0x4D42) {
bmpstream.close();
return EXIT_FAILURE;
}
//Close file
bmpstream.close();
system("PAUSE");
return EXIT_SUCCESS;
}