从BMP文件读入C中的BMP头结构

时间:2014-04-09 15:45:07

标签: c segmentation-fault fread bmp

我试图获取BMP文件并将其读入,然后对其中的像素执行操作以更改其颜色。我的问题是我无法将文件中的数据读入两个BMP头结构。我能够将所有数据读入第一个结构中,但是在读入第二个结构时我遇到了一个seg错误。从代码中可以看出,第一个结构FILEHEADER被读取并包含它应该包含的所有正确数据,但是第二个结构BMPInfoHeader没有被正确读取。为什么会发生这种错误?

#include <stdio.h>
#include <stdlib.h>

typedef struct
{   unsigned short int Type; /* Magic identifier */ 
    unsigned int Size; /* File size in bytes */ 
    unsigned short int Reserved1, Reserved2; 
    unsigned int Offset; /* Offset to data (in B) */
}   FILEHEADER; /* 14 Bytes */

typedef struct
{   unsigned int Size; /* Header size in bytes */ 
    int Width, Height; /* Width / Height of image */ 
    unsigned short int Planes; /* Number of colour planes */ 
    unsigned short int Bits; /* Bits per pixel */ 
    unsigned int Compression; /* Compression type */ 
    unsigned int ImageSize; /* Image size in bytes */ 
    int xResolution, yResolution;/* Pixels per meter */ 
    unsigned int Colors; /* Number of colors */ 
    unsigned int ImportantColors;/* Important colors */ 
} BMPInfoHeader;  /* 40 Bytes */

typedef struct 
{   unsigned char r; /* Red */
    unsigned char b; /* Blue */
    unsigned char g; /* Green */
} IMAGE;

int main(int argc, char *argv[]) {

    FILE *BMPFile;
    FILEHEADER BMPFileHeader;
    BMPInfoHeader *InfoHeader;
    BMPFile=fopen(argv[1],"rb");
    unsigned char *BMPimage;

    if (BMPFile==NULL) {
        printf("\n\nERROR: File not opened properly\n\n");
        return -1;
    }

    fread(&BMPFileHeader,sizeof(unsigned char),14,BMPFile);
    fseek(BMPFile,BMPFileHeader.Offset,SEEK_SET);
    fread(InfoHeader,sizeof(unsigned char),40,BMPFile);

    if (BMPFileHeader.Type != 0x4D42) {
        printf("\n\nERROR with fread\n\n");
        return -1;
    }

        return 0;
 }

1 个答案:

答案 0 :(得分:2)

问题在于您定义的FILEHEADER未对齐,因此编译器将在字段之间插入填充。读取bmp标头的常规方法是拆分2字节幻数并单独读取它:

typedef struct
{
    unsigned int Size; /* File size in bytes */ 
    unsigned short int Reserved1, Reserved2; 
    unsigned int Offset; /* Offset to data (in B) */
}   FILEHEADER; /* 12 Bytes */

   :

char Magic[2];
FILEHEADER BMPFileHeader;
fread(Magic, 1, 2, BMPFile);
fread(&BMPFileHeader, 1, 12, BMPFile);

如果由于字节顺序在大端机器上运行它仍会有问题。为了完全通用,您需要将文件内容作为字节读取并手动构造多字节值。