segmentation fault
ソースは以下です
途中までは実行されるんですけど,,,
教えてください
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265358979 /* 円周率 */
int main(int argc, char **argv)
{
int xsize,ysize,depth,x,y,c;
char code[10];
FILE *fp;
unsigned char ***image,***outimage;
double sum_kido,sum_kido_x,grad;
int xmax,xmin,ymax,ymin,count;
char grad_im;
int sumx, sumy, N;
char dummy[1024];
double Deltax;
double Deltay;
if(argc != 3){
fprintf(stderr,"Usage : a.out input_filename output_filename\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
fprintf(stderr,"File can not open : %s\n",argv[1]);
exit(1);
}
fscanf(fp,"%s",code);
/*
fscanf(fp,"%s",dummy);
*/
fscanf(fp,"%d %d",&xsize,&ysize);
fscanf(fp,"%d ",&depth);
fprintf(stderr,"%s, %d, %d, %d\n",code,xsize,ysize,depth);
getchar();
image = (unsigned char ***)malloc(sizeof(unsigned char**)*ysize);
for(y=0;y<ysize;y++){
image[y] = (unsigned char **)malloc(sizeof(unsigned char*)*xsize);
for(x=0;x<xsize;x++){
image[y][x] = (unsigned char *)malloc(sizeof(unsigned char)*3);
}
}
outimage = (unsigned char ***)malloc(sizeof(unsigned char**)*ysize);
for(y=0;y<ysize;y++){
outimage[y] = (unsigned char **)malloc(sizeof(unsigned char*)*xsize);
for(x=0;x<xsize;x++){
outimage[y][x] = (unsigned char *)malloc(sizeof(unsigned char)*3);
}
}
// load image
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
for(c=0;c<3;c++){
image[y][x][c] = (unsigned char)(fgetc(fp));
printf("%d\n", image[y][x][c]);
}
}
}
fclose(fp);
sumx = 0;
sumy = 0;
N = 0;
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
if((image[y][x][0]*2<image[y][x][2])&&(image[y][x][1]*2<image[y][x][2])){
outimage[y][x][0]=255;
outimage[y][x][1]=0;
outimage[y][x][2]=0;
}else{
outimage[y][x][0]=image[y][x][0];
outimage[y][x][1]=image[y][x][1];
outimage[y][x][2]=image[y][x][2];
sumx += x; sumy += y, N += 1;
}
}
}
// output image
if((fp=fopen(argv[2],"w"))==NULL){
fprintf(stderr,"File can not open : %s\n",argv[2]);
exit(1);
}
fprintf(fp,"%s\n",code);
fprintf(fp,"%d %d\n",xsize,ysize);
fprintf(fp,"%d\n",depth);
count = 0;
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
for(c=0;c<3;c++){
fputc(outimage[y][x][c],fp);
}
}
}
fclose(fp);
fprintf(stdout, "%d, %d, %d, Gravity (x,y) = (%lf, %lf)\n",sumx,sumy,N,(double)(sumx)/(double)(N), (double)(sumy)/(double)(N));
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
Deltax = sqrt(( pow (image[x+1][y][0] , 2.0 ) + pow (image[x+1][y][1] , 2.0 ) + pow (image[x+1][y][2] , 2.0 )) - ( sqrt( pow (image[x-1][y][0] , 2.0) + (image[x-1][y][1] , 2.0) + (image[x-1][y][2] , 2.0))));
Deltay = sqrt(( pow (image[x][y+1][0] , 2.0 ) + pow (image[x][y+1][1] , 2.0 ) + pow (image[x][y+1][2] , 2.0 )) - ( sqrt( pow (image[x][y-1][0] , 2.0) + (image[x][y-1][1] , 2.0) + (image[x][y-1][2] , 2.0))));
}
}
for(y=0;y<ysize;y++){
for(x=0;x<xsize;x++){
double T = atan( Deltax / Deltay ); //勾配ベクトルの方向
double U = sqrt( pow ( Deltax , 2.0 ) + pow (Deltay , 2.0 ) ); //勾配ベクトルの勾配量
double V = atan( ( x - (double)(sumx) / (double)(N) ) / ( y - (double)(sumy) / (double)(N) ) );
printf(" %f\n " , V - ( T + ( M_PI / 2 )));
}
}
}