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


#include	
#include	

/*
	tv - extract true values from comtal images

	carl crawford
	purdue university
	w. lafayette, in 47907

	may 5, 1981


	syntax: tv [options]

	VARIABLES:
	if:	input file name					pic.r
	of:	output file name				pic.v
	n:	size of picture					128
	m:	size of window 					3

	Changes:
		Added 'c' command to save a comment.
					Malcolm   12/7/81
*/

FILE	*ifd;		/* input file descriptor */
int	opin;		/* open input file */
FILE	*ofd;		/* output file descriptor */
int	opout;		/* open output file */
FILE	*tc;		/* for tracking cross device */
char	ifn[25] = "pic.r";	/* name of input file */
char	ofn[25] = "pic.v";	/* name of output file */
int	n =	128;	/* size of picture */
int	m =	3;	/* size of window */
float	line[512];	/* line of pictue */
char	buf[512];	/* buf for various purposes */
char	ibuf[80];	/* input line buffer */
char	linesave[80];	/* Input buffer for comment command */
int	x,y;		/* track cross coordinates */
int	px,py;		/* pixel coordinates */
int	ep;		/* 512 / n */
int	p;		/* m = 2p + 1 */
int	gr;		/* grid indicator */
int	drawflag;		/* grid drawn flag */
char	*argp[20];	/* pointer to words */
int	argn;		/* word count */
int	fnw;		/* file name written into save file */
int	asave;		/* auto save */
float	av;		/* picture average */

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

main(argc,argv)
	int	argc;
	char	**argv;
{
	char	cc;
	register i,j,k;

	while(argv++ , --argc){
		if(**argv=='-' && argv[0][1])while(cc= *++*argv)switch(cc){
			default:
				fprintf(stderr,"bad flag: %c\n",cc);
				exit(1);
		}else switch(comm(*argv)){
			case 1:		/* if */
				strcpy(ifn,*argv+3);
				break;
			case 2:		/* of */
				strcpy(ofn,*argv+3);
				break;
			case 3:		/* n */
				setn(*argv + 2);
				break;
			case 4:		/* m */
				setm(argv + 2);
				break;
		}
	}
	in(ifn);
	if(!p)p = m>>1;
	if(!ep)ep = 512 / n;
	
	while( fputs("> ",stdout),fgets(ibuf,80,stdin) != NULL){
		strcpy(linesave,ibuf);	/* Save line before parsing. */
		words();
		if(!argn){
			ave();
		}else{
			switch(**argp){

			case 'g':	/* grid command */
				grid();
				break;
			case '!':	/* outside command */
				cmd();
				break;
			case 'p':	/* print info */
				pr();
				break;
			case 's':	/* save info */
				save();
				break;
			case 'h':	/* help */
				help();
				break;
			case 'x':	/* exit */
			case 'q':
				exit(0);
			case 'n':
				if(--argn)setn(argp[1]);
				break;
			case 'm':
				if(--argn)setm(argp[1]);
				break;
			case 'i':	/*input file */
				if(--argn){
					in(argp[1]);
				}else{
					printf("need file name\n");
				}
				break;
			case 'a':	/* auto save */
				asave = (asave + 1) & 1;
				printf("auto-save: %s\n",(asave)?"on":"off");
				break;
			case 'c':	/* Save a comment */
				if (opout)
					fprintf(opout,"Comment> %s\n",
						(argp[1] - ibuf) + linesave);
				break;
			default:
				fprintf(stderr,"unknown command\n");
				break;
			}
		}
	}
}
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);
}

err(s1,s2)
	char	*s1,*s2;
{
	fprintf(stderr,"%s%s\n",s1,s2);
}

ave()
{
	int	is,js,jf,i,k,l;

	if(!opin){
		err("no open input file","");
		return;
	}
	if((tc = fopen("/dev/ct/tc","r")) == NULL){
		err("can't open: ","/dev/ct/tc");
		return;
	}
	if(fread(buf,sizeof(*buf),512,tc) != 512){
		err("track cross read error","");
		return;
	}
	fclose(tc);
	x = (buf[0] & 0377) | (buf[1] << 8);
	y = (buf[2] & 0377) | (buf[3] << 8);
	px = x / ep;
	py = y / ep;

	is = max(py-p,0);
	fseek(ifd,(long)(is*n*sizeof(*line)),0);
	is = min(py+p,n-1)+1-is;
	js = max(px-p,0);
	jf = min(px+p,n-1);
	for(i=av=k=0;k 512){
		err("bad picture size","");
		return;
	}
	k = 0;
	j = n;
	while(!(j & 1)){
		k++;
		j >>= 1;
	}
	if(1< 20){
		err("bad window size","");
		return;
	}
	if( !(m & 1))err("window size must be odd","");
	p = m >> 1;
}

in(s)
	char *s;
{
	if(*s >= '0' && *s <= '9'){
		strcpy(buf,"pic.r.");
		strcat(buf,s);
	}else{
		strcpy(buf,s);
	}
	strcpy(ifn,buf);
	if(opin){
		fclose(ifd);
		opin = 0;
	}
	if((ifd = fopen(buf,"r")) == NULL){
		err("can't open: ",buf);
		strcpy(ifn,"NULL");
	}else{
		opin = 1;
		fnw = 0;
	}
}

help()
{
	printf(":\taverage\n");
	printf("g:\tgrid on/off\n");
	printf("!:\trun shell\n");
	printf("p:\tprint parameters\n");
	printf("s:\tsave average\n");
	printf("x:\texit\n");
	printf("n:\tset n\n");
	printf("m:\tset m\n");
	printf("i:\tinput file\n");
	printf("a:\ttoggle auto save\n");
	printf("c:\twrite a comment into file\n");
}

min(i,j)
{
	return((i>j)?j : i);
}


max(i,j)
{
	return((i>j)?i : j);
}