我正在尝试在我的程序中读取bmp文件,但是遇到了一些问题。读入文件后,如果我让它打印pBmp->header.fileSize,则会显示16,但是如果我在十六进制编辑器中查看该文件大小部分,则为F6 7A 10 00。如果我将该值更改为正确的文件大小,则会显示F6 7A F6 7A 10 00,但这会遇到应始终为零的resv1。我知道这只是读取1个像素数据的问题之一,另一个问题是当我尝试使用while循环读取像素直到文件结束时,我遇到了段错误。我已经花费了几个小时的时间搜索并尝试解决这个问题,但是没有太多成功。
// The BMPHEADER structure.
typedef struct {
byte sigB;
byte sigM;
int32_t fileSize;
int16_t resv1;
int16_t resv2;
int32_t pixelOffset;
} tBmpHeader;
// The BMPINFOHEADER structure.
typedef struct {
int32_t size;
int32_t width;
int32_t height;
int16_t colorPlanes;
int16_t bitsPerPixel;
byte zeros[24];
} tBmpInfoHeader;
typedef uint8_t byte;
typedef struct {
byte blue;
byte green;
byte red;
} tPixel;
// A BMP image consists of the BMPHEADER and BMPINFOHEADER structures, and the 2D pixel array.
typedef struct {
tBmpHeader header;
tBmpInfoHeader infoHeader;
tPixel **pixel;
} tBmp;
tPixel **BmpPixelAlloc(int pWidth, int pHeight)
{
tPixel **pixels = (tPixel **)malloc (pHeight * sizeof(tPixel *));
for (int row = 0; row < pHeight; ++row)
{
pixels[row] = (tPixel *)malloc(pWidth * sizeof(tPixel));
}
printf("pixelAlloc\n");
return pixels;
}
pBmp->pixel = BmpPixelAlloc(pBmp->infoHeader.width, pBmp->infoHeader.height);
if(FileRead(file, &pBmp->pixel, sizeof(tPixel), 1)!=0)
{
errorCode = ErrorFileRead;
}