我正在尝试为bmp图像编写边缘检测方法,但在使用fread读取数据之后,我无法访问保存像素RGB值的数组。
图像从读取的数组写得很好,自然不会改变,所以我真的不确定这里发生了什么。我怀疑它与我分配数组的方式有关,但我不确定需要更改什么。非常感谢任何帮助。
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;
}
答案 0 :(得分:3)
... fread(pixelArray, sizeof(struct pixel), infoHeader.width*infoHeader.height, image); ...
由于您为每一行执行了不同的分配,因此无法以这种方式读取像素。
相反,您必须为每一行单独调用fread
。