我正在尝试将给定的过滤器添加到 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 %dn",i);
printf("The width is %dn", 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 imagen");
//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 changen");
//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 filen");
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 %dn", i);
// printf("Height is %dn", j);
Data[i][j].Red = getc(inputFile);
// printf("The Red componet is %Xn", Data[i][j].Red);
Data[i][j].Green = getc(inputFile);
// printf("The green componet is %Xn", Data[i][j].Green);
Data[i][j].Blue = getc(inputFile);
// printf("The blue componet is %Xn", Data[i][j].Blue);
}
}
fclose(inputFile);
return 0;
}
inputColors
中分配给Data
的行将设置该函数中Data
的值。 它的值不会传递回调用者(main
(,当您稍后尝试使用它时,ColorData
未初始化。
要传递你在inputColors
中创建的2D数组,你需要struct PIXEL ***Data
传递数据,用*Data = malloc
赋值,并将ColorData
main
更改为struct PIXEL **ColorData
。 它可以简化inputColors
中新分配的内存的使用,方法是将该内存分配给局部变量(调用它Data
,并将参数更改为struct PIXEL ***pData
(,并且仅在存储时使用参数名称(*pData = Data;
(。
或者,根本不要将Data
作为参数传递。 在 return
语句中将其传递回调用方。