这是我的代码。我想知道如何“正确”读取 BMP 文件,然后读取标头值而不强制打包结构。
typedef struct __attribute__((packed)){
uint8_t magic[2]; /* the magic number used to identify the BMP file:
0x42 0x4D (Hex code points for B and M).
The following entries are possible:
BM - Windows 3.1x, 95, NT, ... etc
BA - OS/2 Bitmap Array
CI - OS/2 Color Icon
CP - OS/2 Color Pointer
IC - OS/2 Icon
PT - OS/2 Pointer. */
uint32_t filesz; /* the size of the BMP file in bytes */
uint16_t creator1; /* reserved. */
uint16_t creator2; /* reserved. */
uint32_t offset; /* the offset, i.e. starting address,
of the byte where the bitmap data can be found. */
} bmp_header_t;
fp = fopen("input.bmp", "r");
bmp_header_p = malloc(sizeof(bmp_header_t));
fread(bmp_header_p, sizeof(char), 14, fp);
printf("magic number = %c%c\n", bmp_header_p->magic[0], bmp_header_p->magic[1]);
printf("file size = %" PRIu32 "\n", bmp_header_p->filesz);