在解析C /读取位图文件 [英] Parse/Read Bitmap file in C
问题描述
我试图做一个程序从一个位图文件(.BMP,Windows的文件格式,8位)读取数据。现在我卡上的图像数据读取之前的头。
I'm trying to make a program to read data from a bitmap file (.bmp, Windows file format, 8bit). Right now I'm stuck on reading the headers before the image data.
我用的是规格为BMP,我发现这里的 ,以使这些结构来保存文件头,信息头和图像数据BMP的:
I used the specifications for bmp that I found here to make these structs to hold the file header, info header, and image data of the bmp:
typedef struct {
unsigned char fileMarker1;
unsigned char fileMarker2;
unsigned int bfSize;
uint16_t unused1;
uint16_t unused2;
unsigned int imageDataOffset;
} FILEHEADER;
typedef struct {
unsigned int biSize;
int width;
int height;
uint16_t planes;
uint16_t bitPix;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} INFOHEADER;
typedef struct {
unsigned char b;
unsigned char g;
unsigned char r;
} IMAGE;
我实在看不出什么毛病,这些(除非我对规范源是错的,但我在其他地方看了看,在我看来,OK)。
I can't really see anything wrong with these (unless my source for the specification is wrong, but I've looked elsewhere and it seems to me ok).
我用下面的code,以测试它被正确解析:
I'm using the following code to test that it gets parsed correctly:
int main(void) {
FILEHEADER fh;
INFOHEADER ih;
FILE *img = fopen("img.bmp", "rb");
fread(&fh, sizeof(unsigned char), sizeof(FILEHEADER), img);
fread(&ih, sizeof(unsigned char), sizeof(INFOHEADER), img);
printf("fM1 = %c, fM2 = %c, bfS = %u, un1 = %hu, un2 = %hu, iDO = %u\n", fh.fileMarker1, fh.fileMarker2, fh.bfSize, fh.unused1, fh.unused2, fh.imageDataOffset);
printf("w = %d, h = %d\n", ih.width, ih.height);
return 0;
}
不幸的是,当我运行此我得到一个错误的结果:
Unfortunately when I run this I get a wrong result:
User$ ./images
fM1 = B, fM2 = M, bfS = 0, un1 = 0, un2 = 118, iDO = 2621440
w = 3276800, h = 65536
根据该链接,unused1和2应该总是0。此外,宽度和高度是完全错误(这是一个16×16的图像)。
According to that link, unused1 and 2 should be always 0. Also, the width and height are completely wrong (it's a 16x16 image).
似乎有某种对齐问题与结构回事。有没有人有这样的经验吗? (我不希望使用任何图像/位图图库,我要做到这一切我自己)。
it seems that there is some sort of alignment issue going on with the structures. Does anyone have any experience with this? (I don't want to use any image/bitmap libraries, I want to do all this myself).
感谢您的帮助!
推荐答案
是啊,我忘了收拾的结构。这解决的事情。哎呀:
Yup I forgot to pack the structs. This fixes things. Oops:
typedef struct __attribute__((__packed__)) {
unsigned char fileMarker1;
unsigned char fileMarker2;
unsigned int bfSize;
uint16_t unused1;
uint16_t unused2;
unsigned int imageDataOffset;
} FILEHEADER;
typedef struct __attribute__((__packed__)) {
unsigned int biSize;
int width;
int height;
uint16_t planes;
uint16_t bitPix;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} INFOHEADER;
typedef struct __attribute__((__packed__)) {
unsigned char b;
unsigned char g;
unsigned char r;
} IMAGE;
这篇关于在解析C /读取位图文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!