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.
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;
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).
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
", fh.fileMarker1, fh.fileMarker2, fh.bfSize, fh.unused1, fh.unused2, fh.imageDataOffset);
printf("w = %d, h = %d
", 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
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).
Thanks for your help!
See Question&Answers more detail:
os