#include <stdio.h>
#include <sys/file.h>
#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include <suntool/gfxsw.h>
#include <sunwindow/rect.h>
#include <sgtty.h>			/* ioctl stuff */
#include <sys/ioctl.h>
#include <memory.h>

#define STDIN		0
#define SCREEN_X        1152		/* Max screen dimensions */
#define SCREEN_Y        900


int	xdim,ydim;			/* X and Y dimensions of image*/
int	xsize,ysize;			/* size after expansion */
int	stepthrough	=FALSE;		/* step through images */
int	expand		=FALSE;		/* should be expanded to fit screen?*/
int	xfact		=1;		/* expansion factors*/
int	yfact		=1;
unsigned char *storage,*malloc();	/* area to store image */

/************************************************************************
 *									*
 * Title: wseq (window sequencer)		Version: 1.0 beta	*
 * Author: Dave Thompson			Date: 10/19/87		*
 *									*
 * Description:								* 
 *      wseq or 'window sequencer' is a program that allows a user to   *
 * view data images on his sun screen in a window environment.  wseq is	*
 * set up to support displaying images on a Sun console while in the	*
 * suntools environment.						*
 *									*
 * Usage:								*
 *  wseq <xdimension> <ydimension> <palet file> [-s] <image> [image...] *
 *									*
 * To compile:								*
 *	 cc wseq.c -O -o wseq -lsuntool -lsunwindow -lpixrect		*
 *									*
 * Please send bugs/fixes to:						*
 *									*
 *			davet@newton.ncsa.uiuc.edu			*
 *			{ihnp4,convex,pur-ee}!uiucdcs!uxc!newton!davet  *
 *									*
 * (c) 1987  The Board of Trustees of the University of Illinois        *
 *          "Unpublished-- All Rights Reserved Under the Copyright Laws"*
 ************************************************************************/


main(argc,argv)
int argc;
char	*argv[];
{
struct	gfxsubwindow	*gfx;		/* Base window handle */
struct	rect		rect;		/* Holds the dimensions of the win */
char	palfile[80];			/* name of color pallet file */
int	fd;				/* file descriptor of images*/
int	image_index;			/* index of image file names */
int	startx,starty;			/* start pos in window of image */
struct  sgttyb          cook_tstate;    /* Original STDIN terminal state */
struct  sgttyb          raw_tstate;     /* raw & no echo terminal state */
struct  pixrect *temp_pr;
struct  mpr_data *mpr;
char	c;				/* used to wait for keyboard input */


	/* Check Arguments */
	if (argc < 4) {
		printf("Usage: %s xdim ydim palet [-s] file\n",argv[0]);
		printf(" -s	step through images\n");
		exit(1);
		}

	/* Process Arguments */
	xsize = xdim = atoi(argv[1]);
	ysize = ydim = atoi(argv[2]);
	strcpy(palfile,argv[3]);
	image_index = 4;		/* start of image file names in argv[]*/

	if (strncmp(argv[4],"-s",2) == 0) {
		stepthrough = TRUE;
		if (argc < 5) {
			printf("Usage: %s xdim ydim palet [-s] file\n",argv[0]);
	                printf(" -s     step through images\n"); 
	                exit(1);
			}
		image_index++;
		}
	else
		stepthrough = FALSE;


	/* Get info on base window. */
	gfx = gfxsw_init(0,(char *)0);


	/* Set STDIN to CBREAK (rare) mode */
        ioctl(STDIN,TIOCGETP,&cook_tstate);
        ioctl(STDIN,TIOCGETP,&raw_tstate);
        raw_tstate.sg_flags = raw_tstate.sg_flags | CBREAK;
        ioctl(STDIN,TIOCSETP,&raw_tstate);


	storage = malloc(xdim * ydim);


	/* load in pallet */
	paload(gfx->gfx_pixwin,palfile);

	/* display image */
	while (image_index < argc) {
		/* get window size and store in rect */
        	win_getsize(gfx->gfx_windowfd, &rect);

		/* expand image to fit window */
		if (expand) {
			if(1 < (xfact = rect.r_width / xdim))
				xsize = xdim * xfact;
			else {
				xfact = 1;
				xsize = xdim;
				}
			if(1 < (yfact = rect.r_height / ydim))
				ysize = ysize * yfact;
			else {
				yfact = 1;
				ysize = ydim;
				}
			} /*if*/

		/* center image in window */
		startx = (xsize < rect.r_width) ? (rect.r_width-xsize)/2 :0;
		starty = (ysize < rect.r_height) ? (rect.r_height-ysize)/2 :0;


	        if (0 > ( fd = open(argv[image_index],O_RDONLY))) {
			printf("error opening %s \n",argv[image_index]);
			exit(1); 
			}
		if ((temp_pr=mem_create(xsize,ysize,8)) == NULL) {
			printf("display:mem_create error\n");
			exit(1);
			}
		mpr = mpr_d(temp_pr);
 

		if (( xfact > 1) || ( yfact > 1 )) {
			read_image(fd,storage,xdim,ydim);
			printf("about to call bigimg\n");
			bigimg(mpr->md_image,storage);
			printf("returned from bigimg\n");
			}
		else
		        read_image(fd,mpr->md_image,xdim,ydim);


		/* Display it */
	        pw_write(gfx->gfx_pixwin,startx,starty,xsize,ysize,PIX_SRC,temp_pr,0,0);
	        pr_close(temp_pr);
		close(fd);
		image_index++;
		
		/* check for damage to window and fix it (such as resize)*/
		notify_dispatch();

		if (stepthrough) 
			c = getchar();

		} /* while */


	/* wait here before exiting */
	c = getchar();

        /* Set STDIN to no echo and make it raw */
        ioctl(STDIN,TIOCSETP,&cook_tstate);


} /* main() */



int read_image(fd,i,xdim,ydim)
int fd;
char *i;
int xdim,ydim;
{
        return (read(fd,i,( xdim*ydim)));
}


paload(pw,palfile)
/* Load the palet */
Pixwin *pw;
char *palfile;
{
        unsigned char rmap[256],bmap[256],gmap[256],*s;
        FILE *fp;
        register i;
 
        /*****panel_set_value(pal_board, palfile);*/
 
        if((fp=fopen(palfile, "r")) == NULL) {
           printf("paload: can't open %s\r\n", palfile);
           return;
        }
 
        for(i=0;i<256;i++) {
           fread(rmap,1,256,fp);
           fread(gmap,1,256,fp);
           fread(bmap,1,256,fp);
        }
        fclose(fp);
 
/*        rmap[255] = gmap[255] = bmap[255] = 0;*/
/*        rmap[0] = gmap[0] = bmap[0] = 255; */
 
        pw_setcmsname(pw,"toolmap");
        pw_putcolormap(pw,0,256,rmap,gmap,bmap);
        return;
}


/*****************************************************************************/
/* expandimg
*  copy an image memory to memory, expanding byte by byte to get a larger image.*  no aliasing, just byte replication
*  This subroutine by Tim Krauskopf
*/
bigimg(targ,src)
        unsigned char *targ,*src;
        {
        register i,j,line;
        register unsigned char *p,*q,*oldq;
 
        for (line = 0; line < ydim; line++) {
                p = src+line*xdim;
                oldq = q = targ+line*xsize*xfact;
 
                for (i=0; i<xdim; i++,p++)
                        for (j=0; j<xfact; j++)
                                *q++ = *p;
        
                for (i=0; i<yfact ; i++) {
                        memcpy(q,oldq,xsize);                           /* make
one copy of the line */
                        q += xsize;
                }
 
        }
}


