gpt4 book ai didi

c - fread 导致数组变得不可读

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

我正在尝试为 bmp 图像编写边缘检测方法,但在使用 fread 读取数据后,我无法访问保存像素 RGB 值的数组。

图像从 fread 读入的数组中写得很好,自然没有改变,所以我真的不确定这里发生了什么。我怀疑这与我分配数组的方式有关,但我不确定需要更改什么。非常感谢任何帮助。

struct fileHeader{
unsigned short type;
unsigned int size;
unsigned short reservedOne, reservedTwo;
unsigned int offset;
}Header;

struct infofileHeader{
unsigned int size;
int width, height;
unsigned short planes;
unsigned short bits;
unsigned int compression;
unsigned int imageSize;
int xResolution, yResolution;
unsigned int colors;
unsigned int importantColors;
} infoHeader;

struct pixel{
unsigned char Red, Green, Blue;
};

int main(int argc, char *argv[])
{
int i;
FILE* image = fopen(argv[1], "rb");
FILE *testFile = fopen("out.bmp", "wb");

fread(&Header.type, 2, 1, image);
fread(&Header.size, 4, 1, image);
fread(&Header.reservedOne, 2, 1, image);
fread(&Header.reservedTwo, 2, 1, image);
fread(&Header.offset, 4, 1, image);

fread(&infoHeader.size, 4, 1, image);
fread(&infoHeader.width, 4, 1, image);
fread(&infoHeader.height, 4, 1, image);
fread(&infoHeader.planes, 2, 1, image);
fread(&infoHeader.bits, 2, 1, image);
fread(&infoHeader.compression, 4, 1, image);
fread(&infoHeader.imageSize, 4, 1, image);
fread(&infoHeader.xResolution, 4, 1, image);
fread(&infoHeader.yResolution, 4, 1, image);
fread(&infoHeader.colors, 4, 1, image);
fread(&infoHeader.importantColors, 4, 1, image);

printf("%X\n", Header.type);
printf("%d\n", Header.size);
printf("%d\n", Header.offset);

printf("Horizontal Resolution: %d\n", infoHeader.width);
printf("Vertical Resolution: %d\n", infoHeader.height);
printf("Total Resolution:Test %d\n", infoHeader.width * infoHeader.height);


struct pixel **pixelArray = malloc(infoHeader.height * sizeof(pixelArray));
if(pixelArray == NULL)printf("Could not allocate memory");

for(i=0; i<infoHeader.height; i++){
pixelArray[i] = (struct pixel*) malloc(infoHeader.width * sizeof(struct pixel));
if(pixelArray[i] == NULL)printf("Could not allocate memory");
}


fread(pixelArray, sizeof(struct pixel), infoHeader.width*infoHeader.height, image);

fclose(image);

fwrite(&Header.type, 2, 1, testFile);
fwrite(&Header.size, 4, 1, testFile);
fwrite(&Header.reservedOne, 2, 1, testFile);
fwrite(&Header.reservedTwo, 2, 1, testFile);
fwrite(&Header.offset, 4, 1, testFile);

fwrite(&infoHeader.size, 4, 1, testFile);
fwrite(&infoHeader.width, 4, 1, testFile);
fwrite(&infoHeader.height, 4, 1, testFile);
fwrite(&infoHeader.planes, 2, 1, testFile);
fwrite(&infoHeader.bits, 2, 1, testFile);
fwrite(&infoHeader.compression, 4, 1, testFile);
fwrite(&infoHeader.imageSize, 4, 1, testFile);
fwrite(&infoHeader.xResolution, 4, 1, testFile);
fwrite(&infoHeader.yResolution, 4, 1, testFile);
fwrite(&infoHeader.colors, 4, 1, testFile);
fwrite(&infoHeader.importantColors, 4, 1, testFile);

fwrite(pixelArray, sizeof(struct pixel), 50000, testFile);

/*for(i=0; i<infoHeader.height; i++){
free(pixelArray[i]);
}
free(pixelArray);*/

fclose(testFile);

return 0;
}

最佳答案

...
fread(pixelArray, sizeof(struct pixel), infoHeader.width*infoHeader.height, image);
...

您不能以那种方式读取像素,因为您对每一行进行了不同的分配。

相反,您必须为每一行单独调用 fread

关于c - fread 导致数组变得不可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639842/

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