Exemple #1
0
int main (int argc, char *argv[]) {
	FILE 		*anafp;
	char            in1root[MAXL];
	char            in2root[MAXL];
	char		outroot[MAXL];
	char		imgfile[MAXL];
	char            program[MAXL], string[MAXL], *ptr;

	struct dsr	hdr;
	short		*img1, *img2, *imgo;
	int		xdim, ydim, zdim, dimension;
	int		img1max = 0, img2max = 0;
	int		img1min = 0, img2min = 0;
	int		swab_img1 = 0, swab_img2 = 0, swab_imgo = 0;
	char		orient, control = '\0';

	int		nbin = NBIN;
	int		*hist, *mrg1, *mrg2;
	int		histmax = 0;
	int		range1, range2;
	int		binwid1, binwid2;
	int		grid1, grid2;
	short		valmax = 0;

	float		ftmp;
	int		c, i, j, k;

/*********/
/* flags */
/*********/
	int		status = 0;
	int		debug = 0;
	int		do_grid = 1;

	if (ptr = strrchr (argv[0], '/')) ptr++; else ptr = argv[0];
	strcpy (program, ptr);
	printf ("%s\n", rcsid);
/************************/
/* 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 'd': debug++;		break;
			case 'g': do_grid = 0;		break;
			case '@': control = *ptr++;	*ptr = '\0'; break;
			case 'r': j = atoi (ptr++);
				ptr++;		/* skip over ":" */
				switch (j) {
					case 1: getrangei (ptr, &img1min, &img1max);	break;
					case 2: getrangei (ptr, &img2min, &img2max);	break;
					default: usage (program);			break;
				}			*ptr = '\0'; break;
		}
	} else switch (k) {
		case 0: getroot (argv[i], in1root);	k++; break;
		case 1: getroot (argv[i], in2root);	k++; break;
		case 2: getroot (argv[i], outroot);	k++; break;
	}
	if (k < 3) usage (program);
	fprintf (stdout, "img1: %s\nimg2: %s\n", in1root, in2root);

	sprintf (imgfile, "%s.hdr", in1root);
	if (!(anafp = fopen (imgfile, "rb")) || fread (&hdr, sizeof (struct dsr), 1, anafp) != 1
	|| fclose (anafp)) errr (program, imgfile);
	if (hdr.hk.sizeof_hdr != sizeof (struct dsr)) {
		printf ("converting %s byte order\n", in1root);
		swab_hdr (&hdr);
		swab_img1++;
	}
	if (hdr.dime.bitpix != 16) erri (program, imgfile);
	xdim = hdr.dime.dim[1];
	ydim = hdr.dime.dim[2];
	zdim = hdr.dime.dim[3];
	orient = hdr.hist.orient;
	dimension = xdim * ydim * zdim;

	sprintf (imgfile, "%s.hdr", in2root);
	if (!(anafp = fopen (imgfile, "rb")) || fread (&hdr, sizeof (struct dsr), 1, anafp) != 1
	|| fclose (anafp)) errr (program, imgfile);
	if (hdr.hk.sizeof_hdr != sizeof (struct dsr)) {
		printf ("converting %s byte order\n", in2root);
		swab_hdr (&hdr);
		swab_img2++;
	}
	if (hdr.dime.bitpix != 16) erri (program, imgfile);
	if (xdim != hdr.dime.dim[1] || ydim != hdr.dime.dim[2] || zdim != hdr.dime.dim[3] || orient != hdr.hist.orient) {
		fprintf (stderr, "%s: %s %s dimension/orientation mismatch\n", program, in1root, in2root);
		exit (-1);
	}

	img1 = (short *) malloc (dimension * sizeof (short));
	img2 = (short *) malloc (dimension * sizeof (short));
	mrg1 = (int *) calloc (nbin, sizeof (int));
	mrg2 = (int *) calloc (nbin, sizeof (int));
	hist = (int *) calloc (nbin*nbin, sizeof (int));
	imgo = (short *) malloc (nbin*nbin * sizeof (short));
	if (!img1 || !img2 || !hist || !mrg1 || !mrg2 || !imgo) errm (program);

	sprintf (imgfile, "%s.img", outroot); startrece (imgfile, argc, argv, rcsid, control);
	sprintf (imgfile, "%s.img", in1root); catrec (imgfile);
	printf ("Reading: %s\n", imgfile);
	if (!(anafp = fopen (imgfile, "rb"))
	|| fread (img1, sizeof (short), dimension, anafp) != dimension || fclose (anafp)) errr (program, imgfile);
	if (swab_img1) for (i = 0; i < dimension; i++) swab2 ((char *) &img1[i]);

	sprintf (imgfile, "%s.img", in2root); catrec (imgfile);
	printf ("Reading: %s\n", imgfile);
	if (!(anafp = fopen (imgfile, "rb"))
	|| fread (img2, sizeof (short), dimension, anafp) != dimension || fclose (anafp)) errr (program, imgfile);
	if (swab_img2) for (i = 0; i < dimension; i++) swab2 ((char *) &img2[i]);

	if (!img1max && !img1min) {
		img1max -32768; img1min = 32767;
		for (i = 0; i < dimension; i++) {
			if (img1[i] > img1max) img1max = img1[i];
			if (img1[i] < img1min) img1min = img1[i];
		}
	} else {
		for (i = 0; i < dimension; i++) {
			if (img1[i] > img1max) img1[i] = img1max;
			if (img1[i] < img1min) img1[i] = img1min;
		}
	}
	range1 = img1max - img1min;
	if (!img2max && !img2min) {
		img2max -32768; img2min = 32767;
		for (i = 0; i < dimension; i++) {
			if (img2[i] > img2max) img2max = img2[i];
			if (img2[i] < img2min) img2min = img2[i];
		}
	} else {
		for (i = 0; i < dimension; i++) {
			if (img2[i] > img2max) img2[i] = img2max;
			if (img2[i] < img2min) img2[i] = img2min;
		}
	}
	range2 = img2max - img2min;

	fprintf (stdout, "img1: min=%6d\tmax=%6d\trange=%6d\n", img1min, img1max, range1);
	fprintf (stdout, "img2: min=%6d\tmax=%6d\trange=%6d\n", img2min, img2max, range2);
	sprintf (string, "before auto range adjust\n"); printrec (string);
	sprintf (string, "img1: min=%6d\tmax=%6d\trange=%6d\n", img1min, img1max, range1); printrec (string);
	sprintf (string, "img2: min=%6d\tmax=%6d\trange=%6d\n", img2min, img2max, range2); printrec (string);

	for (i = 0; i < dimension; i++) {
		k = (nbin * (img1[i] - img1min)) / range1;
		if (k < nbin && k > 0) mrg1[k]++;
		k = (nbin * (img2[i] - img2min)) / range2;
		if (k < nbin && k > 0) mrg2[k]++;
	}
	for (k = 2; k < nbin; k++) {
		mrg1[k] += mrg1[k - 1];
		mrg2[k] += mrg2[k - 1];
	}
	if (debug) for (k = 0; k < nbin; k++) {
		printf ("%6d  %10.6f  %10.6f\n", k,
			(float) mrg1[k] / (float) mrg1[nbin - 1], (float) mrg2[k] / (float) mrg2[nbin - 1]);
	}
	for (i = 1; i < nbin; i++) if (((float) mrg1[i] / (float) mrg1[nbin - 1]) > CUM_D) break;
	for (j = 1; j < nbin; j++) if (((float) mrg1[j] / (float) mrg1[nbin - 1]) > 1.0-CUM_D) break;
	if (debug) printf ("img1: first_bin=%d\tlast_bin=%d\n", i, j);
	img1min += (float) (range1 * i) / nbin;
	range1  *= (float) (j - i) / nbin;
	binwid1 = (float) range1 / nbin;
	if (!binwid1) binwid1++;
	range1 = binwid1 * nbin;
	img1min += binwid1 - (img1min + range1) % binwid1;
	for (i = 1; i < nbin; i++) if (((float) mrg2[i] / (float) mrg2[nbin - 1]) > CUM_D) break;
	for (j = 1; j < nbin; j++) if (((float) mrg2[j] / (float) mrg2[nbin - 1]) > 1.0-CUM_D) break;
	if (debug) printf ("img2: first_bin=%d\tlast_bin=%d\n", i, j);
	img2min += (float) (range2 * i) / nbin;
	range2  *= (float) (j - i) / nbin;
	binwid2 = (float) range2 / nbin;
	if (!binwid2) binwid2++;
	range2 = binwid2 * nbin;
	img2min += binwid2 - (img2min + range2) % binwid2;
	fprintf (stdout, "img1: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img1min, img1min + range1, range1, binwid1);
	fprintf (stdout, "img2: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img2min, img2min + range2, range2, binwid2);
	sprintf (string, "after auto range adjust\n");
		printrec (string);
	sprintf (string, "img1: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img1min, img1min + range1, range1, binwid1);
		printrec (string);
	sprintf (string, "img2: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img2min, img2min + range2, range2, binwid2);
		printrec (string);
	if (!range1 || !range2) exit (-1);

	for (k = 0; k < dimension; k++) {
		i = (img1[k] - img1min) / binwid1;
		j = (img2[k] - img2min) / binwid2;
		if (i < 0 || i >= nbin || j < 0 || j >= nbin) continue;
		hist[nbin * j + i]++;
		if (!hist[nbin * j + i]) {
			fprintf (stderr, "2Dhist: histogram bin overflow\n");
			exit (-1);
		}
		if (hist[nbin * j + i] > histmax) histmax = hist[nbin * j + i];
	}
	fprintf (stdout, "maximum bin count=%d\n", histmax);

/***************************/
/* write histogram to imgo */
/***************************/
	for (k = 0; k < nbin*nbin; k++) {
		if (hist[k]) {
			ftmp = 100 * log ((double) hist[k]);
			imgo[k] = (short) ftmp;
		} else imgo[k] = 0;
		if (imgo[k] > valmax) valmax = imgo[k];
	}

	if (do_grid) {
		grid1 = range1 / 10; grid1 /= 10; grid1 *= 10;
		for (i = k = 0; i < nbin; k += grid1) {
			i = (k - img1min) / binwid1;
			if (i >= 0 && i < nbin) for (j = 0; j < nbin; j++) imgo[nbin * j + i] = valmax / 2;
		}
		sprintf (string, "img1: grid interval=%d\n", grid1); printrec (string);

		grid2 = range2 / 10; grid2 /= 10; grid2 *= 10;
		for (j = k = 0; j < nbin; k += grid2) {
			j = (k - img2min) / binwid2;
			if (j >= 0 && j < nbin) for (i = 0; i < nbin; i++) imgo[nbin * j + i] = valmax / 2;
		}
		sprintf (string, "img2: grid interval=%d\n", grid2); printrec (string);
	}

	swab_imgo = ((CPU_is_bigendian() != 0) && (control == 'l' || control == 'L'))
		 || ((CPU_is_bigendian() == 0) && (control == 'b' || control == 'B'));
	if (swab_imgo) for (i = 0; i < nbin*nbin; i++) swab2 ((char *) &imgo[i]);
	sprintf (imgfile, "%s.img", outroot); fprintf (stdout, "Writing: %s\n", imgfile);
	if (!(anafp = fopen (imgfile, "wb")) || fwrite (imgo, sizeof (short), nbin*nbin, anafp) != nbin*nbin
	|| fclose (anafp)) errw (program, imgfile);

	hdr.dime.dim[0] = 2;
	hdr.dime.dim[1] = nbin;
	hdr.dime.dim[2] = nbin;
	hdr.dime.dim[3] = 1;
	hdr.dime.datatype = 4;
	hdr.dime.pixdim[1] = 1;
	hdr.dime.pixdim[2] = 1;
	hdr.dime.pixdim[3] = 1;
	hdr.dime.glmax = valmax;
	hdr.dime.glmin = 0;
	hdr.hist.orient = 0;
	if (swab_imgo) swab_hdr (&hdr);
	sprintf (imgfile, "%s.hdr", outroot);
	fprintf (stdout, "Writing: %s\n", imgfile);
	if (!(anafp = fopen (imgfile, "wb"))
	|| fwrite (&hdr, sizeof (struct dsr), 1, anafp) != 1 || fclose (anafp)) errw (program, imgfile);

	free (img1); free (img2); free (imgo);
	free (mrg1); free (mrg2);
	free (hist);
	endrec ();

	exit (status);
}
Exemple #2
0
int main (int argc, char *argv[]) {
	FILE		*imgfp;			/* input file */
	FILE		*outfp;			/* output file */

/*************/
/* image i/o */
/*************/
	char		*ptr, string[MAXL], program[MAXL], command[MAXL];
	char		imgroot[MAXL], imgfile[MAXL], ifhfile[MAXL]; 
	char		mskroot[MAXL], mskfile[MAXL]; 
	char		tmproot[MAXL], tmpfile[MAXL]; 
	char		trailer[MAXL] = "", outroot[MAXL], outfile[MAXL], recfile[MAXL];

/***************/
/* computation */
/***************/
	float		*imgv, *imgr, *imgm;
	float		v, frac, t, del2v, dmin;
	float		fndex[3], x[3], w[3], dvdx[3], d2vdx2[3];
	float		ctneg = 0.0, ctpos = 0.0;
	float		vtneg = 0.0, vtpos = 0.0;
	float		srad = 0.0, orad = 0;
	int		ix, iy, iz, dim, nx, ny, nz;
	int		c, i, j, k, l, jmin;
	int		isbig, isbigm;
	char		control = '\0';

/**************/
/* peak lists */
/**************/
	EXTREMUM	*ppos, *pneg, *pall, *pallN, ptmp[1];
	int		mpos = 0, mneg = 0, npos = 0, nneg = 0, nall, nallN, Nvoxcrit = 1;
	int		npos0, npos1, npos2;
	int		nneg0, nneg1, nneg2;

/*********/
/* flags */
/*********/
	int		mask = 0;
	int		status = 0;
	int		debug = 0;
	int		quiet = 0;
	int		forceblur = 0;
	
	printf ("%s\n", rcsid);
	if (!(ptr = strrchr (argv[0], '/'))) ptr = argv[0]; else ptr++;
	strcpy (program, ptr);

/******************************/
/* get command line arguments */
/******************************/
	for (k = 0, i = 1; i < argc; i++) {
		if (*argv[i] == '-') {
			strcpy (command, argv[i]); ptr = command;
			while (c = *ptr++) switch (c) {
				case 'q': quiet++;				break;
				case 'F': forceblur++;				break;
				case 's': srad = atof (ptr);			*ptr = '\0'; break;
				case 'd': dthresh = atof (ptr);			*ptr = '\0'; break;
				case 'n': ntop = atoi (ptr);			*ptr = '\0'; break;
				case 'N': Nvoxcrit = atoi (ptr);		*ptr = '\0'; break;
				case 'o': orad = atof (ptr);			*ptr = '\0'; break;
				case '@': control = *ptr++;			*ptr = '\0'; break;
				case 'c': getrange (ptr, &ctneg, &ctpos);	*ptr = '\0'; break;
				case 'v': getrange (ptr, &vtneg, &vtpos);	*ptr = '\0'; break;
				case 'm': getroot (ptr, mskroot); mask++;	*ptr = '\0'; break;
				case 'a': strncpy (trailer, ptr, MAXL - 1);	*ptr = '\0'; break;
			}
		} else switch (k) {
		 	case 0: getroot (argv[i], imgroot);	k++; break;
		}	
	}
	if (k < 1) {
		printf ("Usage: %s <file_4dfp>\n", program);
		printf (" e.g., %s grand_average_222[.4dfp.img] -s10\n", program);
		printf ("\toption\n");
		printf ("\t-s<flt>\tpreblur with hard sphere kernel of specified radius\n");
		printf ("\t-n<int>\tlimit initial pos and neg peak list lengths (default=%d)\n", NTOP);
		printf ("\t-c<flt>[to<flt>] specify sign inverted curvature thresholds (default none)\n");
		printf ("\t-v<flt>[to<flt>] specify peak value thresholds (default none)\n");
		printf ("\t-d<flt>\tconsolidate extremum pairs closer than specified distance\n");
		printf ("\t-o<flt>\toutput a fidl compatible 4dfp format ROI file with regions of specified radius\n");
		printf ("\t-m<str>\tapply named mask file to output ROIs\n");
		printf ("\t-N<int>\tspecify output ROI minimum voxel count (default = 1)\n");
		printf ("\t-a<str>\tappend specified string to ROI output filename\n");
		printf ("\t-q\tquiet mode (suppress rec file listing)\n");
		printf ("\t-F\tforce preblur image creation even if hsphere_4dfp result exists\n");
		printf ("\t-@<b|l>\toutput big or little endian (default input endian)\n");
		printf ("N.B.:\toperations controlled by options -s, -n, -c, -v, -d, -o, -m, -N are applied serially in listed order\n");
		printf ("N.B.:\tall distances are in mm\n");
		printf ("N.B.:\toption -s<flt> creates a blurred image by invoking hsphere_4dfp\n");
		printf ("N.B.:\toption -F is intended for use in iterative scripts and has no effect absent -s<flt>\n");
		exit (1);
	}

/************/
/* read ifh */
/************/
	sprintf (imgfile, "%s.4dfp.img", imgroot);
	if (srad > 0.0) {
		sprintf (tmproot, "%s_%.0fmm", imgroot, srad);
	} else {
		strcpy  (tmproot, imgroot);
	}
	sprintf (tmpfile, "%s.4dfp.img", tmproot);
	sprintf (ifhfile, "%s.4dfp.ifh", imgroot);

	fprintf (stdout, "Reading: %s\n", ifhfile);
	if (Getifh (ifhfile, &imgifh)) errr (program, ifhfile);
	isbig = strcmp (imgifh.imagedata_byte_order, "littleendian");
	if (!control) control = (isbig) ? 'b' : 'l';
	printf ("image dimensions \t%10d%10d%10d%10d\n",
		imgifh.matrix_size[0], imgifh.matrix_size[1], imgifh.matrix_size[2], imgifh.matrix_size[3]);
	printf ("image mmppix     \t%10.6f%10.6f%10.6f\n",
			imgifh.mmppix[0], imgifh.mmppix[1], imgifh.mmppix[2]);
	printf ("image center     \t%10.4f%10.4f%10.4f\n",
			imgifh.center[0], imgifh.center[1], imgifh.center[2]);
	printf ("image orientation\t%10d\n", imgifh.orientation);

/********************************************************************/
/* check that optionally specified mask is dimensionally consistent */
/********************************************************************/
	if (mask) {
		sprintf (mskfile, "%s.4dfp.img", mskroot);
		sprintf (ifhfile, "%s.4dfp.ifh", mskroot);
		fprintf (stdout, "Reading: %s\n", ifhfile);
		if (Getifh (ifhfile, &mskifh)) errr (program, ifhfile);
		isbigm = strcmp (mskifh.imagedata_byte_order, "littleendian");
		status = imgifh.orientation - mskifh.orientation;
		for (k = 0; k < 3; k++) {
			status |= imgifh.matrix_size[k] - mskifh.matrix_size[k];
			status |= (fabs ((double) (imgifh.mmppix[k] - mskifh.mmppix[k])) > 0.0001);
		}
		if (status) {
			fprintf (stderr, "%s: %s %s dimension mismatch\n", program, imgroot, mskroot);
			exit (-1);
		}
	}

	dim = 1; for (k = 0; k < 3; k++) dim *= imgifh.matrix_size[k];
	if (!(imgv = (float *) malloc (dim * sizeof (float)))) errm (program);
	if (!(imgr = (float *) malloc (dim * sizeof (float)))) errm (program);
	if (!(imgm = (float *) malloc (dim * sizeof (float)))) errm (program);
	nx = imgifh.matrix_size[0];
	ny = imgifh.matrix_size[1];
	nz = imgifh.matrix_size[2]; 

/*****************************************/
/* virtual flip instead of x4dfp2ecat () */
/*****************************************/
	vrtflip_ (&imgifh.orientation, imgifh.matrix_size, imgifh.center, imgifh.mmppix, centerr, mmppixr);
	printf ("atlas mmppix     \t%10.6f%10.6f%10.6f\n", mmppixr[0], mmppixr[1], mmppixr[2]); 
	printf ("atlas center     \t%10.4f%10.4f%10.4f\n", centerr[0], centerr[1], centerr[2]);

/****************************************************/
/* read filtered image or create using hpshere_4dfp */
/****************************************************/
	if (forceblur || access (tmpfile, R_OK)) {
		sprintf (command, "hsphere_4dfp %s %.4f", imgfile, srad);
		printf ("%s\n", command);
		if (system (command)) errw (program, tmpfile);
	}
	printf ("Reading: %s\n", tmpfile);
	if (!(imgfp = fopen (tmpfile, "rb")) || eread (imgv, dim, isbig, imgfp)
	|| fclose (imgfp)) errr (program, tmpfile);

/************************************************************************************************/
/* intial extremum memory allocation (realloc on unassigned pointer => unpredictable core dump) */
/************************************************************************************************/
	if (!(ppos = (EXTREMUM *) malloc ((mpos = MSIZE) * sizeof (EXTREMUM)))) errm (program);
	if (!(pneg = (EXTREMUM *) malloc ((mneg = MSIZE) * sizeof (EXTREMUM)))) errm (program);
/*******************/
/* compile extrema */
/*******************/
	printf ("peak value     thresholds %10.4f to %10.4f\n", vtneg, vtpos);
	printf ("peak curvature thresholds %10.4f to %10.4f\n", ctneg, ctpos);
	printf ("compiling extrema slice");
	for (iz = 1; iz < imgifh.matrix_size[2] - 1; iz++) {printf (" %d", iz + 1); fflush (stdout);
	for (iy = 1; iy < imgifh.matrix_size[1] - 1; iy++) {
	for (ix = 1; ix < imgifh.matrix_size[0] - 1; ix++) {
		i = ix + nx*(iy + ny*iz);
		dvdx[0] = 0.5*(-imgv[i - 1]	+ imgv[i + 1]);
		dvdx[1] = 0.5*(-imgv[i - nx]	+ imgv[i + nx]);
		dvdx[2] = 0.5*(-imgv[i - nx*ny]	+ imgv[i + nx*ny]);
		d2vdx2[0] = -2.0*imgv[i] + imgv[i - 1]		+ imgv[i + 1];
		d2vdx2[1] = -2.0*imgv[i] + imgv[i - nx]		+ imgv[i + nx];
		d2vdx2[2] = -2.0*imgv[i] + imgv[i - nx*ny]	+ imgv[i + nx*ny];
		for (k = 0; k < 3; k++) w[k] = -dvdx[k] / d2vdx2[k];
		for (del2v = k = 0; k < 3; k++) del2v += d2vdx2[k] / (mmppixr[k] * mmppixr[k]);
		fndex[0] = (float) (ix + 1) + w[0];
		fndex[1] = (float) (iy + 1) + w[1];
		fndex[2] = (float) (iz + 1) + w[2];
		for (k = 0; k < 3; k++) x[k] = fndex[k] * mmppixr[k] - centerr[k];

		if (imgv[i] > 0.0
		&& del2v <= -ctpos
		&& imgv[i] > imgv[i - 1]	&& imgv[i] > imgv[i + 1]
		&& imgv[i] > imgv[i - nx]	&& imgv[i] > imgv[i + nx]
		&& imgv[i] > imgv[i - nx*ny]	&& imgv[i] > imgv[i + nx*ny]) {
			imgvalx_ (imgv, &nx, &ny, &nz, centerr, mmppixr, x, &t, &l);
			if (l < 1) {printf ("undefined imgvalx point\n"); continue;}
			if (t < vtpos) continue;
			if (mpos <= npos) {
				mpos += MSIZE;
				if (!(ppos = (EXTREMUM *) realloc (ppos, mpos * sizeof (EXTREMUM)))) errm (program);
			}
			for (k = 0; k < 3; k++) ppos[npos].x[k] = x[k];
			ppos[npos].v = t;
			ppos[npos].del2v = del2v;
			ppos[npos].weight = 1.0;
			ppos[npos].nvox = ppos[npos].killed = 0;
			npos++;
		}

		if (imgv[i] < 0.0
		&& del2v >= -ctneg
		&& imgv[i] < imgv[i - 1]	&& imgv[i] < imgv[i + 1]
		&& imgv[i] < imgv[i - nx]	&& imgv[i] < imgv[i + nx]
		&& imgv[i] < imgv[i - nx*ny]	&& imgv[i] < imgv[i + nx*ny]) {
			imgvalx_ (imgv, &nx, &ny, &nz, centerr, mmppixr, x, &t, &l);
			if (l < 1) {printf ("undefined imgvalx point\n"); continue;}
			if (t > vtneg) continue;
			if (mneg <= nneg) {
				mneg += MSIZE;
				if (!(pneg = (EXTREMUM *) realloc (pneg, mneg * sizeof (EXTREMUM)))) errm (program);
			}
			for (k = 0; k < 3; k++) pneg[nneg].x[k] = x[k];
			pneg[nneg].v = t;
			pneg[nneg].del2v = del2v;
			pneg[nneg].weight = 1.0;
			pneg[nneg].nvox = pneg[nneg].killed = 0;
			nneg++;
		}
	}}}
	printf ("\n"); fflush (stdout);
	printf ("before sorting npos=%d nneg=%d\n", npos, nneg); npos0 = npos; nneg0 = nneg;

	qsort ((void *) ppos, npos, sizeof (EXTREMUM), pcompare);
	qsort ((void *) pneg, nneg, sizeof (EXTREMUM), pcompare);

	printf ("positive peaks\n");
	ntot = 0;
	peaklist (stdout, ppos, npos, 0); npos1 = ntot;
	if (dthresh > 0.0) consolidate (ppos, npos);

	printf ("negative peaks\n");
	ntot = 0;
	peaklist (stdout, pneg, nneg, 0); nneg1 = ntot;
	if (dthresh > 0.0) consolidate (pneg, nneg);

	printf ("final peak list\n");
	ntot = 0;
	peaklist (stdout, ppos, npos, 0);
	peaklist (stdout, pneg, nneg, 0);

/********************************************/
/* combine positive and negative peak lists */
/********************************************/
	if (!(pall = (EXTREMUM *) malloc (ntot * sizeof (EXTREMUM)))) errm (program);
	nall = 0;
	for (npos2 = i = 0; i < ntop && i < npos; i++) if (!ppos[i].killed) {pall[nall++] = ppos[i]; npos2++;}
	for (nneg2 = i = 0; i < ntop && i < nneg; i++) if (!pneg[i].killed) {pall[nall++] = pneg[i]; nneg2++;}
	assert (ntot == nall);
	free (ppos); free (pneg);

	if (orad == 0.0) goto DONE;
/***************************/
/* create output ROI image */
/***************************/
		printf ("Reading: %s\n", imgfile);
		if (!(imgfp = fopen (imgfile, "rb")) || eread (imgv, dim, isbig,  imgfp)
		|| fclose (imgfp)) errr (program, imgfile);
	if (mask) {
		printf ("Reading: %s\n", mskfile);
		if (!(imgfp = fopen (mskfile, "rb")) || eread (imgm, dim, isbigm, imgfp)
		|| fclose (imgfp)) errr (program, mskfile);
	}

/****************************/
/* count voxels in each ROI */
/****************************/
	for (iz = 0; iz < imgifh.matrix_size[2]; iz++) {
	for (iy = 0; iy < imgifh.matrix_size[1]; iy++) {
	for (ix = 0; ix < imgifh.matrix_size[0]; ix++) {
		i = ix + nx*(iy + ny*iz);
		fndex[0] = (float) (ix + 1);
		fndex[1] = (float) (iy + 1);
		fndex[2] = (float) (iz + 1);
		for (k = 0; k < 3; k++) ptmp[0].x[k] =  fndex[k]*mmppixr[k] - centerr[k];
		dmin = 1.e6;
		for (j = 0; j < nall; j++) {
			t = (float) pdist (ptmp, pall + j);
			if (t < dmin) {
				jmin = j;
				dmin = t;
			}
		}
		k = !mask || (fabs (imgm[i]) > 1.e-37);
		if (k && (dmin < orad)) pall[jmin].nvox++;
	}}}

/*******************************/
/* apply voxel count criterion */
/*******************************/
	for (i = 0; i < ntot; i++) if (pall[i].nvox < Nvoxcrit) pall[i].killed = 1;
	if (!(pallN = (EXTREMUM *) malloc (ntot * sizeof (EXTREMUM)))) errm (program);
	for (nallN = i = 0; i < ntot; i++) if (!pall[i].killed) pallN[nallN++] = pall[i];
	free (pall);

	printf ("creating ROI slice");
	for (iz = 0; iz < imgifh.matrix_size[2]; iz++) {printf (" %d", iz + 1); fflush (stdout);
	for (iy = 0; iy < imgifh.matrix_size[1]; iy++) {
	for (ix = 0; ix < imgifh.matrix_size[0]; ix++) {
		i = ix + nx*(iy + ny*iz);
		imgr[i] = 0.0;
		fndex[0] = (float) (ix + 1);
		fndex[1] = (float) (iy + 1);
		fndex[2] = (float) (iz + 1);
		for (k = 0; k < 3; k++) ptmp[0].x[k] =  fndex[k]*mmppixr[k] - centerr[k];
		dmin = 1.e6;
		for (j = 0; j < nallN; j++) {
			t = (float) pdist (ptmp, pallN + j);
			if (t < dmin) {
				jmin = j;
				dmin = t;
			}
		}
		k = !mask || (fabs (imgm[i]) > 1.e-37);
		if (k && (dmin < orad)) imgr[i] = jmin + 2;
	}}}
	printf ("\n"); fflush (stdout);

	if (!(ptr = strrchr (imgroot, '/'))) ptr = imgroot; else ptr++;
	if (strlen (trailer)) {
		sprintf (outroot, "%s_ROI_%s", ptr, trailer);
	} else {
		sprintf (outroot, "%s_ROI", ptr);
	}
	sprintf (outfile, "%s.4dfp.img", outroot);
	printf ("Writing: %s\n", outfile);
	if (!(outfp = fopen (outfile, "wb")) || ewrite (imgr, dim, control, outfp)
	|| fclose (outfp)) errw (program, outfile);

/********************************************/
/* output ROI ifh and make analyze hdr file */
/********************************************/
	if (Writeifh (program, outfile, &imgifh, control)) errw (program, outroot);
	sprintf (outfile, "%s.4dfp.ifh", outroot);
	if (!(outfp = fopen (outfile, "a"))) errw (program, outfile);
	for (i = 0; i < nallN; i++) {
		fprintf (outfp, "region names\t:= %-5droi_%+03d_%+03d_%+03d%10d\n", i,
			nint (pallN[i].x[0]), nint (pallN[i].x[1]), nint (pallN[i].x[2]), pallN[i].nvox);
	}
	fclose (outfp);

/********************/
/* make analyze hdr */
/********************/
	sprintf (command, "ifh2hdr %s -r%d", outroot, nallN + 1);
	printf ("%s\n", command); status |= system (command);

/******************/
/* output ROI rec */
/******************/
	sprintf   (outfile, "%s.4dfp.img", outroot);
	startrece (outfile, argc, argv, rcsid, control);
	sprintf   (recfile, "%s.rec", outfile);
	if (!(outfp = fopen (recfile, "a"))) errw (program, recfile);
	fprintf (outfp, "Peak value     thresholds %10.4f to %10.4f\n", vtneg, vtpos);
	fprintf (outfp, "Peak curvature thresholds %10.4f to %10.4f\n", ctneg, ctpos);
	fprintf (outfp, "Peak counts before sorting pos=%d neg=%d\n", npos0, nneg0);
	fprintf (outfp, "Peak counts after  sorting pos=%d neg=%d\n", npos1, nneg1);
	fprintf (outfp, "Peak counts after %.4f mm threshold consolidation pos=%d neg=%d\n", dthresh, npos2, nneg2);
	ntot = 0; peaklist (outfp, pallN, nallN, 1);
	free (pallN);
	fprintf (outfp, "N.B.: indices count from 1 and include orientation specific 4dfptoanalyze flips\n");
	fclose (outfp);
	catrec (tmpfile);
	if (mask) catrec (mskfile);
	endrec ();
	sprintf (command, "brec %s -2", recfile); if (!quiet) system (command);

DONE:	free (imgv); free (imgr); free (imgm);
	exit (status);
}
Exemple #3
0
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);
}
Exemple #4
0
int main (int argc, char *argv[]) {
	FILE 		*imgfp, *datfp;
	char		imgroot[MAXL], imgfile[MAXL], gfcfile[MAXL], ifhfile[MAXL];
	char		outroot[MAXL], outfile[MAXL], datfile[MAXL], volfile[MAXL], recfile[MAXL];
	char		program[MAXL], string[MAXL],  command[MAXL], *ptr;
	int		isbig;
	char		control = '\0';

	IFH		ifh;
	UCHAR		ix, iy, iz;
	XYZN		xyzn;
	int		iv, iv0, jv, imgdim[3];
	int		ir, jr, nn;
	int		c, i, j, k, m, n;
	double		dela, delg, u1;
	float		vmin, vmax, fwhm = 0., alpha = ALPHA, *img_orig=NULL;
	float		maxfac = 0.0;

/**************/
/* gain field */
/**************/
	int		norder = NORDER, nterm = NTERM;
	int		niter = 0, limiter = LIMITER;
	int		eight = 8;		/* field length for call to powstring_ () */
	float		a[NTERM],		/* polynomial coefficients */
			a0[NTERM];
	float		drms, diffcrit = DIFFCRIT;
	float		u1gfc[MAXR];
	double		sum0, sum, q, t;

/*********/
/* flags */
/*********/
	int		test = 0;
	int		status = 0;
	int		debug = 0;
	int		gfreeze = 0;
	int		verbose = 0;
	int		negdef = 0;

	if (!(ptr = strrchr (argv[0], '/'))) ptr = argv[0]; else ptr++;
	strcpy (program, ptr);

/************************/
/* 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 'v': verbose++;		break;
			case 'n': negdef++;		break;
			case 'g': gfreeze++;		break;
			case '@': control = *ptr++;		*ptr = '\0'; break;
			case 'b': g_bandwidth	= atof (ptr);	*ptr = '\0'; break;
			case 'p': fwhm		= atof (ptr);	*ptr = '\0'; break;
			case 'e': diffcrit	= atof (ptr);	*ptr = '\0'; break;
			case 'i': g_sigma	= atof (ptr);	*ptr = '\0'; break;
			case 'l': limiter	= atoi (ptr);	*ptr = '\0'; break;
			case 'm': g_mr		= atoi (ptr);	*ptr = '\0'; break;
			case 's': g_sconst	= atof (ptr);	*ptr = '\0'; break;
			case 'z': g_zthresh	= atof (ptr);	*ptr = '\0'; break;
			case 'M': maxfac	= atof (ptr);	*ptr = '\0'; break;
			case 'r': getrange (ptr, &g_gfc_thresh, &g_gfc_ceil); *ptr = '\0'; break;
		}
	} else switch (k) {
		case 0: getroot (argv[i], imgroot); k++;  break;
	}
	if (k < 1) {
		fprintf (stderr, "Usage:\t%s <imgroot>\n", program);
		fprintf (stderr, " e.g.,\t%s vc1440_mpr_n4_111_t88.4dfp\n", program); /*??*/
		fprintf (stderr, "\toption\n");
		fprintf (stderr, "\t-g	freeze initial gain field\n");
		fprintf (stderr, "\t-n	force negative definite quadratic gain field\n");
		fprintf (stderr, "\t-v	verbose mode\n");
		fprintf (stderr, "\t-p<flt> pre-blur by specified FWHM in mm\n");
		fprintf (stderr, "\t-b<flt>\tspecify bandwidth in intensity units (default=%.1f)\n", BANDWIDTH);
		fprintf (stderr, "\t-e<flt>\tspecify drms convergence criterion (default=%f)\n", DIFFCRIT);
		fprintf (stderr, "\t-i<flt>\tspecify sigma (default=%f)\n", SIGMA);
		fprintf (stderr, "\t-l<int>\tspecify iteration limit (default=%d)\n", LIMITER);
		fprintf (stderr, "\t-m<flt>\tspecify gfc computation region count (default=%d)\n", MR);
		fprintf (stderr, "\t-s<flt>\tspecify space constant in mm (default=%f)\n", SCONST);
		fprintf (stderr, "\t-z<flt>\tspecify background threshold (default=%.1f)\n", ZTHRESH);
		fprintf (stderr, "\t-M<flt>\tspecify maximum correction factor\n");
		fprintf (stderr, "\t-r<flt>[to<flt>]\tspecify gfc range (default=%.1fto%.1f)\n", GFC_THRESH, GFC_CEIL);
		exit (1);
	}

/*******************************************/
/* read/initialize gain field coefficients */
/*******************************************/
	sprintf (gfcfile, "%s.4dfp.gfc", imgroot);
	if (imgfp = fopen (gfcfile, "r")) {
		printf ("Reading: %s\n", gfcfile);
		for (k = 0; k < nterm; k++) fscanf (imgfp, "%f", a + k);
		fclose (imgfp);
	} else {
		a[0] = 1.0;
		for (k = 1; k < nterm; k++) a[k] = 0.0;
	}
	for (k = 0; k < nterm; k++) fprintf (stdout, "%8.4f", a[k]); fprintf (stdout, "\n");

/******************************/
/* get input image dimensions */
/******************************/
	if (Getifh (imgroot, &ifh) != 0) errr (program, imgroot);
	isbig = strcmp (ifh.imagedata_byte_order, "littleendian");
	if (!control) control = (isbig) ? 'b' : 'l';

	for (k = 0; k < 3; k++) g_voxdim[k] = ifh.scaling_factor[k];
	if (debug) printf ("%10.6f%10.6f%10.6f\n", g_voxdim[0], g_voxdim[1], g_voxdim[2]);
	g_dimension = ((g_xdim = ifh.matrix_size[0])
		      *(g_ydim = ifh.matrix_size[1])
		      *(g_zdim = ifh.matrix_size[2]));
/************************************************/
/* check image index limits against XYZN typedef */
/*************************************************/
	if (g_xdim > MAXD || g_ydim > MAXD || g_zdim > MAXD) {
		fprintf (stderr, "%s: %s dimension exceeds %d\n", program, imgroot, MAXD);
		exit (-1);
	}
	if (ifh.number_of_bytes_per_pixel != 4) {
		printf ("%s: cannot process %d bytes per pixel", program, ifh.number_of_bytes_per_pixel);
		exit (-1);
	}

/************/
/* allocate */
/************/
	if (!(g_img1 = (float *) malloc (g_dimension * sizeof (float)))
	||  !(g_img0 = (float *) malloc (g_dimension * sizeof (float)))
	||  !(g_imgg = (float *) malloc (g_dimension * sizeof (float)))
	||  !(g_imgo = (short *) calloc (g_dimension,  sizeof (short)))
	||  !(g_imgb = (UCHAR *) calloc (g_dimension,  sizeof (UCHAR)))) errm (program);

	sprintf (imgfile, "%s.4dfp.img", imgroot);
	printf ("Reading: %s\n", imgfile);
	if (!(imgfp = fopen (imgfile, "rb")) || eread (g_img0, g_dimension, isbig, imgfp)
	|| fclose (imgfp)) errr (program, imgfile);

/******************/
/* pre-blur image */
/******************/
	if (!gfreeze && fwhm > 0) {
		if (!(img_orig = (float *) malloc (g_dimension * sizeof (float)))) errm (program);
/*************************************/
/* img_orig will hold original image */
/*************************************/
		memcpy (img_orig, g_img0, g_dimension*sizeof(float));
		memcpy (g_img1,   g_img0, g_dimension*sizeof(float));
		printf ("blur FWHM = %.2f mm\n", fwhm);
		imgdim[0] = g_xdim; imgdim[1] = g_ydim; imgdim[2] = g_zdim;
		imgblur3d_ (&fwhm, &alpha, g_voxdim, g_img1, imgdim, g_img0);	/* g_img1 returned unusable */
/************************************************************************/
/* g_img0 now points to blurred image to be used in further computation */
/************************************************************************/
	}

/*******************/
/* compute outroot */
/*******************/
	if (ptr = strrchr (imgroot, '/')) ptr++; else ptr = imgroot;
	getroot (ptr, outroot);

/*********************/
/* apply initial gfc */
/*********************/
	evalgain3d_ (g_imgg, &g_xdim, &g_ydim, &g_zdim, &norder, a);
	for (sum = sum0 = i = 0; i < g_dimension; i++) {
		sum0 += g_img0[i];
		sum  += (g_img1[i] = g_img0[i]/g_imgg[i]);
	}
	for (i = 0; i < g_dimension; i++) g_img1[i] *= (sum0/sum);

	if (gfreeze) goto WRITE;

/****************/
/* open datfile */
/****************/
	sprintf (datfile, "%s_gfc.dat", outroot);
	if (!(datfp = fopen (datfile, "a"))) errw (program, datfile);
	powstring_ (&norder, &eight, string);
	fprintf (datfp, "#\t%s\n", string);
	fprintf (datfp, "%d\t", niter);
	for (k = 0; k < nterm; k++) fprintf (datfp, "%8.4f", a[k]);
	fprintf (datfp, "%8.4f\n", 0.0); fflush (datfp);

/***********************/
/* clear region buffer */
/***********************/
	memset (&g_region, '\0', MAXR * sizeof (REGION));
	iv0 = ir = 0;

/***************************/
/* find contiguous regions */
/***************************/
CONTIG:	while (ir < MAXR) {
		for (iv = iv0; iv < g_dimension; iv++) if (!g_imgo[iv]) break;
		if (iv == g_dimension) break;
		iv0 = iv;

		g_imgo[iv] = ir + 1;
		g_region[ir].u1 = (floor (g_img1[iv] / g_bandwidth) + 0.5) * g_bandwidth;
		k = iv;
		j = g_dimension;
		j /= g_zdim; iz = k / j; k -= iz * j;
		j /= g_ydim; iy = k / j; k -= iy * j;
		j /= g_xdim; ix = k / j; k -= ix * j;
		xyzn.ix = ix; xyzn.iy = iy; xyzn.iz = iz; xyzn.nn = 0;
		assign (ir, xyzn);

		do {
			m = g_region[ir].ne++;
			iv = g_region[ir].xyzn[m].ix + g_xdim*(g_region[ir].xyzn[m].iy + g_ydim*g_region[ir].xyzn[m].iz);
			for (j = 0; j < 6; j++) {
				xyzn = g_region[ir].xyzn[m];
				switch (j) {
				case 0:	k =  -1;	if (xyzn.ix < 1)		continue; xyzn.ix--; break;
				case 1:	k *= -1;	if (xyzn.ix > g_xdim - 2)	continue; xyzn.ix++; break;
				case 2:	k *= -g_xdim;	if (xyzn.iy < 1)		continue; xyzn.iy--; break;
				case 3:	k *= -1;	if (xyzn.iy > g_ydim - 2)	continue; xyzn.iy++; break;
				case 4:	k *= -g_ydim;	if (xyzn.iz < 1)		continue; xyzn.iz--; break;
				case 5:	k *= -1;	if (xyzn.iz > g_zdim - 2)	continue; xyzn.iz++; break;
				}
				jv = iv + k;
				if (g_imgo[jv] || g_img0[jv] < g_zthresh) continue;
				dela = g_img1[jv] - g_region[ir].u1;
				if (fabs (dela) < 0.5 * g_bandwidth) {
					g_imgo[jv] = ir + 1;
					assign (ir, xyzn);
				}
			}
			if (verbose && !(g_region[ir].count % 1000)) {
				xyzn = g_region[ir].xyzn[m];
				printf ("checkr0: region=%4d unexa=%8d coords=%4d%4d%4d u1=%8.2f\n",
					ir, g_region[ir].count - m, xyzn.ix, xyzn.iy, xyzn.iz, g_region[ir].u1);
			}
		} while (g_region[ir].count > g_region[ir].ne);

		if (g_region[ir].count == 1) {
			iv = g_region[ir].xyzn[0].ix + g_xdim*(g_region[ir].xyzn[0].iy + g_ydim*g_region[ir].xyzn[0].iz);
			g_imgo[iv] = -1;
			g_region[ir].count = g_region[ir].ne = 0;
		} else {
			ir++;
		}
	}

/************************/
/* sort regions by size */
/************************/
	qsort ((void *) g_region, ir, sizeof (REGION), rcompare);
	if (ir == MAXR) {
		if (verbose) printf ("ir %d -> %d iv0=%10d ivmax=%d\n", ir, MAXR/2, iv0, g_dimension);
		while (--ir > MAXR/2) {
			for (m = 0; m < g_region[ir].count; m++) {
				xyzn = g_region[ir].xyzn[m]; iv = xyzn.ix + g_xdim*(xyzn.iy + g_ydim*xyzn.iz);
				g_imgo[iv] = 0;
			}
			g_region[ir].count = g_region[ir].ne = 0;
		}
		goto CONTIG;
	}
	g_nr = ir;
	for (ir = g_nr; ir < MAXR; ir++) if (g_region[ir].xyzn) free (g_region[ir].xyzn);

	test = 1;
ITER:	getmean ();	/* analyze g_img1 */
	getxyzn0 ();	/* analyze g_img1 */
/********************/
/* reassign regions */
/********************/
	for (i = 0; i < g_dimension; i++) g_imgo[i] = 0;
	printf ("total number of regions %d\n", g_nr);
	for (ir = 0; ir < g_nr; ir++) {
		g_region[ir].ne = g_region[ir].count = 0;
		if (g_region[ir].u1 < g_gfc_thresh || g_region[ir].u1 > g_gfc_ceil) continue;
		assign (ir, g_region[ir].xyzn0);
		do {
			m = g_region[ir].ne++;
			xyzn = g_region[ir].xyzn[m];
			iv = xyzn.ix + g_xdim*(xyzn.iy + g_ydim*xyzn.iz);
			for (j = 0; j < 6; j++) {
				xyzn = g_region[ir].xyzn[m];
				switch (j) {
				case 0:	k =  -1;	if (xyzn.ix < 1)		continue; xyzn.ix--; break;
				case 1:	k *= -1;	if (xyzn.ix > g_xdim - 2)	continue; xyzn.ix++; break;
				case 2:	k *= -g_xdim;	if (xyzn.iy < 1)		continue; xyzn.iy--; break;
				case 3:	k *= -1;	if (xyzn.iy > g_ydim - 2)	continue; xyzn.iy++; break;
				case 4:	k *= -g_ydim;	if (xyzn.iz < 1)		continue; xyzn.iz--; break;
				case 5:	k *= -1;	if (xyzn.iz > g_zdim - 2)	continue; xyzn.iz++; break;
				}
				jv = iv + k;
				if (g_imgo[jv] || g_img0[jv] < g_zthresh) continue;
				dela = g_img1[jv] - g_region[ir].u1;
				delg = g_sconst * (g_img1[jv] - g_img1[iv]) / g_voxdim[j/2];
				if (sqrt (dela*dela + delg*delg) < g_sigma*g_bandwidth) {
					g_imgo[jv] = ir + 1;
					assign (ir, xyzn);
				}
			}
			if (verbose && !(g_region[ir].count % 1000)) {
				xyzn = g_region[ir].xyzn[m];
				printf ("checkr1: region=%4d unexa=%8d coords=%4d%4d%4d u1=%8.2f\n",
				ir, g_region[ir].count - m, xyzn.ix, xyzn.iy, xyzn.iz, g_region[ir].u1);
			}
		} while (g_region[ir].count > g_region[ir].ne);
	}
	getmean (); getxyzn0 ();	/* analyze g_img1 */
	listreg (stdout);
	if (g_region[0].u1 < g_bandwidth) {
		fprintf (stderr, "%s: algorithmic failure\n", program);
		exit (-1);
	}	

	if (++niter > limiter || !test) goto COUNT;
	fprintf (stdout, "%s: iteration %d\n", program, niter);
/***************/
/* compute gfc */
/***************/
	for (ir = 0; ir < g_nr; ir++) u1gfc[ir] = g_region[ir].u1;
	for (k = 0; k < nterm; k++) a0[k] = a[k];
	fitgain3dd_ (g_img0, &g_xdim, &g_ydim, &g_zdim, g_imgo, u1gfc, &g_mr, &norder, a, &drms);
	if (negdef) fnegdef_ (a);
	printf ("niter=%d drms=%.6f diffcrit=%.6f\n", niter, drms, diffcrit);
	fprintf (datfp, "%d\t", niter);
	for (k = 0; k < nterm; k++) fprintf (datfp, "%8.4f", a[k]);
	fprintf (datfp, "%8.4f\n", 100*drms); fflush (datfp);
	evalgain3d_ (g_imgg, &g_xdim, &g_ydim, &g_zdim, &norder, a);
	for (sum = i = 0; i < g_dimension; i++) sum += (g_img1[i] = g_img0[i]/g_imgg[i]);
	for (      i = 0; i < g_dimension; i++) g_img1[i] *= (sum0/sum);
/********************/
/* convergence test */
/********************/
	test = (drms > diffcrit);
	goto ITER;

COUNT:	fclose (datfp);
/*******************/
/* count neighbors */
/*******************/
	for (ir = 0; ir < g_nr; ir++) {
		for (g_nl = m = 0; m < g_region[ir].count; m++) {
			ix = g_region[ir].xyzn[m].ix;
			iy = g_region[ir].xyzn[m].iy;
			iz = g_region[ir].xyzn[m].iz;
			iv = ix + g_xdim*(iy + g_ydim*iz);
			nn = 0;
			k =  -1;	if (ix > 0)	     {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);}
			k *= -1;	if (ix < g_xdim - 1) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);}
			k *= -g_xdim;	if (iy > 0)	     {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);}
			k *= -1;	if (iy < g_ydim - 1) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);}
			k *= -g_ydim;	if (iz > 0)	     {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);}
			k *= -1;	if (iz < g_zdim - 1) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);}
			if (nn > g_region[ir].nnmax) g_region[ir].nnmax = nn;
			g_imgb[iv] = g_region[ir].xyzn[m].nn = nn;
		}
		if (verbose) {
			printf ("%5d%10d%10d%5d%2d |", ir, g_region[ir].count, k, j, g_region[ir].nnmax);
			for (i = 0; i < g_nl; i++) printf (" %d", g_list[i]); printf ("\n");
		}
	}

/*********************************/
/* write gain field coefficients */
/*********************************/
	if (!(imgfp = fopen (gfcfile, "w"))) errw (program, gfcfile);
	printf ("Writing: %s\n", gfcfile);
	for (k = 0; k < nterm; k++) fprintf (imgfp, "%8.4f", a[k]); fprintf (imgfp, "\n");
	fclose (imgfp);

/*************************/
/* write corrected image */
/*************************/
WRITE:	for (sum0 = sum = i = 0; i < g_dimension; i++) {
		if (!gfreeze && fwhm > 0) g_img0[i] = img_orig[i];
		sum0 += g_img0[i];
		q = 1./g_imgg[i];
		t = (maxfac > 0.0) ? 2.*maxfac*tanh(.5*q/maxfac) : q;
		if (0) printf ("%10.6f %10.6f\n", q, t);
		sum  += (g_img1[i] = g_img0[i]*t);
	}
	for (i = 0; i < g_dimension; i++) g_img1[i] *= (sum0/sum);

	vmin = FLT_MAX; vmax = -vmin;
	for (i = 0; i < g_dimension; i++) {
		if (g_img1[i] < vmin) vmin = g_img1[i];
		if (g_img1[i] > vmax) vmax = g_img1[i];
	}
	sprintf (outfile, "%s_gfc.4dfp.img", outroot);
	fprintf (stdout, "Writing: %s\n", outfile);
	if (!(imgfp = fopen (outfile, "wb")) || ewrite (g_img1, g_dimension, control, imgfp)
	|| fclose (imgfp)) errw (program, outfile);
/***************/
/* ifh and hdr */
/***************/
	sprintf (ifhfile, "%s_gfc.4dfp.ifh", outroot);
	if (Writeifh (program, ifhfile, &ifh, control)) errw (program, ifhfile);
	sprintf (command, "ifh2hdr %s -r%.0f", ifhfile, vmax);
	system  (command);
/*******/
/* rec */
/*******/
	startrece (outfile, argc, argv, g_rcsid, control);
	sprintf (string, "%s gain field coefficients\n", imgroot);	printrec (string);
	powstring_ (&norder, &eight, string);				printrec (string);
	for (k = 0; k < nterm; k++) {sprintf (string, "%8.4f", a[k]);	printrec (string);} 	printrec ("\n");
	if (!gfreeze) {
		sprintf (recfile, "%s_gfc.4dfp.img.rec", outroot);
		if (!(imgfp = fopen (recfile, "a"))) errw (program, recfile); listreg (imgfp); fclose (imgfp);
	}
	catrec (imgfile);
	endrec ();
	if (gfreeze) goto FREE;

/*************************/
/* compute regions image */
/*************************/
	for (i = 0; i < g_dimension; i++) g_img1[i] = 0;
	for (ir = 0; ir < g_mr; ir++) {
		for (m = 0; m < g_region[ir].count; m++) {
			xyzn = g_region[ir].xyzn[m];
			iv = xyzn.ix + g_xdim*(xyzn.iy + g_ydim*xyzn.iz);
			g_img1[iv] = g_region[ir].u1;
		}
	}

/******************************/
/* write processing QA images */
/******************************/
	sprintf (volfile, "%s_gfc_vols.4dfp.img", outroot);
	printf ("Writing: %s\n", volfile);
	if (!(imgfp = fopen (volfile, "w"))) errw (program, volfile);
/*****************/
/* regions image */
/*****************/
	if (ewrite (g_img1, g_dimension, control, imgfp)) errw (program, volfile);
/****************/
/* border image */
/****************/
	for (i = 0; i < g_dimension; i++) g_img1[i] = g_imgb[i];
	if (ewrite (g_img1, g_dimension, control, imgfp)) errw (program, volfile);
/**************/
/* gain field */
/**************/
	for (i = 0; i < g_dimension; i++) g_img1[i] = g_imgg[i];
	if (ewrite (g_img1, g_dimension, control, imgfp)) errw (program, volfile);
	fclose (imgfp);
/***************/
/* ifh and hdr */
/***************/
	sprintf (ifhfile, "%s_gfc_vols.4dfp.ifh", outroot);
	ifh.matrix_size[3] = 3;
	if (Writeifh (program, ifhfile, &ifh, control)) errw (program, ifhfile);
	sprintf (string, "ifh2hdr %s -r%.0f", ifhfile, vmax);
	system  (string);
/*******/
/* rec */
/*******/
	startrece (volfile, argc, argv, g_rcsid, control);
	sprintf (string, "%s_gfc QA (3 volumes)\n", imgroot); printrec (string);
	catrec (outfile);
	printrec ("Volume 1: regions\nVolume 2: border\nVolume 3: gain field\n");
	endrec ();

/************/
/* clean up */
/************/
FREE:	free (g_img1); free (g_img0); free (g_imgg);
	free (g_imgo); free (g_imgb);
	if (img_orig) free (img_orig);
	for (ir = 0; ir < g_nr; ir++) free (g_region[ir].xyzn);
	exit (status);
}
Exemple #5
0
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);
}
Exemple #6
0
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);
}
Exemple #7
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);
}
Exemple #8
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);
}