
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	
	defines boxes for use in image definitions

	BOX number [NC=] [NR=] [CC=] [CR=] [SR=] [SC=] [INT] [DEL] [help]
	
	if no args specified makes a list of boxes

	9/22/92 - call to exit(0) -rrt
			- removed screen puts
*/

#include <stdio.h>
#include <stdlib.h>
#include "pcvista.h"
#include "pcvimage.h"

#include <unistd.h>

#ifdef PROTO
int main(int, char **);
static void boxlist(int);
static int make_newbox(void);
#else
static void boxlist();
static int make_newbox();
#endif

int show_flag, delete_flag, int_flag;

char *progname = "box";
char *usage = "box number [nc=] [nr=] [cc=] [cr=] [sr=] [sc=] [int] [del] [help]";

int 
main(argc, argv)
int argc;
char *argv[];
{
	static int i, nr, nc, sr, sc, cr, cc, sr_flag, sc_flag, boxno = -1;
	char *gotit, buf[BIG_BUF];

	if ((argc == 2) && (strcmp(argv[1], "help")) == 0) {
		printf("%s\n", usage);
		exit(0);
	}

	if (argc == 1) {
		boxlist(boxno);
		exit(0);
	}
	nr = -1;
	nc = -1;
	sr = -1;
	sc = -1;
	cr = -1;
	cc = -1;
	sr_flag = 0;
	sc_flag = 0;

	for(i = 1; i < argc; i++) {		/* extract new values from command line */
		if ((gotit = find("nc", argv[i])) != NULL) {
			nc = (int)(evaluate(gotit) + .5);
			continue;
		}
		if ((gotit = find("nr", argv[i])) != NULL) {
			nr = (int)(evaluate(gotit) + .5);
			continue;
		}
		if ((gotit = find("sr", argv[i])) != NULL) {
			sr = (int)(evaluate(gotit) + .5);
			sr_flag = 1;
			continue;
		}
		if ((gotit = find("sc", argv[i])) != NULL) {
			sc = (int)(evaluate(gotit) + .5);
			sc_flag = 1;
			continue;
		}
		if ((gotit = find("cc", argv[i])) != NULL) {
			cc = (int)(evaluate(gotit) + .5);
			continue;
		}
		if ((gotit = find("cr", argv[i])) != NULL) {
			cr = (int)(evaluate(gotit) + .5);
			continue;
		}
		if (keyword("show", argv[i])) {
			show_flag = 1;
			continue;
		}
		if (keyword("del", argv[i])) {
			delete_flag = 1;
			continue;
		}
		if (keyword("int", argv[i])) {
			if (boxno == -1)
				boxno = make_newbox();
			int_flag = 1;
			continue;
		}
		if (i == 1) {
			boxno = (int)(evaluate(argv[1]) + 0.5);		
			if (boxno >= NBOX)
				error(-1, "maximum number of boxes exceeded");
			if (getbox(boxno, &sr, &sc, &nr, &nc) == 0) {
				cr = sr + nr/2;
				cc = sc + nc/2;
			}
			continue;
		}		
		error(-1,"invalid option");
	}
	if (delete_flag) {
		if (boxno != -1) {
			sprintf(buf, "box%d=", boxno);
			putsym(buf);
			exit(0);
		}
		else
			error(-1, "no box specified");
	}

	if (show_flag || int_flag) { 
		image_init(TV_GREYSCALE);
	}
		
	if (int_flag)
		int_box(&sr, &sc, &nr, &nc);
	else {
		if ((cc > 0) && (sc_flag == 0))	 /* convert centered values to start values */
			sc = cc - nc/2;
		if (sc < 0)
			error(1, "error - starting column less than zero");
		if ((cr > 0) && (sr_flag == 0))
			sr = cr - nr/2;
		if (sr < 0)
			error(1, "error - starting row less than zero");
		if (nr < 1)
			error(1, "error - number of rows less than one");
		if (nc < 1)
			error(1, "error - number of columns less than one");
	}
	if (boxno != -1) {
		sprintf(buf,"box%d=%d %d %d %d", boxno, sr, sc, nr, nc);
		putsym(buf);
	}
	boxlist(boxno);
	exit(0);
}

static void 
boxlist(boxno)
int boxno;
{
	int box, nr, nc, sr, sc, boxflag;
	char buf[NBUF];

	if (boxno == -1) {
		for (box = 0, boxflag = 0; box < NBOX; box++) { 
			if (getbox(box, &sr, &sc, &nr, &nc) == 0) {
				sprintf(buf,"box #%d cr=%d cc=%d nr=%d nc=%d",
					box, sr + nr/2, sc + nc/2, nr, nc);
				if (show_flag) {
					puts(buf);
					show_box(sr, sc, sr+nr, sc+nc);
				}
				else
					puts(buf);
				boxflag = 1;
			}
		}
		if (boxflag == 0)
			puts("no boxes found");
	}
	else {
		if (getbox(boxno, &sr, &sc, &nr, &nc))
			error(-1, "program error");
		printf("box #%d cr=%d cc=%d nr=%d nc=%d\n",
			boxno, sr + nr/2, sc + nc/2, nr, nc);
		if ((show_flag) || (int_flag)) {
			show_box(sr, sc, sr + nr, sc + nc);
		}
	}
}


static int 
make_newbox()			/* finds first unused box number */
{
	int i, sc, sr, nr, nc;

	for (i = 0; i < NBOX; i++)
		if (getbox(i, &sr, &sc, &nr, &nc))
			return(i);
	return(-1);
}

