我正在尝试将给定的过滤器添加到bmp文件中,但是在第一次添加过滤器数组和tempRed时它就存在段错误(我使用gdb找到了它)。我以为是因为它正在尝试从数组[-1]访问存储,但是由于我们从1开始,所以我认为情况并非如此。任何帮助是极大的赞赏。预先感谢!
int edgeDect(struct HEADER *Header, struct INFOHEADER *InfoHeader, struct PIXEL **Data){
int i = 0, j = 0;
char filter[3][3] =
{{0, -1, 0},
{-1, 4, -1},
{0, -1, 0}};
char tempRed, tempGreen, tempBlue;
for(i = 1; i < InfoHeader->Height - 1; i++){
for(j = 1; j < InfoHeader->Width - 1; j++){
printf("The height is %d\n",i);
printf("The width is %d\n", j);
tempRed = 0;
tempGreen = 0;
tempBlue = 0;
//***Where seg fault occurs
tempRed += (filter[0][0] * Data[i-1][j-1].Red);
tempGreen += (filter[0][0] * Data[i-1][j-1].Green);
tempBlue += (filter[0][0] * Data[i-1][j-1].Blue);
tempRed += (filter[0][1] * Data[i-1][j].Red);
tempGreen += (filter[0][1] * Data[i-1][j].Green);
tempBlue += (filter[0][1] * Data[i-1][j].Blue);
tempRed += (filter[0][2] * Data[i-1][j+1].Red);
tempGreen += (filter[0][2] * Data[i-1][j+1].Green);
tempBlue += (filter[0][2] * Data[i-1][j+1].Blue);
tempRed += (filter[1][0] * Data[i][j-1].Red);
tempGreen += (filter[1][0] * Data[i][j-1].Green);
tempBlue += (filter[1][0] * Data[i][j-1].Blue);
tempRed += (filter[1][1] * Data[i][j].Red);
tempGreen += (filter[1][1] * Data[i][j].Green);
tempBlue += (filter[1][1] * Data[i][j].Blue);
tempRed += (filter[1][2] * Data[i][j+1].Red);
tempGreen += (filter[1][2] * Data[i][j+1].Green);
tempBlue += (filter[1][2] * Data[i][j+1].Blue);
tempRed += (filter[2][0] * Data[i+1][j-1].Red);
tempGreen += (filter[2][0] * Data[i+1][j-1].Green);
tempBlue += (filter[2][0] * Data [i+1][j-1].Blue);
tempRed += (filter[2][1] * Data[i+1][j].Red);
tempGreen += (filter[2][1] * Data[i+1][j].Green);
tempBlue += (filter[2][1] * Data[i+1][j].Blue);
tempRed += (filter[2][2] * Data[i+1][j+1].Red);
tempGreen += (filter[2][2] * Data[i+1][j+1].Green);
tempBlue += (filter[2][2] * Data[i+1][j+1].Blue);
Data[i][j].Red = tempRed;
Data[i][j].Green = tempGreen;
Data[i][j].Blue = tempBlue;
}
}
return 0;
}
主要:
int main(int argc, char **argv){
struct PIXEL *ColorData;
struct INFOHEADER InfoHeader;
unsigned char red, green, blue;
struct HEADER Header;
//Checks to make sure that there are enough command line arguments
if(argc != 6){
printf("Not enough input arguments in the command line.\n");
exit(1);
}
printf("Inputing the information into the header structs.\n");
//Inputs the header information into the header structs
InputHeaders(argv[1], &InfoHeader, &Header);
printf("Inputing the RGB componets into the struct.\n");
//Inputs the colors from the picture into is RGB comnponets
inputColors(argv[1], &InfoHeader, &ColorData, &Header);
//Gets the integer value for the colors from the command line
red = atoi(argv[3]);
green = atoi(argv[4]);
blue = atoi(argv[5]);
printf("Adding filter to input image\n");
//Edge detctor: FInd the edge of the picture
edgeDect(&Header, &InfoHeader, &ColorData);
printf("Saving the filter into outputfilename(edge).bmp.\n");
//Saves the edge data to the correct output file
saveEdge(argv[2], &InfoHeader, &Header, &ColorData);
printf("Color changing function adding the command line numbers to the current RGB values.\n");
//Changes the color of each RGB pixel with the inputed command line values
colorChange(&ColorData, red, green, blue, &InfoHeader);
printf("Saving the new bmp file with the color change\n");
//Saves te shade file to the correct output file
saveShade(argv[2], &InfoHeader, &Header, &ColorData);
return 0;
}
存储RGB值:
int inputColors(char *filename, struct INFOHEADER *InfoHeader, struct PIXEL **Data, struct HEADER *Header){
int i = 0, j = 0;
FILE *inputFile;
//Opens up the file so that it can be read from
if((inputFile = fopen(filename, "rb")) == NULL){
printf("Unable to open .bmp file\n");
exit(1);
}
//Mallocing enough space for the 2D structures of pixels (colors)
Data = (struct PIXEL **)malloc(InfoHeader->Height * sizeof(struct PIXEL *));
for(i = 0; i < InfoHeader->Height; i++){
Data[i] = (struct PIXEL *)malloc(InfoHeader->Width * sizeof(struct PIXEL));
}
//This goes until after we are down with the header
fseek(inputFile, Header->Offset, SEEK_SET);
//Inputing the data into the malloced struct
i = 0, j = 0;
for(i = 0; i < InfoHeader->Height; ++i){
for(j = 0; j < InfoHeader->Width; ++j){
// printf("Width is %d\n", i);
// printf("Height is %d\n", j);
Data[i][j].Red = getc(inputFile);
// printf("The Red componet is %X\n", Data[i][j].Red);
Data[i][j].Green = getc(inputFile);
// printf("The green componet is %X\n", Data[i][j].Green);
Data[i][j].Blue = getc(inputFile);
// printf("The blue componet is %X\n", Data[i][j].Blue);
}
}
fclose(inputFile);
return 0;
}
答案 0 :(得分:0)
在inputColors
中分配给Data
的行将设置该函数内Data
的值。其值不会传递回调用方(main
),而在以后尝试使用时,ColorData
不会被初始化。
要传递在inputColors
中创建的2D数组,您需要将数据传递为struct PIXEL ***Data
,将其分配给*Data = malloc
,然后在{{1}中更改ColorData
}为main
。通过将内存分配给本地变量(将其命名为struct PIXEL **ColorData
,并将参数更改为inputColors
),并且仅使用参数,可以简化对Data
中新分配的内存的使用。储存一次(struct PIXEL ***pData
)。
或者,根本不要传递*pData = Data;
作为参数。在Data
语句中将其传递回调用方。