I have read this question/answer and this question/answer as well as BITMAPFILEHEADER structure and BITMAPINFOHEADER structure from MS, and I want to check if I am interpreting them properly. In filter.c, the following code is run:

fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
        bi.biBitCount != 24 || bi.biCompression != 0)
        fprintf(stderr, "Unsupported file format.\n");
        return 6;

where in bmp.h, BITMAPFILEHEADER is defined as:

typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

typedef struct
    WORD   bfType;
    DWORD  bfSize;
    WORD   bfReserved1;
    WORD   bfReserved2;
    DWORD  bfOffBits;
} __attribute__((__packed__))

And BITMAPINFOHEADER is defined as

typedef struct
    DWORD  biSize;
    LONG   biWidth;
    LONG   biHeight;
    WORD   biPlanes;
    WORD   biBitCount;
    DWORD  biCompression;
    DWORD  biSizeImage;
    LONG   biXPelsPerMeter;
    LONG   biYPelsPerMeter;
    DWORD  biClrUsed;
    DWORD  biClrImportant;
} __attribute__((__packed__))

My question: It seems that there must be some overlap between what which bytes at the top of a bitmap file mean. For one example, in BITMAPFILEHEADER, bits 0-15 of the bitmap file should read Ascii "BM" (or 0x4d42 in little endian). However, in BITMAPINFOHEADER bits 0-31 of the same bitmap also somehow contain the size of BITMAPINFOHEADER?

As an aside, is the check bi.biSize != 40 checking that the header data in the bitmap is 40 bytes in size? I'm asking becausethe struct BITMAPINFOHEADER must always be 40 bytes wide by definition.

Am I misinterpreting the standard or is it just implemented in a complex way that I am not seeing?

You must log in to answer this question.

Browse other questions tagged .