示例#1
0
文件: z2logp_4dfp.c 项目: ysu001/PUP
int main (int argc, char *argv[]) {
/*************/
/* image I/O */
/*************/
	FILE		*fp_img, *fp_out;
	IFH		ifh;
	char		imgfile[MAXL], outfile[MAXL], imgroot[MAXL], outroot[MAXL];
	char		*str, command[MAXL], program[MAXL];

/**************/
/* processing */
/**************/
	char		control = '\0';
	int		dimension, c, i, j, k;
	int		imgdim[4], orient, isbig;
	float		voxdim[3], voxdimn[3];
	float		*imgz, *imgp;
	double		q;

/*********/
/* flags */
/*********/
	int		status = 0;
	int		p_flag = 0;
	int		two_sided = 0;
	int		defined;
	int		debug = 0;

	printf ("%s\n", rcsid);
	setprog (program, argv);

/************************/
/* process command line */
/************************/
        for (k = 0, i = 1; i < argc; i++) {
                if (*argv[i] == '-') {
		strcpy (command, argv[i]); str = command;
			while (c = *str++) switch (c) {
				case 'd': debug++;		break;
				case 'p': p_flag++;		break;
				case '2': two_sided++;		break;
				case '@': control = *str++;	*str = '\0'; break;
			}
		} else switch (k) {
                        case 0: getroot (argv[i], imgroot);	k++; break;
                }
        }
        if (k < 1) usage (program);

/**************************/
/* get input 4dfp z-image */
/**************************/
	sprintf (imgfile, "%s.4dfp.img", imgroot);
	if (get_4dfp_dimoe (imgfile, imgdim, voxdim, &orient, &isbig) < 0) errr (program, imgfile);
	if (Getifh (imgfile, &ifh)) errr (program, imgfile);
	if (!control) control = (isbig) ? 'b' : 'l';
	dimension = imgdim[0] * imgdim[1] * imgdim[2];
	if (!(imgz = (float *) malloc (dimension * sizeof (float)))) errm (program);
	if (!(imgp = (float *) malloc (dimension * sizeof (float)))) errm (program);

/***************************/
/* prepare 4dfp read/write */
/***************************/
	fprintf (stdout, "Reading: %s\n", imgfile);
	if (!(fp_img = fopen (imgfile, "rb"))) errr (program, imgfile);
	sprintf (outroot, "%s_%s", imgroot, (p_flag) ? "p" : "log10p");	
	sprintf (outfile, "%s.4dfp.img", outroot);
	fprintf (stdout, "Writing: %s\n", outfile);
	if (!(fp_out = fopen (outfile, "wb"))) errw (program, outfile);

/***********/
/* process */
/***********/
	for (j = 0; j < imgdim[3]; j++) {
		if (eread (imgz, dimension, isbig, fp_img)) errr (program, imgfile);
		for (i = 0; i < dimension; i++) {
			q = (double) imgz[i];
			defined = !isnan (q) && imgz[i] != (float) 1.e-37 && finite (q);
			if (defined) {
				if (two_sided) {
					q = fabs (q);
					imgp[i] =         (p_flag) ? 2.*p_z (q) : (-1./M_LN10)*(lnp_z (q) + M_LN2);
				} else {
					if (q > 0) {
						imgp[i] = (p_flag) ?    p_z (q) : (-1./M_LN10)*lnp_z (q);
					} else {
						imgp[i] = (p_flag) ?        1.0 : 0.0;
					}
				}
			} else {
				imgp[i] = 1.e-37;
			}
		}
		if (ewrite (imgp, dimension, control, fp_out)) errw (program, outfile);
	}
	if (fclose (fp_img)) errr (program, imgfile);
	if (fclose (fp_out)) errw (program, outfile);

/*******/
/* ifh */
/*******/
	if (Writeifh (program, outfile, &ifh, control)) errw (program, outroot);

/*******/
/* hdr */
/*******/
	sprintf (command, "ifh2hdr %s", outroot);
	status |= system (command);

/*******/
/* rec */
/*******/
	startrece (outfile, argc, argv, rcsid, control);
	catrec (imgfile);
	endrec ();

	free (imgz); free (imgp);
	exit (status);
}
示例#2
0
文件: RFX_4dfp.c 项目: ysu001/PUP
int main (int argc, char *argv[]) {
	FILE		*fp;
	IFH		ifh;
	char		imgroot[2][MAXR][MAXL], Nmgroot[2][MAXL], lstfile[2][MAXL];
	int		nimg[2], isbig[2][MAXR], isbigN[2];
	int		orient, orient1, imgdim[4], imgdim1[4], dimension, vdim;
	char		outroot[MAXL] = "", imgfile[MAXL], outfile[MAXL];
	float		voxdim[3], voxdim1[3];
	float		amin = FLT_MAX, amax = -FLT_MAX;
	float		*img1,  **imgN;
	double		**imgs, **imgv;
	int		**imgm, ngroup;
	char		control = '\0';

/***********/
/* utility */
/***********/
	double		u;
	int		c, i, j, k, m;
	char		*ptr, *str, command[MAXL];
	char		*srgv[MAXL];		/* string field pointers */

/*********/
/* flags */
/*********/
	int		defined;
	int		debug = 0;
	int		NaN_flag = 'E';		/* 'E' 1.e-37; 'Z' 0.0; 'N' NaN; */
	int		status = 0;
	int		saverec = 1;

	printf ("%s\n", rcsid);
	setprog (program, argv);
/************************/
/* process command line */
/************************/
	k = 0; for (i = 1; i < argc; i++) {
		if (*argv[i] == '-') {
			strcpy (command, argv[i]); ptr = command;
			while (c = *ptr++) switch (c) {
				case 'R': saverec = 0;			break;
				case 'd': debug++;			break;
				case 'o': getroot (ptr, outroot);	*ptr = '\0'; break;
				case '@': control = *ptr++;		*ptr = '\0'; break;
			}
		} else switch (k) {
			case 0:	strcpy (lstfile[0], argv[i]);		k++; break;
			case 1:	getroot (argv[i], Nmgroot[0]);		k++; break;
			case 2:	strcpy (lstfile[1], argv[i]);		k++; break;
			case 3:	getroot (argv[i], Nmgroot[1]);		k++; break;
		}	
	}
	if (k != 2 && k != 4) usage ();
	ngroup = k/2;

/********************/
/* parse list files */
/********************/
	for (k = 0; k < ngroup; k++) {
		nimg[k] = 0;
		if (!(fp = fopen (lstfile[k], "r"))) errr (program, lstfile[k]);
		printf ("Reading: %s\n", lstfile[k]);
		while (fgets (command, MAXL, fp)) {
			if (nimg[k] >= MAXR) {
				fprintf (stderr, "%s: maximum number of input images (%d) exceeded\n", program, MAXR);
				exit (-1);
			}
			if (ptr = strchr (command, '#'))  *ptr = '\0';
			if (!strlen (command)) continue;		/* skip blank lines */
			if (ptr = strchr (command, '\n')) *ptr = '\0';	/* strip terminal nl */
			i = m = 0; while (m < MAXL && i < MAXL) {
				while (!isgraph ((int) command[i]) && command[i]) i++;
				if (!command[i]) break;
				srgv[m++] = command + i;
				while (isgraph ((int) command[i])) i++;
				if (!command[i]) break;
				command[i++] = '\0';
			}
			getroot (srgv[0], imgroot[k][nimg[k]++]);
		}
		fclose (fp);
		if (!nimg[k]) usage ();
	
/*********************************/
/* check dimensional consistency */
/*********************************/
		for (i = 0; i < nimg[k]; i++) {
			printf ("image %3d: %s\n", i + 1, imgroot[k][i]);
			if (!i && !k) {
				if (get_4dfp_dimoe (imgroot[k][i], imgdim,  voxdim,  &orient,  isbig[k] + i)) exit (-1);
				if (Getifh (imgroot[k][i], &ifh)) errr (program, imgroot[k][i]);
			} else {
				if (get_4dfp_dimoe (imgroot[k][i], imgdim1, voxdim1, &orient1, isbig[k] + i)) exit (-1);
				status = (orient1 != orient);
				for (j = 0; j < 4; j++) status |= (imgdim1[j]      != imgdim[j]);
				for (j = 0; j < 3; j++) status |= (fabs (voxdim1[j] - voxdim[j]) > 1.e-4);
			}
			if (status) {
				fprintf (stderr, "%s: %s %s dimension mismatch\n", program, imgroot[0][0], imgroot[k][i]);
				exit (-1);
			}
		}
		if (get_4dfp_dimoe (Nmgroot[k], imgdim1, voxdim1, &orient1, isbigN + k)) exit (-1);
		status = (orient1 != orient);
		for (j = 0; j < 3; j++) status |= (imgdim1[j]      != imgdim[j]);
		for (j = 0; j < 3; j++) status |= (fabs (voxdim1[j] - voxdim[j]) > 1.e-4);
		if (status) {
			fprintf (stderr, "%s: %s %s dimension mismatch\n", program, imgroot[0][0], Nmgroot[k]);
			exit (-1);
		}
	}
	if (!control) control = (isbig[0][0]) ? 'b' : 'l';

	vdim = imgdim[0]*imgdim[1]*imgdim[2];
	dimension = vdim*imgdim[3];
	if (!(img1 = (float *) calloc (dimension, sizeof (float)))) errm (program);
	imgv = calloc_double2 (ngroup, dimension);	/* variance accumulator */
	imgs = calloc_double2 (ngroup, dimension);	/* sum      accumulator */
	imgm = calloc_int2    (ngroup, dimension);	/* voxel    counter */
	imgN = calloc_float2  (ngroup, vdim);		/* input N image */

	for (k = 0; k < ngroup; k++) {
/****************/
/* read N image */
/****************/
		printf ("Reading: %s\n", Nmgroot[k]);
		sprintf (imgfile, "%s.4dfp.img", Nmgroot[k]);
		if (!(fp = fopen (imgfile, "rb")) || eread (imgN[k], vdim, isbigN[k], fp)
		|| fclose (fp)) errr (program, imgfile);

/**********************************/
/* read and accumulate image data */
/**********************************/
		for (j = 0; j < nimg[k]; j++) {
			printf ("Reading: %s\n", imgroot[k][j]);
			sprintf (imgfile, "%s.4dfp.img", imgroot[k][j]);
			if (!(fp = fopen (imgfile, "rb")) || eread (img1, dimension, isbig[k][j], fp)
			|| fclose (fp)) errr (program, imgfile);
			for (i = 0; i < dimension; i++) {
				defined = isnormal (img1[i]) && img1[i] != (float) 1.e-37;
				if (!defined) continue;
				imgm[k][i]++;
				imgs[k][i] += img1[i];
				imgv[k][i] += img1[i]*img1[i];
			}
		}
		for (i = 0; i < dimension; i++) {
			u = (imgs[k][i] /= imgm[k][i]);
			imgv[k][i] -= u*u*imgm[k][i]; if (imgv[k][i] < 0.0) imgv[k][i] = 0.0;
			imgv[k][i] /= (imgm[k][i] - 1);
		}
/***************************************/
/* divide variance estimate by N image */
/***************************************/
		for (i = 0; i < vdim; i++) if (imgN[k][i]) for (j = 0; j < imgdim[3]; j++) {
			imgv[k][j*vdim + i] /= imgN[k][i];
		}
	}

/***************************************************************************/
/* compute voxelwize t statistic (Welch's approximate t' when ngroup == 2) */
/***************************************************************************/
	for (i = 0; i < dimension; i++) {
		if (ngroup == 2) {
			imgs[0][i] -= imgs[1][i];
			imgv[0][i] += imgv[1][i];
		}
		if (isnormal (imgs[0][i]) && isnormal (imgv[0][i])) {
			img1[i] = imgs[0][i] / sqrt (imgv[0][i]);
			if (img1[i] > amax) amax = img1[i];
			if (img1[i] < amin) amin = img1[i];
		} else switch (NaN_flag) {
			case 'Z': img1[i] = 0.0;		break;
			case 'E': img1[i] = (float) 1.e-37;	break;
			case 'N': img1[i] = rnan ();		break;
		}
	}

	if (!strlen (outroot)) {
/*******************/
/* compute outroot */
/*******************/
		strcpy (command, lstfile[0]);
		if (!(ptr = strrchr (command, '/'))) ptr = command; else ptr++;
		if (str = strrchr (ptr, '.')) *str = '\0';
		sprintf (outroot, "RFX_%s", ptr);
		if (ngroup == 2) {
			strcat (outroot, "_vs_");
			strcpy (command, lstfile[1]);
			if (!(ptr = strrchr (command, '/'))) ptr = command; else ptr++;
			if (str = strrchr (ptr, '.')) *str = '\0';
			strcat (outroot, ptr);
		}
		strcat (outroot, "_tmap");
	}
	sprintf (outfile, "%s.4dfp.img", outroot);

/****************/
/* write result */
/****************/
	printf ("Writing: %s\n", outfile);
	if (!(fp = fopen (outfile, "wb"))
	|| ewrite (img1, dimension, control, fp)
	|| fclose (fp)) errw (program, outfile);

/*******/
/* ifh */
/*******/
	Writeifh (program, outfile, &ifh, control);

/*******/
/* hdr */
/*******/
	sprintf (command, "ifh2hdr %s -r%.0fto%.0f", outroot, amin, amax);
	printf ("%s\n", command);
	status |= system (command);

/*******/
/* rec */
/*******/
	startrecle (outfile, argc, argv, rcsid, control);
	for (k = 0; k < ngroup; k++) {
		sprintf (command, "imglist %d\n", k + 1);
		printrec (command); catrec (lstfile[k]); printrec ("endimglist\n");
		if (saverec) {
			sprintf (imgfile, "%s.4dfp.img", Nmgroot[k]); catrec (imgfile);
			for (j = 0; j < nimg[k]; j++) {
				sprintf (imgfile, "%s.4dfp.img", imgroot[k][j]); catrec (imgfile);
			}
		}
	}
	endrec ();
	
	free (img1); free_double2 (imgs); free_double2 (imgv); free_int2 (imgm); free_float2 (imgN);
	exit (status);
}
示例#3
0
int main (int argc, char *argv[]) {
/*******/
/* i/o */
/*******/
	FILE		*fpimg, *fpout;
	IFH		ifh;
	struct dsr	hdr;				/* ANALYZE hdr */
	char		imgroot[MAXL], imgfile[MAXL], outfile[MAXL];
	char		trailer[8] = ".4dint";

/****************/
/* image arrays */
/****************/
	float		*imgf, cscale = 1.0;
	short int	*imgi=NULL;
	unsigned char	*imgu=NULL;
	float		voxsiz[3];
	int		imgdim[4], vdim, orient, isbig;
	int		imin = 32767, imax = -32768;
	char		control = '\0';
	short int	origin[3];		/* used in SPM99 conversions */

/***********/
/* utility */
/***********/
	int 		c, i, j, k;
	char		*str, command[MAXL], program[MAXL];

/*********/
/* flags */
/*********/
	int		uchar = 0;
	int		debug = 0;
	int		spm99 = 0;
	int		swab_flag = 0;

	fprintf (stdout, "%s\n", rcsid);
	setprog (program, argv);
/************************/
/* process command line */
/************************/
	for (k = 0, i = 1; i < argc; i++) {
		if (*argv[i] == '-') {
			strcpy (command, argv[i]); str = command;
			while ((c = *str++)) switch (c) {
				case '8': uchar++; strcpy (trailer, "_8bit");	break;
				case 'd': debug++;				break;
				case 'c': cscale = atof (str);			*str = '\0'; break;
				case 'S': if (!strcmp (str, "PM99")) spm99++;	*str = '\0'; break;
				case '@': control = *str++;			*str = '\0'; break;
			}
		} else switch (k) {
			case 0:	getroot (argv[i], imgroot);	k++; break;
		}	
	}
	if (k < 1) {
		printf ("Usage:\t%s <(4dfp) filename>\n", program);
		printf ("\toption\n");
		printf ("\t-c<flt>\tscale output values by specified factor\n");
		printf ("\t-8\toutput 8 bit unsigned char\n");
		printf ("\t-SPM99\tinclude origin and scale in hdr (http:/wideman-one.com/gw/brain/analyze/format.doc)\n");
		printf ("\t-@<b|l>\toutput big or little endian (default CPU endian)\n");
		exit (1);
	}

/*****************************/
/* get input 4dfp dimensions */
/*****************************/
	if (get_4dfp_dimoe (imgroot, imgdim, voxsiz, &orient, &isbig)) errr (program, imgroot);
	vdim = imgdim[0] * imgdim[1] * imgdim[2];
	if (uchar) {
		if (!(imgu = (unsigned char *) malloc (vdim * sizeof (char))))  errm (program);
	} else {
		if (!(imgi = (short *)         malloc (vdim * sizeof (short)))) errm (program);
	}
	if (!(imgf = (float *) malloc (vdim * sizeof (float)))) errm (program);
		
/*************************/
/* open input and output */
/*************************/
	sprintf (imgfile, "%s.4dfp.img", imgroot);
	printf ("Reading: %s\n", imgfile);
	if (!(fpimg = fopen (imgfile, "rb"))) errr (program, imgfile);
 	sprintf (outfile, "%s%s.img", imgroot, trailer);
	if (!(fpout = fopen (outfile, "wb"))) errw (program, outfile);
	printf ("Writing: %s\n", outfile);

/**********************/
/* process all frames */
/**********************/
	for (k = 0; k < imgdim[3]; k++) {
		if (eread (imgf, vdim, isbig, fpimg)) errr (program, imgfile);
		switch (orient) {
			case 4:	flipx (imgf, imgdim + 0, imgdim + 1, imgdim + 2);	/* sagittal */
			case 3:	flipz (imgf, imgdim + 0, imgdim + 1, imgdim + 2);	/* coronal */
			case 2:	flipy (imgf, imgdim + 0, imgdim + 1, imgdim + 2);	/* transverse */
				break;
			default:
				fprintf (stderr, "%s: %s image orientation not recognized\n", program, imgfile);
				exit (-1);
				break;
		}
		for (i = 0; i < vdim; i++) {
			j = nint (cscale*imgf[i]);
			if (debug) printf ("%10.6f%10d\n", imgf[i], j);
			if (uchar) {
				if (j < 0)	j = 0;
				if (j > 255)	j = 255;
				imgu[i] = j;
			} else {
				imgi[i] = j;
			}
			if (j > imax) imax = j;
			if (j < imin) imin = j;
		}
		if (uchar) {
			if (fwrite (         imgu, sizeof (char),  vdim, fpout) != vdim)	errw (program, outfile);
		} else {
			if (gwrite ((char *) imgi, sizeof (short), vdim, fpout, control))	errw (program, outfile);
		}
	}
	fclose (fpimg);
	fclose (fpout);

/**************************/
/* create ANALYZE 7.5 hdr */
/**************************/
	Inithdr (&hdr, imgdim, voxsiz, "");
	if (uchar) {
		hdr.dime.datatype = 2;		/* unsigned char */
		hdr.dime.bitpix = 8;
	} else {
		hdr.dime.datatype = 4;		/* signed integer */
		hdr.dime.bitpix = 16;
	}
	hdr.dime.glmax = imax;
	hdr.dime.glmin = imin;
	hdr.hist.orient = orient - 2;

	swab_flag = ((CPU_is_bigendian() != 0) && (control == 'l' || control == 'L'))
		 || ((CPU_is_bigendian() == 0) && (control == 'b' || control == 'B'));

	if (spm99) {
		if (Getifh (imgroot, &ifh)) errr (program, imgroot);
		for (i = 0; i < 3; i++) origin[i] = 0.4999 + ifh.center[i]/ifh.mmppix[i];
/*************************************************/
/* flip 4dfp->analyze assuming transverse orient */
/*************************************************/
		origin[1] = imgdim[1] + 1 - origin[1];
/*******************************************************************/
/* origin field officially text and so not affected by swab_hdr () */
/*******************************************************************/
		if (swab_flag) for (i = 0; i < 3; i++) swab2 ((char *) (origin + i));
		memcpy ((char *) &hdr + 253, (char *) origin, 3*sizeof (short int));
		memcpy ((char *) &hdr + 112, (char *) &cscale,  sizeof (float));
		hdr.hk.extents = 0;
	}

	if (swab_flag) swab_hdr (&hdr);
	sprintf (outfile, "%s%s.hdr", imgroot, trailer);
	printf ("Writing: %s\n", outfile);
	if (!(fpout = fopen (outfile, "wb")) || fwrite (&hdr, sizeof (struct dsr), 1, fpout) != 1
	|| fclose (fpout)) errw (program, outfile);

/*******************/
/* create rec file */
/*******************/
 	sprintf   (outfile, "%s%s.img", imgroot, trailer);
	startrece (outfile, argc, argv, rcsid, control);
	sprintf   (command, "Voxel values scaled by %f\n", cscale); printrec (command);
	catrec (imgfile);
	endrec ();

	free (imgf);
	if (uchar) {
		free (imgu);
	} else {
		free (imgi);
	}
	exit (0);
}
示例#4
0
文件: crop_4dfp.c 项目: ysu001/PUP
int main (int argc, char *argv[]) {
/************/
/* 4dfp I/O */
/************/
	FILE		*fp_img, *fp_out;
	IFH		ifh;
	char		outfile[MAXL], imgfile[MAXL];
	char		imgroot[MAXL], outroot[MAXL] = "";
	char		control = '\0';

/****************/
/* image arrays */
/****************/
	float		*img1, *imgc;
	float		voxdim[3];
	int		imgdim[4], outdim[4], ix, iy, iz, index, vdim, isbig;
	int		ix0 = 1, ix1 = 0, iy0 = 1, iy1 = 0, iz0 = 1, iz1 = 0;
	int		vdim_crop, orient;

/***********/
/* utility */
/***********/
	int		c, i, j, k;
	char		*ptr, string[MAXL];

/*********/
/* flags */
/*********/
	int		doflip = 0;
	int		docrop = 1;
	int		scrollx = 0, scrolly = 0, scrollz = 0;

	printf ("%s\n", rcsid);
	setprog (program, argv);
/************************/
/* process command line */
/************************/
	for (k = 0, i = 1; i < argc; i++) {
		if (*argv[i] == '-') {
			strcpy (string, argv[i]); ptr = string;
			while (c = *ptr++) switch (c) {
				case 'f': doflip++;		break;
				case 'Z': docrop = 0;		break;
				case '@': control = *ptr++;		*ptr = '\0'; break;
				case 'x': getirange (ptr, &ix0, &ix1);	*ptr = '\0'; break;
				case 'y': getirange (ptr, &iy0, &iy1);	*ptr = '\0'; break;
				case 'z': getirange (ptr, &iz0, &iz1);	*ptr = '\0'; break;
				case 's': switch (*ptr++) {
					case 'x': scrollx = atoi (ptr); break;
					case 'y': scrolly = atoi (ptr); break;
					case 'z': scrollz = atoi (ptr); break;
				}					*ptr = '\0'; break;
			}
		}
		else switch (k) {
			case 0:	getroot (argv[i], imgroot); k++; break;
			case 1:	getroot (argv[i], outroot); k++; break;
		}	
	}
	if (k < 1) {
		printf ("Usage:\t%s <(4dfp) inroot> [(4dfp) outroot]\n", program);
		printf ("\toption\n");
		printf ("\t-<x|y|z><int>[to[<int>]\tspecify x y z crop limits\n");
		printf ("\t-s<x|y|z><int>\tscroll specified axis by specified number of pixels\n");
		printf ("\t-f\tinterpret specifications under 4dfp<->analyze flips\n");
		printf ("\t-Z\tzero voxels instead of physically cropping\n");
		printf ("\t-@<b|l>\toutput big or little endian (default input endian)\n");
		printf ("N.B.:\tcrop limit indices count from 1\n");
		printf ("N.B.:\tscrolling is done after cropping\n");
		printf ("N.B.:\tdefault (4dfp) output root is <(4dfp) inroot>\"_crop\"\n");
		exit (1);
	}

	sprintf (imgfile, "%s.4dfp.img", imgroot);
	if (get_4dfp_dimoe (imgfile, imgdim, voxdim, &orient, &isbig) < 0
	||  Getifh (imgfile, &ifh)) errr (program, imgfile);
	if (!control) control = (isbig) ? 'b' : 'l';
	printf ("input  dimensions  %10d%10d%10d%10d\n",   imgdim[0], imgdim[1], imgdim[2], imgdim[3]);
	printf ("voxel  dimensions  %10.6f%10.6f%10.6f\n", voxdim[0], voxdim[1], voxdim[2]);

	if (ix1 > imgdim[0] || ix1 < 1) ix1 = imgdim[0];
	if (iy1 > imgdim[1] || iy1 < 1) iy1 = imgdim[1];
	if (iz1 > imgdim[2] || iz1 < 1) iz1 = imgdim[2];
	if (ix0 > imgdim[0] || ix0 < 1) ix0 = 1;
	if (iy0 > imgdim[1] || iy0 < 1) iy0 = 1;
	if (iz0 > imgdim[2] || iz0 < 1) iz0 = 1;
	if (ix1 < ix0) {k = ix1; ix1 = ix0; ix0 = k;}
	if (iy1 < iy0) {k = iy1; iy1 = iy0; iy0 = k;}
	if (iz1 < iz0) {k = iz1; iz1 = iz0; iz0 = k;}
	printf ("crop limits x %3d to %3d\n", ix0, ix1);
	printf ("crop limits y %3d to %3d\n", iy0, iy1);
	printf ("crop limits z %3d to %3d\n", iz0, iz1);
	printf ("scroll x %3d\n", scrollx);
	printf ("scroll y %3d\n", scrolly);
	printf ("scroll z %3d\n", scrollz);
	outdim[0] = (docrop) ? ix1 - ix0 + 1 : imgdim[0];
	outdim[1] = (docrop) ? iy1 - iy0 + 1 : imgdim[1];
	outdim[2] = (docrop) ? iz1 - iz0 + 1 : imgdim[2];
	outdim[3] = imgdim[3];
	printf ("output dimensions  %10d%10d%10d%10d\n",   outdim[0], outdim[1], outdim[2], outdim[3]);

	vdim =  imgdim[0] * imgdim[1] * imgdim[2];
	if (!(img1 = (float *) malloc (vdim * sizeof (float)))) errm (program);
	if (!(fp_img = fopen (imgfile, "rb"))) errr (program, imgfile);
	printf ("Reading: %s\n", imgfile);

	if (!strlen (outroot)) sprintf (outroot, "%s_crop", imgroot);
	sprintf (outfile, "%s.4dfp.img", outroot);
	vdim_crop = outdim[0] * outdim[1] * outdim[2];
	if (!(imgc = (float *) calloc (vdim_crop, sizeof (float)))) errm (program);
	if (!(fp_out = fopen (outfile, "wb"))) errw (program, outfile);
	printf ("Writing: %s\n", outfile);

/***********/
/* process */
/***********/
	printf ("processing volume");
	for (k = 0; k < imgdim[3]; k++) {printf (" %d", k + 1); fflush (stdout);
		if (eread (img1, vdim, isbig, fp_img)) errr (program, imgfile);
		if (doflip) {
			if (x4dfp2analyze (img1, imgdim, ifh.orientation)) {
				fprintf (stderr, "%s: invalid %s orientation\n", program, imgroot);
				exit (-1);
			}
		}
		i = 0;
		for (iz = iz0 - 1; iz < iz1; iz++) {
		for (iy = iy0 - 1; iy < iy1; iy++) {
		for (ix = ix0 - 1; ix < ix1; ix++) {
			index = ix + imgdim[0]*(iy + imgdim[1]*iz);
			if (docrop) {
				imgc[i++]	= img1[index];
			} else {
				imgc[index]	= img1[index];
			}
		}}}
		if (scrollx) xscroll (img1, imgc, outdim, scrollx);
		if (scrolly) yscroll (img1, imgc, outdim, scrolly);
		if (scrollz) zscroll (img1, imgc, outdim, scrollz);
		if (doflip) x4dfp2analyze (imgc, outdim, ifh.orientation);
		if (ewrite (imgc, vdim_crop, control, fp_out)) errw (program, outfile);
	} printf ("\n"); fflush (stdout);
	fclose (fp_img);
	fclose (fp_out);

/**************/
/* output ifh */
/**************/
	if (docrop) {
		if (writeifhe (program, outroot, outdim, voxdim, ifh.orientation, control)) errw (program, outroot);
	} else {
		Writeifh (program, outroot, &ifh, control);
	}

/***************/
/* run ifh2hdr */
/***************/
	sprintf (string, "ifh2hdr %s", outroot); status |= system (string);

/************/
/* rec file */
/************/
	startrece (outfile, argc, argv, rcsid, control);
	if (doflip)  printrec ("indices subjected to 4dfp<->analyze orientation dependent flips\n");
	if (!docrop) printrec ("voxels zeroed instead of physically cropped\n");
	sprintf (string, "crop limits x %3d to %3d\n", ix0, ix1); printrec (string);
	sprintf (string, "crop limits y %3d to %3d\n", iy0, iy1); printrec (string);
	sprintf (string, "crop limits z %3d to %3d\n", iz0, iz1); printrec (string);
	if (scrollx) {sprintf (string, "scroll x %3d\n", scrollx); printrec (string);}
	if (scrolly) {sprintf (string, "scroll y %3d\n", scrolly); printrec (string);}
	if (scrollz) {sprintf (string, "scroll z %3d\n", scrollz); printrec (string);}
	catrec (imgfile);
	endrec ();

/************/
/* clean up */
/************/
	free (img1); free (imgc);
	exit (status);
}
示例#5
0
int main (int argc, char **argv) {
/*************/
/* image I/O */
/*************/
	FILE            *fp_img, *fp_out;
	IFH		ifh;
	char            imgfile[MAXL], imgroot[MAXL];
	char            outfile[MAXL], outroot[MAXL] = "";

/**************/
/* processing */
/**************/
	int             imgdim[4], dimension, orient, isbig;
	float           voxdim[3];
	float           *imgr;
	float		thresh=0.0;
	char		control ='\0';

/***********/
/* utility */
/***********/
	int             c, i, k;
	char            *ptr, command[MAXL], program[MAXL];

/*********/
/* flags */
/*********/
        int             status = 0;

	printf ("%s\n", rcsid);
	setprog (program, argv);

/************************/
/* process command line */
/************************/
	for (k = 0, i = 1; i < argc; i++) {
		if (i > 1 && *argv[i] == '-') {
			strcpy (command, argv[i]); ptr = command;
			while ((c = *ptr++)) switch (c) {
				case '@': control = *ptr++;		*ptr = '\0'; break;
			}
		} else switch (k) {
			case 0: thresh = atof (argv[i]);    k++;	break;
			case 1: getroot (argv[i], imgroot); k++;	break;
			case 2: getroot (argv[i], outroot); k++;	break;
		}
	}
	if (k < 2) {
		printf ("Usage:	%s <flt> <file_4dfp> [outroot]\n", program);
		printf ("	option\n");
		printf ("	-@<b|l>\toutput big or little endian (default input endian)\n");
		printf ("e.g.,	%s 90 pt349_study9to9\n", program);
		printf ("e.g.,	%s 90 pt349_study9to9 pt349_study9to9z\n", program);
		printf ("N.B.:	default output 4dfp root is <file_4dfp>\"z\"\n");
		exit (1);
	}

	sprintf (imgfile, "%s.4dfp.img", imgroot);
/***************************************/
/* create output filename if not given */
/***************************************/
	if (!strlen (outroot)) sprintf (outroot, "%sz", imgroot);
	sprintf (outfile, "%s.4dfp.img", outroot);

/*****************************/
/* get 4dfp input dimensions */
/*****************************/
	if (get_4dfp_dimoe (imgfile, imgdim, voxdim, &orient, &isbig) < 0) errr (program, imgroot);
	if (Getifh (imgfile, &ifh)) errr (program, imgroot);
	if (!control) control = (isbig) ? 'b' : 'l';
	dimension = imgdim[0] * imgdim[1] * imgdim[2];

/*****************/
/* alloc buffers */
/*****************/
	if (!(imgr = (float *) malloc (dimension * sizeof (float)))) errm (program);

/***********/
/* process */
/***********/
	if (!(fp_img = fopen (imgfile, "rb"))) errr (program, imgfile);
	if (!(fp_out = fopen (outfile, "wb"))) errw (program, outfile);
	fprintf (stdout, "Reading: %s\n", imgfile);
	fprintf (stdout, "Writing: %s\n", outfile);
	for (k = 0; k < imgdim[3]; k++) {
		if (gread  ((char *) imgr, sizeof (float), dimension, fp_img, isbig)) errr (program, imgfile);
		for (i = 0; i < dimension; i++) if (imgr[i] < thresh) imgr[i] = 0.0;
		if (gwrite ((char *) imgr, sizeof (float), dimension, fp_out, control)) errw (program, outfile);
	}
	fclose (fp_img);
	fclose (fp_out);

/***************/
/* ifh and hdr */
/***************/
	if (Writeifh (program, outfile, &ifh, control)) errw (program, outfile);
	sprintf (command, "ifh2hdr %s", outroot); printf ("%s\n", command);
	status = system (command);

/*******************/
/* create rec file */
/*******************/
	startrece (outfile, argc, argv, rcsid, control);
	catrec    (imgfile);
        endrec    ();

        free (imgr);
	exit (status);
}