예제 #1
0
파일: filter.c 프로젝트: dumrelu/Blur
FILTER *filter_create_gauss(int radius, double sigma) {
	//Allocate mem for the structure
	FILTER *filter = (FILTER*) malloc(sizeof(FILTER));
	filter->radius = radius;
	filter->type = FILTER_GAUSS;

	//Used for iterations
	int i, j;

	//The matrix width and height
	int dim = 2*radius+1;

	//Alocate mem for the matrix
	filter->matrix = (double**) malloc(dim * sizeof(double*));
	for(i = 0; i < dim; i++)
		filter->matrix[i] = (double*) malloc(dim * sizeof(double));

	//Calculate
	double sum = 0.0;
	for(i = -radius; i <= radius; i++)
		for(j = -radius; j <= radius; j++) {
			filter->matrix[i+radius][j+radius] = gauss_2d(j, i, sigma);
			sum += filter->matrix[i+radius][j+radius];
		}

	//Correct so that the sum of all elements ~= 1
	for(i = 0; i < 2*radius+1; i++)
		for(j = 0; j < 2*radius+1; j++)
			filter->matrix[i][j] /= sum;

	return filter;
}
void srcs_images(double *xi1,double *xi2,int nx1,int nx2,double *gpar,int npars,double *gpars,int nsubs,double *g_srcs) {
	int i,j,k,l,index;
    gauss_2d(xi1,xi2,nx1,nx2,gpar,g_srcs);
    double * gpars_i = (double *)malloc(npars*sizeof(double));
    double * g_lens_subs = (double *)malloc(nx1*nx2*sizeof(double));
	for (i = 0; i < nsubs; ++i) {
		for (j = 0; j < npars; ++j) {
			gpars_i[j] = gpars[i*npars+j];
		}
		gauss_2d(xi1,xi2,nx1,nx2,gpars_i,g_lens_subs);
		for (k = 0; k < nx1; ++k) for (l = 0; l < nx2; ++l){
			index = k*nx2+l;
			g_srcs[index] = g_srcs[index] + g_lens_subs[index];
		}
	}
    free(gpars_i);
    free(g_lens_subs);
}
void all_about_lensing(double *xi1,double *xi2,int nx1,int nx2,double * spar, int nspars, double * spars, int nssubs, double * lpar,int nlpars,double * lpars,int nlsubs,double *s_image,double *g_lensimage,double *critical,double *caustic){
	int i,j,k,l,index;
    double * al1 = (double *)malloc(sizeof(double)*nx1*nx2);
    double * al2 = (double *)malloc(sizeof(double)*nx1*nx2);
    lq_nie(xi1,xi2,nx1,nx2,lpar,al1,al2);

    double * als1 = (double *)malloc(sizeof(double)*nx1*nx2);
    double * als2 = (double *)malloc(sizeof(double)*nx1*nx2);

    double * lpars_i = (double *)malloc(sizeof(double)*nlpars);
	for (i = 0; i < nlsubs; ++i) {
		for (j = 0; j < nlpars;++j) {
			lpars_i[j] = lpars[i*nlpars+j];
		}
        lq_nie(xi1,xi2,nx1,nx2,lpars_i,als1,als2);
		for (k = 0; k < nx1; k++) for (l = 0; l < nx2; l++) {
			index = k*nx2+l;
			al1[index] = al1[index]+als1[index];
			al2[index] = al2[index]+als2[index];
		}
	}
    free(als1);
    free(als2);
    free(lpars_i);
//------------------------------------------------------------------------
    double dsx = xi1[nx2+1]-xi1[0];
    double * a11 = (double *)malloc(nx1*nx2*sizeof(double));
    double * a12 = (double *)malloc(nx1*nx2*sizeof(double));
    double * a21 = (double *)malloc(nx1*nx2*sizeof(double));
    double * a22 = (double *)malloc(nx1*nx2*sizeof(double));

	lanczos_diff_2_tag(al1,al2,a21,a22,a11,a12,dsx,nx1,-1);

    double * imu = (double *)malloc(nx1*nx2*sizeof(double));
	for (k = 0; k < nx1; k++) for (l = 0; l < nx2; l++) {
		index = k*nx2+l;
		imu[index] = (1.0-(a11[index]+a22[index])+a11[index]*a22[index]-a12[index]*a21[index]);
	}

	free(a11);
	free(a12);
	free(a21);
	free(a22);

    find_critical_curve(imu,nx1,nx2,critical);
	free(imu);

//------------------------------------------------------------------------
    double * yi1 = (double *)malloc(sizeof(double)*nx1*nx2);
    double * yi2 = (double *)malloc(sizeof(double)*nx1*nx2);
	for (k = 0; k < nx1; k++) for (l = 0; l < nx2; l++) {
		index = k*nx2+l;
		yi1[index] = xi1[index]-al1[index];
		yi2[index] = xi2[index]-al2[index];
	}
    free(al1);
    free(al2);
    gauss_2d(xi1,xi2,nx1,nx2,spar,s_image);
	srcs_images(xi1,xi2,nx1,nx2,spar,nspars,spars,nssubs,s_image);
	srcs_images(yi1,yi2,nx1,nx2,spar,nspars,spars,nssubs,g_lensimage);
	free(yi1);
	free(yi2);
//------------------------------------------------------------------------
    int clen = 0;
	int nfiner = 1;

	for (i = 0; i < nx1; ++i) for (j = 0; j < nx2; ++j){
		index = i*nx2+j;
		if (critical[index]>0) {
			clen = clen+1;
		}
	}
	int ylen = clen*nfiner*nfiner;

    double * yif1 = (double *)malloc(ylen*sizeof(double));
    double * yif2 = (double *)malloc(ylen*sizeof(double));

	refine_critical(xi1,xi2,nx1,nx2,lpar,nlpars,lpars,nlsubs,critical,clen,nfiner,yif1,yif2);

    double bsz;
    bsz = dsx*nx1;
    double * img_in = (double *)malloc(ylen*sizeof(double));

	for (i = 0; i < ylen; ++i) {
		img_in[i] = 1.0;
	}
    forward_cic(img_in,yif1,yif2,bsz,bsz,nx1,nx2,ylen,caustic);

	free(yif1);
	free(yif2);
	free(img_in);

	for (i = 0; i < nx1; ++i) for (j = 0; j < nx2; ++j){
		index = i*nx2+j;
		if (caustic[index]>0) {
			caustic[index] = 1;
		}
	}
}