#include
#include
#pragma pack(push, 1)
typedef struct {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BITMAPFILEHEADER;
typedef struct {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BITMAPINFOHEADER;
#pragma pack(pop)
int main(int argc, char argv) {
if (argc != 2) {
printf("Usage: ./BitmapOpen
return -1;
}
FILE* file = fopen(argv[1], "rb");
if (!file) {
printf("Could not open filen");
return -1;
}
BITMAPFILEHEADER fileHeader;
fread(&fileHeader, sizeof(BITMAPFILEHEADER), 1, file);
if (fileHeader.bfType != 0x4D42) {
printf("Not a valid bitmap filen");
fclose(file);
return -1;
}
BITMAPINFOHEADER infoHeader;
fread(&infoHeader, sizeof(BITMAPINFOHEADER), 1, file);
unsigned char* imageData = (unsigned char*)malloc(infoHeader.biSizeImage);
fseek(file, fileHeader.bfOffBits, SEEK_SET);
fread(imageData, 1, infoHeader.biSizeImage, file);
fclose(file);
// 输出图像信息
printf("Width: %d, Height: %d, BitCount: %dn", infoHeader.biWidth, infoHeader.biHeight, infoHeader.biBitCount);
// 释放内存
free(imageData);
return 0;
}