and ready to go code, tested with g++ (not Windows, but may help someone):
#pragma pack(1)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#include "bmp.h"
vector<char> buffer;
PBITMAPFILEHEADER file_header;
PBITMAPINFOHEADER info_header;
void fill() {
std::ifstream file("data.bmp");
if (file) {
file.seekg(0,std::ios::end);
streampos length = file.tellg();
file.seekg(0,std::ios::beg);
buffer.resize(length);
file.read(&buffer[0],length);
file_header = (PBITMAPFILEHEADER)(&buffer[0]);
info_header = (PBITMAPINFOHEADER)(&buffer[0] + sizeof(BITMAPFILEHEADER));
}
}
int main() {
fill();
cout << buffer[0] << buffer[1] << endl;
cout << file_header->bfSize << endl;
cout << info_header->biWidth << " " << info_header->biHeight << endl;
return 0;
}
In bmp.h i have defined structures:
#pragma once
typedef int LONG;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…