gpt4 book ai didi

C 将 bmp 像素读入二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:09 24 4
gpt4 key购买 nike

我有标题和信息标题。使用以下代码第一个像素读取正常。

// 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));
}

return pixels;
}

tError BmpRead(char *pFilename, tBmp *pBmp)
{
pBmp->pixel = BmpPixelAlloc(pBmp->infoHeader.width, pBmp->infoHeader.height);

if(FileRead(file, &pBmp->pixel, sizeof(tPixel), 1)!=0)
{
errorCode = ErrorFileRead;
}
}

与我交谈的导师说我应该做以下事情

int i = 0;
while(!feof(file))
{
if(FileRead(file, &pBmp->pixel[i], sizeof(tPixel), 1)!=0)
{
errorCode = ErrorFileRead;
}
++i;
}

但这给了我一个段错误。如果我在循环中打印一个打印件,它会在说段错误之前打印几千次。我尝试使用双 for 循环,但它甚至无法编译。我在谷歌上花了几个小时,但无法弄明白。

最佳答案

没关系,我想通了。

for(int i = 0; i < pBmp->infoHeader.height; ++i)
{
for(int j = 0; j < pBmp->infoHeader.width; ++j)
{
if(FileRead(file, &pBmp->pixel[i][j], sizeof(tPixel), 1)!=0)
{
errorCode = ErrorFileRead;
}
}
}

关于C 将 bmp 像素读入二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19746645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com