我最近制作了一个程序来读取BMP文件的fileheader和infoheader。我把它们打包如图所示,然后将它们打开,然后打印宽度和高度。不幸的是,宽度和高度不正确。我不知道为什么。也许有点/小端的问题?如果是这样,我不知道如何解决它。我正在GCC上编译并运行它。
#pragma pack(1)
typedef struct
{
unsigned char fileMarker1; /* 'B' */
unsigned char fileMarker2; /* 'M' */
unsigned int bfSize;
unsigned short unused1;
unsigned short unused2;
unsigned int imageDataOffset; /* Offset to the start of image data */
}FILEHEADER;
typedef struct
{
unsigned int biSize;
signed int width; /* Width of the image */
signed int height; /* Height of the image */
unsigned short planes;
unsigned short bitPix;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
}INFOHEADER;
#pragma pack()
.....
fread( &header, sizeof(FILEHEADER), 1, image );
.....
fread( &iheader, sizeof(INFOHEADER), 1, image );
.....
printf("Width: %i\n", iheader.width);
printf("Height: %i\n", iheader.height);
答案 0 :(得分:1)
Windows位图文件确实存储为小端。因此,假设您的系统是big-endian,则需要在加载后反转每个2或4字节int值的字节顺序。这个IBM article描述了各种方法。