コード例 #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
ファイル: compile.c プロジェクト: jsouthworth/newsqueak
void
proggen(Node *t, Node *n)
{
	int or;
	Node *of;
	Errjmp s;
	Store *p;
	long len, loc;
	long nauto, oao;
	int osp, odb;
	extern int (**prog)(Proc*);
	extern int storeprog;
	odb=didbecome;
	oao=autooffset;
	or=returnloc;
	of=formals;
	osp=storeprog;
	autooffset=0;
	returnloc=0;
	formals=t->l;
	errsave(s);
	if(errmark()){
		didbecome=odb;
		returnloc=or;
		formals=of;
		autooffset=oao;
		storeprog=osp;
		errrest(s);
		errjmp();
	}
	loc=here();
	emitconst(0);	/* space for locals pointer */
	pushscope();
	dclformals(t->l);
	autooffset=0;
	emit(Ipushfp);
	nauto=here();
	storeprog=here();
	emitconst(0L);
	gen(n, 0);
	trlrgen();
	patch((int)nauto, autooffset);
	popscope();
	errrest(s);
	if(!didbecome && n && t->r->o.t!=TUnit)
		lerror(n, "prog has no become");
	didbecome=odb;
	autooffset=oao;
	returnloc=or;
	formals=of;
	storeprog=osp;
	len=here()-loc+1;
	p=emalloc(SHSZ+len*WS);
	memcpy(p->data, prog+loc, len*WS);
	p->ref=1;
	p->len=len;
	p->type=Sprog;
	setprog(loc);
	emit(Ipushdata);
	emitconst((long)p);
	emitstore(p);
}
コード例 #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
ファイル: ecatto4dfp.c プロジェクト: ysu001/PUP
int main (int argc, char *argv[])
{
	char		ecatspec[MAXL], ecatname[MAXL];
	char		imgroot[MAXL], imgname[MAXL];
	FILE		*fptr;
	struct Matval	matval;
	MatrixFile	*mptr;
	MatrixData	*matrix;
	MatDirNode	*entry, **entries;
	Image_subheader	*ih;

	float		global_max, global_min;
	int		volume, plane, line, nframes, iframe, jframe, matnum, *missing, nmissing;
	float		scale_factor, calibration_factor/*, scan_time*/;
	int		cubic = CUBIC;

	float		*img;
	short		*ecat;
	
	int		i, j, k;
	char		string[MAXL], *ptr, c;
	char		control = '\0';
	int		isbig;

	IFH		ifh;
	
	setprog (program, argv);

	for (j = 0, i = 1; i < argc; i++)
	if (*argv[i] == '-')
	{
		strncpy (string, argv[i], MAXL); ptr = string;
		while (c = *ptr++) switch (c)
		{
			case '@': control = *ptr++; *ptr = '\0'; break;
		}
	}
	else switch (j)
	{
		case 0: strncpy (ecatspec, argv[i], MAXL);	j++; break;
		case 1: getroot (argv[i], imgroot);		j++; break;
	}
	if (j != 2)
	{
		fprintf (stderr, "Usage:\t%s <ecat> <(4dfp)image>\n", program);
		fprintf (stderr, "\t-@<b|l>\toutput big or little endian (default input endian)\n");
		exit (1);
	}

/******** Get filenames and open ********/
	matspec (ecatspec, ecatname, &matnum);	/* matnum is unused */
	if (!(mptr = matrix_open (ecatname, MAT_READ_ONLY, MAT_UNKNOWN_FTYPE)))
	{
		fprintf (stderr, "%s: cannot open %s as an ECAT image file\n", program, ecatname);
		exit (-1);
	}
	if (mptr->mhptr->file_type != InterfileImage
	 && mptr->mhptr->file_type != PetImage
	 && mptr->mhptr->file_type != PetVolume
	 && mptr->mhptr->file_type != ByteImage
	 && mptr->mhptr->file_type != ByteVolume)
	{
		fprintf (stderr, "%s: filetype not supported\n", program);
		exit (-1);
	}
	sprintf (imgname, "%s.4dfp.img", imgroot);
	if (!(fptr = fopen (imgname, "w"))) errw (program, imgname);
	
	if (!control) control = (CPU_is_bigendian ()) ? 'b' : 'l';
	
	calibration_factor = mptr->mhptr->calibration_factor;
	fprintf (stdout, "Calibration Factor := %10e\n", calibration_factor);

	if (!(missing = (int *)         calloc (mptr->mhptr->num_frames,  sizeof (int)))
	 || !(entries = (MatDirNode **) malloc (mptr->mhptr->num_frames * sizeof (MatDirNode *)))) errm (program);

	sprintf (string, "%s.4dfp.img.rec", imgroot);
	startrece (string, argc, argv, rcsid, control);
	printrec ("Frame     \t    Length\t  Midpoint\t     Start\t Frame_Min\t Frame_Max\t Decay_Fac\tEcat_Frame\n");

	for (nframes = 0, entry = mptr->dirlist->first; entry; entry = entry->next) entries[nframes++] = entry;
	qsort (entries, nframes, sizeof (MatDirNode *), frame_cmp);

	for (iframe = 1, jframe = 0, nmissing = 0/*, scan_time = 0*/; jframe < nframes; iframe++, jframe++)
	{
		entry = entries[jframe];
		mat_numdoc (entry->matnum, &matval);
		while (iframe < matval.frame) {
			fprintf (stderr, "%s: %s frame %d not included\n", program, ecatname, iframe);
			missing[nmissing++] = iframe++;
		}
		if (!(matrix = load_volume (mptr, matval.frame, cubic)))
		{
			fprintf (stderr, "%s: ecat frame %d not found\n", program, matval.frame);
			exit (-1);
		}
		if ( matrix->data_type != SunShort && matrix->data_type != VAX_Ix2)
		{
			fprintf (stderr, "%s: only integer 2 images are currently supported\n", program);
			exit(-1);
		}

		scale_factor = matrix->scale_factor * calibration_factor;
/*		fprintf (stdout, "Scale Factor := %10e\tTotal Factor := %10e\n", matrix->scale_factor, scale_factor);
*/
		if (jframe == 0)
		{
			ifh.scaling_factor[0] = ifh.scaling_factor[1] = 10 * matrix->pixel_size;
			ifh.scaling_factor[2] = 10 * matrix->z_size;
			ifh.matrix_size[0] = matrix->xdim;
			ifh.matrix_size[1] = matrix->ydim;
			ifh.matrix_size[2] = matrix->zdim;
			line   = ifh.matrix_size[0];
			plane  = ifh.matrix_size[1] * line;
			volume = ifh.matrix_size[2] * plane;
			if (!(img = (float *) malloc (volume * sizeof (float)))) errm (program);
			global_min = matrix->data_min;
			global_max = matrix->data_max;
		}
		else
		{
			if (matrix->data_min < global_min) global_min = matrix->data_min;
			if (matrix->data_max > global_max) global_max = matrix->data_max;
		}

/******** Flip Z, and write to output file ********/
		ecat = (short *) matrix->data_ptr;
		for (i = volume-plane; i >= 0;     i -= plane)
/*		for (j = 0;            j <  plane; j += line)
		for (k = 0;            k <  line;  k++, ecat++) img[i+j+k] = (*ecat) * scale_factor;
*/		for (j = 0;            j <  plane; j++, ecat++) img[i+j] = (*ecat) * scale_factor;
		if (ewrite (img, volume, control, fptr)) errw (program, imgname);

		ih = (Image_subheader*) matrix->shptr;
		sprintf (string, "Frame_%-4d\t%10d\t%10.2f\t%10d\t%10d\t%10d\t%10.5f\t%10d\n", jframe+1,
			ih->frame_duration,
			(ih->frame_start_time + ih->frame_duration / 2.0) / 1000/*scan_time += ih->frame_duration / 1000*/,
			ih->frame_start_time,
			ih->image_min,
			ih->image_max,
			ih->decay_corr_fctr,
			iframe);
		printrec (string);
	}
	
	sprintf (string, "%s Missing Frames:", ecatname);
	printrec (string);
	for (i = 0; i < nmissing; i++)
	{
		sprintf (string, " %d", missing[i]);
		printrec (string);
	}
	printrec ("\n");
	endrec ();

/******** Write ifh, hdr, and rec files ********/
	ifh.matrix_size[3]    = nframes;
	ifh.orientation       = TRANSVERSE;
	sprintf (string, "%s.4dfp.ifh", imgroot);
	if (writeifhe (program, string, ifh.matrix_size, ifh.scaling_factor, ifh.orientation, control)) errw (program, string);

	sprintf (string,  "ifh2hdr %s -r%fto%f",  imgroot, global_min, global_max);
	system (string);
	
/******** Free, close and quit ********/
	matrix_close (mptr);
	fclose (fptr);
	free (img);
	exit (0);
}
コード例 #6
0
ファイル: qntv_4dfp.c プロジェクト: ysu001/PUP
int main (int argc, char *argv[]) {
/*************/
/* image I/O */
/*************/
	CONC_BLOCK	conc_block;			/* conc i/o control block */
	FILE            *imgfp, *ROIfp, *outfp, *keyfp;
	char            imgroot[MAXL], imgfile[MAXL], ROIroot[MAXL], ROIfile[MAXL], outfile[MAXL] = "", keyfile[MAXL], tmpfile[MAXL];

/********************/
/* iamge processing */
/********************/
	int             imgdim[4], ROIdim[4], diedim[3], vdim, imgori, ROIori, isbig, isbigm;
	int		lendie = LENDIE, ncrit = NCRIT, ndie, mdie, *mdielist;
	int		ix, iy, iz, jx, jy, jz;
	float           imgvox[3], ROIvox[3];
        float           *imgt, *imgr;
	DIEDAT		*diedat;

/*************************/
/* timeseries processing */
/*************************/
	char		*format;
	double		tol = TOL, **b, *lambda, **v;
	int		nnez, nred;

/***********/
/* utility */
/***********/
	char            *ptr, command[2*MAXL];
	double		q;
	int             c, i, j, k, l, m, n;

/*********/
/* flags */
/*********/
	int		outtype = 0;
	int		conc_flag = 0;
	int             status = 0;
	int		count_zero = 0;
	int		defined;			/* test on imgt and imgr voxels */
	int		test_svd = 1;
	int		H_flag = 0;
	int		K_flag = 0;
	int		V_flag = 0;

/**************/
/* debug code */
/**************/
	double		**bcopy, **e, errmax;

	setprog (program, argv);
	if (!(format = (char *) calloc (1024, sizeof (char)))) errm (program);
/************************/
/* process command line */
/************************/
	for (j = 0, i = 1; i < argc; i++) {
		if (*argv[i] == '-') {
			strcpy (command, argv[i]); ptr = command;
			while (c = *ptr++) switch (c) {
				case 'Z': count_zero++;			break; 
				case 'V': V_flag++;			break; 
				case 'H': H_flag++;			break; 
				case 'K': K_flag++;			break; 
				case 'O': outtype = atoi (ptr);		*ptr = '\0'; break; 
				case 'l': lendie = atoi (ptr);		*ptr = '\0'; break; 
				case 'f': strncpy (format, ptr, 1023);	*ptr = '\0'; break; 
				case 'n': ncrit = atoi (ptr);		*ptr = '\0'; break; 
				case 't': tol = atof (ptr);		*ptr = '\0'; break; 
				case 'o': strcpy (outfile, ptr);	*ptr = '\0'; break; 
				default:				break;
			}
		} else switch (j) {
			case 0:	getroot (argv[i], imgroot);
				conc_flag = (strstr (argv[i], ".conc") == argv[i] + strlen (imgroot));
                						j++; break;
                	case 1: getroot (argv[i], ROIroot);	j++; break;
			default:				break;
		}
	}
	if (j < 2) usage (program);
	if (lendie <= 0 || ncrit <= 0) usage (program);

	if (strlen (outfile)) {
		if (!(outfp = fopen (outfile, "w"))) errw (program, outfile);
		printf ("Writing: %s\n", outfile);
	} else {
		outfp = stdout;
	}
	if (H_flag) write_command_line (outfp, argc, argv);

/**************************************/
/* get input 4dfp dims and open files */
/**************************************/
	if (conc_flag) {
		conc_init_quiet (&conc_block, program);
		conc_open_quiet (&conc_block, imgroot);
		strcpy (imgfile, conc_block.lstfile);
		for (k = 0; k < 4; k++) imgdim[k] = conc_block.imgdim[k];
		for (k = 0; k < 3; k++) imgvox[k] = conc_block.voxdim[k];
		imgori = conc_block.orient;
		isbig  = conc_block.isbig;
	} else {
		sprintf (imgfile, "%s.4dfp.img", imgroot);
		if (get_4dfp_dimoe_quiet (imgfile, imgdim, imgvox, &imgori, &isbig) < 0) errr (program, imgfile);
		if (!(imgfp = fopen (imgfile, "rb"))) errr (program, imgfile);
	}

/*****************/
/* alloc buffers */
/*****************/
	vdim = imgdim[0]*imgdim[1]*imgdim[2];
	if (!(imgt = (float *) malloc (vdim*sizeof (float)))) errm (program);
	if (!(imgr = (float *) malloc (vdim*sizeof (float)))) errm (program);

/****************************/
/* prepare dice from ROIimg */
/****************************/
	sprintf (ROIfile, "%s.4dfp.img", ROIroot);
	if (get_4dfp_dimoe_quiet (ROIfile, ROIdim, ROIvox, &ROIori, &isbigm) < 0) errr (program, ROIfile);
	status = ROIori - imgori;
	for (k = 0; k < 3; k++) status |= (imgdim[k] != ROIdim[k]);
	for (k = 0; k < 3; k++) status |= (fabs (imgvox[k] - ROIvox[k]) > 0.0001);
	if (status) {
		fprintf (stderr, "%s: %s %s vdim mismatch\n", program, imgroot, ROIroot);
		exit (-1);
	}
	fprintf (stdout, "Reading: %s\n", ROIfile);
	if (!(ROIfp = fopen (ROIfile, "rb"))
	|| eread (imgr, vdim, isbigm, ROIfp)
	|| fclose (ROIfp)) errr (program, ROIfile);

	ndie = 1; for (k = 0; k < 3; k++) {
		diedim[k] = imgdim[k]/lendie;
		ndie *= diedim[k];
	}
	if (0) printf ("diedim %d %d %d ndie %d\n", diedim[0], diedim[1], diedim[2], ndie);
	if (!(diedat = (DIEDAT *) calloc (ndie, sizeof (DIEDAT)))) errm (program);
	j = 0;		/* index of die */
	for (iz = 0; iz < imgdim[2] - (imgdim[2] % lendie); iz += lendie) {
	for (iy = 0; iy < imgdim[1] - (imgdim[1] % lendie); iy += lendie) {
	for (ix = 0; ix < imgdim[0] - (imgdim[0] % lendie); ix += lendie) {
	if (0) printf ("ix, iy, iz, j %5d%5d%5d%10d\n", ix, iy, iz, j);
		diedat[j].fx = ix + lendie/2. - 0.5;
		diedat[j].fy = iy + lendie/2. - 0.5;
		diedat[j].fz = iz + lendie/2. - 0.5;
		j++;
	}}}
	assert (j == ndie);

	for (iz = 0; iz < imgdim[2] - (imgdim[2] % lendie); iz++) {jz = iz/lendie;
	for (iy = 0; iy < imgdim[1] - (imgdim[1] % lendie); iy++) {jy = iy/lendie;
	for (ix = 0; ix < imgdim[0] - (imgdim[0] % lendie); ix++) {jx = ix/lendie;
		j = jx + diedim[0]*(jy + diedim[1]*jz);		/* index of die */
		i = ix + imgdim[0]*(iy + imgdim[1]*iz);		/* index of voxel */
		defined = isnormal (imgr[i]) && imgr[i] != (float) 1.e-37;
		if (defined) {
			if (diedat[j].n == diedat[j].ma) {
				diedat[j].ma += INCR;
				if (!diedat[j].p) {
					diedat[j].p =  		    malloc (diedat[j].ma*sizeof (int));
				} else {
					diedat[j].p = realloc (diedat[j].p, diedat[j].ma*sizeof (int));
				}
				if (!diedat[j].p) errm (program);
			}
			diedat[j].p[diedat[j].n++] = i;
		}
		i++;
	}}}

/********************/
/* create mdie list */
/********************/
	for (mdie = j = 0; j < ndie; j++) {
		if (V_flag) printf ("j diedat[j].n %5d %5d\n", j, diedat[j].n);
		if (diedat[j].n >= ncrit) mdie++;
	}
	printf ("defined die count = %d\n", mdie);
	if (!(mdielist = (int *) calloc (mdie, sizeof (int)))) errm (program);
	for (mdie = j = 0; j < ndie; j++) if (diedat[j].n >= ncrit) mdielist[mdie++] = j;

	if (K_flag) {
/*********************************/
/* create voxel address key file */
/*********************************/
		sprintf (tmpfile, "temp%d", getpid ());
		if (!(keyfp = fopen (tmpfile, "w"))) errw (program, tmpfile);
		for (m = 0; m < mdie; m++) {
			j = mdielist[m];			/* index of die with at least ncrit voxels in ROI mask */
			fprintf (keyfp, "%10.2f%10.2f%10.2f\n", diedat[j].fx, diedat[j].fy, diedat[j].fz);
		}
		if (fclose (keyfp)) errw (program, tmpfile);
		strcpy (keyfile, imgroot);
		if (!(ptr = strrchr (keyfile, '/'))) ptr = keyfile; else ptr++;
		strcpy (keyfile, ptr);
		strcat (keyfile, "_");
		strcpy (command, ROIroot);
		if (!(ptr = strrchr (command, '/'))) ptr = command; else ptr++;
		strcat (keyfile, ptr);
		strcat (keyfile, "_indices.txt");
		if (!(keyfp = fopen (keyfile, "w"))) errw (program, keyfile);
		printf ("Writing: %s\n", keyfile);
		write_command_line (keyfp, argc, argv);
		if (fclose (keyfp)) errw (program, keyfile);
		sprintf (command, "index2atl %s %s >> %s",
			(conc_flag) ? conc_block.imgfile0[0] : imgroot, tmpfile, keyfile);
		printf ("%s\n", command);
		status |= system (command);
		remove (tmpfile);
		if (status) exit (-1);
	}

/****************/
/* parse format */
/****************/
	if (!(format = (char *) realloc (format, (imgdim[3] + 1)*sizeof (char)))) errm (program);
	if (strlen (format)) {
		if (k = expandf (format, imgdim[3] + 1)) exit (-1);
		if (strlen (format) != imgdim[3]) {
			fprintf (stderr, "%s: %s frame-to-count format length mismatch\n", program, imgroot);
			exit (-1); 
		}
		for (nnez = k = 0; k < imgdim[3]; k++) if (format[k] != 'x') nnez++;
	} else {
		nnez = imgdim[3];
		for (i = 0; i < imgdim[3]; i++) format[i] = '+';
	}
	printf ("%s\n", format);
	printf ("%s: time series defined for %d frames, %d exluded\n", program, imgdim[3], imgdim[3] - nnez);

/***************************/
/* allocate memory for svd */
/***************************/
	if (!(lambda = (double *) calloc (nnez, sizeof (double)))) errm (program);
	v = calloc_double2 (nnez, nnez);
	b = calloc_double2 (mdie, nnez);

/***********/
/* process */
/***********/
	printf ("Reading: %s\n", imgfile);
	for (n = k = 0; k < imgdim[3]; k++) {
		if (conc_flag) {
			conc_read_vol (&conc_block, imgt);
		} else {
			if (eread (imgt, vdim, isbig, imgfp)) errr (program, imgfile);
		}
		for (m = 0; m < mdie; m++) {
			j = mdielist[m];			/* index of die with at least ncrit voxels in ROI mask */
			diedat[j].v = diedat[j].m = 0;		/* initialize anew for each frame */
			for (l = 0; l < diedat[j].n; l++) {	/* loop on voxels */
				i = diedat[j].p[l];		/* index into imgt */
				defined = isnormal (imgt[i]) && imgt[i] != (float) 1.e-37;
				if (count_zero && imgt[i] == 0.) defined = 1;
				if (defined) {
					diedat[j].v += imgt[i];
					diedat[j].m++;
				}
			}
			if (diedat[j].m < ncrit) {
				fprintf (stderr, "%s: %s contains undefined voxels within %s mask\n",
								program, imgfile, ROIroot);
				exit (-1);
			}
			diedat[j].v /= diedat[j].m;
		}
		if (format[k] != 'x') {
			for (m = 0; m < mdie; m++) {
				j = mdielist[m];		/* index of die with at least ncrit voxels in ROI mask */
				b[m][n] = diedat[j].v;
			}
			n++;					/* increment non-skipped frames count */
		}
	}
	assert (n == nnez);
	free (imgt); free (imgr);
	if (conc_flag) {
		conc_free (&conc_block);
	} else {
		if (fclose (imgfp)) errr (program, imgfile);
	}
	if (!outtype) goto DONE;

/*****************************************************/
/* make timeseries zero mean (but not unit variance) */
/*****************************************************/
	if (outtype > 1) for (j = 0; j < mdie; j++) dzeromean (b[j], nnez);

/*******************************/
/* output extracted timeseries */
/*******************************/
	if (outtype == 1 || outtype == 2) {
		for (n = k = 0; k < imgdim[3]; k++) {
			for (m = 0; m < mdie; m++) fprintf (outfp, " %9.4f", (format[k] != 'x') ? b[m][n] : 0.0);
			if (format[k] != 'x') n++;
			if (mdie) fprintf (outfp, "\n");
		}
		assert (n == nnez);
		goto DONE;
	}

	if (test_svd) {
		bcopy = calloc_double2 (mdie, nnez);
   	   	    e = calloc_double2 (mdie, nnez);
		for (j = 0; j < mdie; j++) for (i = 0; i < nnez; i++) bcopy[j][i] = b[j][i];
	}
   	        dsvdcmp0 (b, mdie, nnez, lambda, v);
	nred = ndsvdcmp0 (b, mdie, nnez, lambda, v, tol);
	if (H_flag) {
		fprintf (outfp, "#reduced dimensionality = %d\n", nred);
		fprintf (outfp, "#lambda after ndsvdcmp0");
		for (i = 0; i < nred; i++) fprintf (outfp, " %.2f", lambda[i]); fprintf (outfp, "\n");
	}
	if (test_svd) {
		for (j = 0; j < mdie; j++) for (i = 0; i < nnez; i++) for (k = 0; k < nred; k++) {
			e[j][i] += v[i][k]*lambda[k]*b[j][k];
		}
		for (errmax = i = 0; i < nnez; i++) for (j = 0; j < mdie; j++) {
			q = fabs (e[j][i] - bcopy[j][i]);
			if (q > errmax) errmax = q;
		}
		if (H_flag) fprintf (outfp, "#maximum error = %.3g\n", errmax);
		free_double2 (bcopy); free_double2 (e);
	}
	for (k = 0; k < nnez; k++) for (j = 0; j < nred; j++) {
		v[k][j] *= (outtype == 3) ? lambda[j] : sqrt ((double) nnez);
	}
/******************************/
/* output computed timeseries */
/******************************/
	if (outtype == 3 || outtype == 4) {
		for (n = k = 0; k < imgdim[3]; k++) {
			for (m = 0; m < nred; m++) fprintf (outfp, " %9.4f", (format[k] != 'x') ? v[n][m] : 0.0);
			if (format[k] != 'x') n++;
			if (mdie) fprintf (outfp, "\n");
		}
		assert (n == nnez);
	}

/*********************/
/* clean up and exit */
/*********************/
DONE:	if (strlen (outfile)) if (fclose (outfp)) errw (program, outfile);
	free_double2 (v); free_double2 (b); free (lambda);
	free (mdielist);
	for (j = 0; j < ndie; j++) if (diedat[j].p) free (diedat[j].p);
	free (diedat);
	free (format);
	exit (status);
}
コード例 #7
0
ファイル: 4dfptoanalyze.c プロジェクト: ewong718/freesurfer
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);
}
コード例 #8
0
ファイル: zero_lt_4dfp.c プロジェクト: ewong718/freesurfer
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);
}