www.pudn.com > 医学算法.rar > scan.c


#include	
#include	

/*
	Scan - Massage raw data from the CRC scanner

	Malcolm Slaney
	Purdue University
	W. Lafayette, IN 47907

	Jan. 22, 1982


	syntax: scan [options]

	VARIABLES:						Default
	if:	input file name					t
	of:	output file name				udata
	n:	# of rays per projection			100
	k:	# of projections				1

	FLAGS:
	t:	Process 16 bit time of flight data		on
	l:	Process 32 bit C-Scan Data			off
	c:	Process packed(compressed) waveform data	off
	w:	Process unpacked waveform data			off
	q:	Process Quaduature data (5 bytes/ray)		off
	f:	Process floating point data			off

	r:	Output raw data (do not post process)		off
	s:	Do not swap alternate projections		off
	a:	Do no adjust data for slant			off
	p:	Don't print program parameters

*/

#define	RECSIZE	2048			/* Default Record Size */

#define	TOF	0
#define	FLOAT	1
#define	QUAD	2
#define	WAVE	3
#define	PACKED	4
#define	LONG	5

char	*ifn = "t";	/* name of input file */
char	*ofn = "udata";	/* name of output file */
int	n =	100;	/* Number of rays per projection */
int	k = 	1;	/* Number of Projections */
int	length = 1900;	/* Length of data in Waveform Mode */
int	mode = TOF;	/* Input data mode */

int	doswap = 1;	/* Swap projections */
int	doadjust = 1;	/* Adjust projections for slant */
int	noprint;	/* Do not print program parameters */

char	buf[2*RECSIZE];	/* Input buffer */
float	proj[RECSIZE];	/* Projection Data */

FILE	*input;		/* Input File Descriptor */
FILE	*output;	/* Output File Descriptor */

int	i,j,l,m;	/* Play variables */
float	gain, t;
float	findgain();
char	*p;
char	*progname;

char	*table[] = {
		"if",
		"of",
		"n",
		"k",
		"length",
		0
	};

main(argc,argv)
	int	argc;
	char	**argv;
{
	char	cc;

	progname = argv[0];
	while(argv++ , --argc){
		if(**argv=='-' && argv[0][1])while(cc= *++*argv)switch(cc){
			case 'c':
				mode = PACKED;
				doadjust = 0;
				doswap = 0;
				break;
			case 'q':
				mode = QUAD;
				break;
			case 't':
				mode = TOF;
				break;
			case 'f':
				mode = FLOAT;
				break;
			case 'w':
				mode = WAVE;
				doadjust = 0;
				doswap = 0;
				break;
			case 'l':
				mode = LONG;
				break;
			case 'r':
				doadjust = doswap = 0 ;
				break;
			case 's':
				doswap = 0;
				break;
			case 'a':
				doadjust = 0;
				break;
			case 'p':
				noprint = 1;
				break;
			default:
				fprintf(stderr,"bad flag: %c\n",cc);
				exit(1);
		}else switch(comm(*argv)){
			case 1:		/* if */
				ifn = *argv+3;
				break;
			case 2:		/* of */
				ofn = *argv+3;
				break;
			case 3:		/* n */
				n = atoi(*argv + 2);
				break;
			case 4:		/* k */
				k = atoi(*argv + 2);
				break;
			case 5:		/* length */
				length = atoi(*argv + 7);
				break;
		}
	}
	
	input = fopen(ifn,"r");
	if (!input){
		fprintf(stderr,"%s: Can't open %s for input.\n",progname,
			ifn);
		exit(2);
	}

	output = fopen(ofn,"w");
	if (!output){
		fprintf(stderr,"%s: Can't open %s for output.\n",progname,
			ofn);
		exit(2);
	}

	if(!noprint){
		printf("SCAN DATA\n\n");
		printf("n: %d\n",n);
		printf("k: %d\n",k);
		printf("of: %s\n",ofn);
		printf("if: %s\n",ifn);
		printf("Input Data:");
		switch(mode){
		case TOF:
			printf("Time of Flight (16 bit integers)\n");
			printf("	Swap projections: %s\n",
				doswap?"yes": "no");
			printf("	Adjust projections: %s\n",
				doadjust?"yes":"no");
			break;
		case LONG:
			printf("Long Integers (32 bit integers)\n");
			printf("	Swap projections: %s\n",
				doswap?"yes": "no");
			printf("	Adjust projections: %s\n",
				doadjust?"yes":"no");
			break;
		case QUAD:
			printf("Quaduature, (Real,Imaginary,Gain)\n");
			printf("	Swap projections: %s\n",
				doswap?"yes": "no");
			break;
		case FLOAT:
			printf("Floating Point Rays\n");
			printf("	Swap projections: %s\n",
				doswap?"yes": "no");
			printf("	Adjust projections: %s\n",
				doadjust?"yes":"no");
			break;
		case WAVE:
			printf("Waveform Mode (2048 byte records)\n");
			printf("	%d valid points\n",length);
			break;
		case PACKED:
			printf("Packed Waveform Mode (256 bytes per waveform)\n");
			break;
		}
	}
	for (i=0;i32768)
					 l = -l;
				proj[j] = l;
			}
			procray(proj,i,n);
			fwrite(proj,sizeof(float),n,output);
			break;
		case LONG:		/* 32 bit data per ray */
			fread(buf,RECSIZE,1,input);
			for (j=0;j 127)
						l = -l;
					proj[m] = l * gain;
				}
				fwrite(proj,sizeof(float),length,output);
			}
			break;
		case QUAD:		/* Five bytes per ray */
			fread(buf,RECSIZE,1,input);
			for (j=0, p=buf; j 32767)
					l = -l;
				proj[2*j] = l * gain;
				l = (p[2] << 8) + (p[3] & 0377);
				if (l > 32767)
					l = -l;
				proj[2*j+1] = l * gain;
			}
			procray(proj,i,2*n);
			if (doswap && (i & 1)){
				for (j=0;j127)
						t = -t;
					fwrite(&t,sizeof(t),1,output);
				}
				l --;
			}
			break;
		}
	}
	if (i != k)
		fprintf(stderr,"%s: Warning only %d (of %d) projections read.\n"
			,progname, i, k);
}
comm(s)
	char	*s;
{
	register	int	i,j,r;

	for(i=0;table[i];i++){
		for(j=0;(r=table[i][j]) == s[j] && r;j++);
		if(r == 0 && s[j] == '=' && s[j+1] )return(i+1);
	}
	fprintf(stderr,"bad option: %s\n",s);
	exit(1);
}

procray(proj,i,n)
float	*proj;
int	i,n;{
	if (doadjust)
		slant(proj,n);
	
	if (doswap && (i&1))
		swap(proj,n);
}
		
slant(proj,n)
float	proj[];
int	n;
{
	float   ave,avb;
	register int     i;
	register float	 *p;

	avb = (proj[4] + proj[5]) * 0.5;
	ave = (proj[n-6] + proj[n-5]) * 0.5;
	ave = (ave - avb) / n;
	avb -= ave * 4.0;
	for(i=0,p=proj;i>1;
	for(p=proj,q=proj+n-1,i=0;i>2) & 037;
	if (!j)
		t = pow(10.0,j/20.0);
	else
		t = 1.0;
	t = 1.0/t;
	t *= biogain[i & 07];
	return(t);
}