/* include statements */

#include <stdio.h>		/* standard i/o routines */
#include <fcntl.h>		/* other standard routines */
#include <stdlib.h>		/* more lattice c standard i/o routines */
/*#include <tardev.h>		/* targa header files */
#include <math.h>		/* some math functions needed */
/*#include <time.h>		/* some time functions needed */
#include <dos.h>		/* used for NO9 init call */
#include <ctype.h>		/* used for some character testing */
#include <memory.h>		/* used for some memory allocation routines */
#include <direct.h>		/* used for some directory routines */
#include <search.h>		/* used for some search and sort routines */
#include <string.h>		/* string functions */
#include <io.h>			/* used for mktemp call */
#include "df.h"			/* header file with defines for HDF */

#ifndef LATTICE
typedef unsigned char byte;
#endif
typedef unsigned long ulong;

#include "externs.h"	/* declarations of the variable types of routines in ImProVise */

/* define statements */
#define MOUSE					/* define this variable if a mouse is to be used with this program */
#define TRUE			1
#define FALSE			0
#define HI				2
#define LO				3
#define	SCRLLEFT		203		/* the values returned from my getchar() routines for keys on the keypad */
#define	SCRLUP			200
#define	SCRLDOWN		208
#define	SCRLRIGHT		205
#define	PAGEUP			201
#define	PAGEDOWN		209
#define	HOME			199
#define	END				207
#define ALTSCRLUP		152
#define ALTSCRLDOWN		160
#define ALTSCRLLEFT		155
#define ALTSCRLRIGHT	157	
#define ALTPAGEUP		153
#define ALTPAGEDOWN		161
#define ALTHOME			151
#define ALTEND			159
#define NO9			'9'			/* attempting to make references to which mode we are in easier */
#define	VGA			'v'
#define	EGA			'e'
#define	TARGA		't'
#define HDF			'h'
#define RASTER		'b'
#define TIFF		't'
#define SDS			's'
#define TEMPINFO	(*temp_list).wind_info
#define SQR(x)		x*x
#define PI			3.1415926535898
#define LEFTARROW	0			/* actual ascii codes for these character names in the font */
#define RIGHTARROW	1
#define CLOSEBOX	2
#define DOWNARROW	3
#define	UPARROW		4
#define	RESIZE		5
#define	FILLTEXT	6
#define	OPENFULLY	7
#define LEFTEDGE	8
#define	VERTBAR		16
#define	HORIBAR		24
#define SUPERN 		30
#define HIDE_BORDER 31
#define ENTER		13
#define ESCAPE		27
#define SPACE		32
#define DASH		10
#define RAWSIZE 32000	/* maximum size of the block to be read in from disk */
#ifdef QAK
#define LINEEGA		egaline(xwhere,ywhere+i,store[i+yoff],xoff,max(min(xdim-xoff,maxx-xwhere),0)) /*,trans)*/
	/* call to print a line on the ega screen */
#define LINEVGA	   vgaline1(xwhere,ywhere+i,store[i+yoff],xoff,max(min(maxx-xwhere,xdim-xoff),0))
	/* call to print a line on the vga screen */
#define LINENO9	   no9line(xwhere,ywhere+i,store[i+yoff],xoff,max(min(maxx-xwhere,xdim-xoff),0))
	/* call to print a line on the no. 9 screen */
/*#define LINET16	   t16line(xwhere,ywhere+i,store[i+yoff],xoff,max(min(maxx-xwhere,xdim-xoff),0))*/
	/* call to print a line on the Targa16 screen */
#endif

/* regular variables defined */

int	i,j,k,l,c,			/* temporary global counters and such */
	xdim,ydim,			/* x & y dimensions for the image to be displayed */
	file,				/* the file pointer for binary files */
	pal_window=-1,		/* index to the window which contains the palette */
	pal_hid=0,			/* whether the palette is hidden or not */
	maxx=640,maxy=350,	/* the x & y dimensions of the default EGA screen */
	lastref,			/* the last reference number used in palette searching */
	lasttag,			/* the last tag number used in searching for palettes */
	newref,				/* reference number for a new palette */
	imref,				/* the current image's reference number */
	imtag,				/* the current image's tag number */
	animate=0,			/* the animation toggle */
	one_file=0,			/* the one file animation toggle */
	first_hdf=1,		/* boolean to flag the first time through an hdf animation */
	oneimage=0,			/* boolean variable to indicate whether there are multiple binary images to be displayed */
	leftbutton,			/* boolean variables for buttons on the mouse */
	rightbutton,
	mx,my,				/* last position where the mouse was at */
	anispeed=0,			/* speed for animation */
	doneinit=0,			/* boolean variable to keep track of whether the palette has been initialized for ega mode */
	getout=0,			/* boolean variable to exit program */
	pal_height=16,		/* height in bytes of the color bar on the screen */
	expandit=1,			/* preference variable to do: contours (2), expand(1) or interpolate(0) */ 
	contour_num=10,		/* number of contour levels for contour plot */
	pal_mode=0,			/* which palette mode you are performing: drag & place (1), or rotate(0) */
	window_num,			/* variable to keep track of the window open */
	first_window=1,		/* boolean variable to keep track of the first window opened */
	first_palette=1,	/* boolean to keep track of the first palette input */
	font_width,			/* the width in pixels of a font character */
	font_height,		/* the height in poixels of a font character */
	font_space,			/* the amount of bytes a font character takes up */
	file_err,			/* the error returned from file operations */
	file_attr,			/* the file attribute to look for */
	clear_color=0,		/* the color to clear to */
	def_drive=4,		/* variable to set the default drive  A=0, B=1, ...*/
	do_anim=0,			/* boolean whether animations are operating in the background */
	top_image=-1,		/* window number of the top image in the window list */
	contour_levels[26],	/* an array of contour levels for the picture */
	*hide_num;			/* pointer to the length of the hide palette text */

unsigned int m1,m2,m3,m4;		/* mouse variables */

double def_xmag=1.0,	/* the default x magnification */
	def_ymag=1.0;		/* the defualt y magnification */

/*unsigned short int l16[512];	/* array for the targa line */

char *p,				/* temporary character storage */
	mode=EGA,			/* the display mode we are in = EGA,VGA,NO9,TARGA */
	filetype=HDF;		/* the type of file to be read = 'h,'b' */

byte rmap[256],gmap[256],bmap[256],	/* the red, green, and blue bytes for 24 bit palettes */
			*charset[128],						/* pointers to the font data for windows */
			filename[128],						/* name of binary image file */
			*scrap_name,						/* the name of the scrapbook data */
			**hide_name,						/* handle of the hide palette pointer */
			def_path[64]="\\improvis",			/* the default path to find improvise in */
			dial_path[64],						/* the path to the dialog box directory */
			canvas_path[64],					/* the path to the canvas directory */
			palfile[128] = {0,0},				/* starts out = NULL */
			trans[256]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},			/* translation table for palette in ega mode */
			pal[768],							/* for writing the palette out */
			count[64],							/* the colors to choose from in ega mode */
			regrs[16];							/* the actual palette for ega mode */

extern byte *DTAPTR;		/* pointer to a DTA containing filenames to be found (located in dosio.asm) */

FILE	*fp,	/* file pointer for binary image files */
		*pfp;	/* file pointer for binary palette files */

/* number nine variables */
char *NO9ram;
int *NO9bank;		 /* bank control registers */

typedef struct pentry {		/* define a palette entry format */
	byte red,
		green,
		blue;
  }pal_entry;

typedef struct pnode {		/* define a node structure for a palette linked list */
	pal_entry	orig[256],		/* the original, unmodified palette */
		updt[256],				/* the palette changes from the last palette mode */
		mod[256];				/* the changed palette */
	byte trans[256];			/* a copy of the translation table for ega palettes */
	byte ega_regs[16];			/* a copy of the ega registers */
	byte pal_name[128];			/* the file name of the palette */
	int	cond;					/* the condition of the palette, (0) - unmodified, (1) - modified */
	struct pnode *next_node;	/* a pointer to the next node in the palette list */
  }palette;

palette *head_pal,*curr_pal;		/* pointer to the head of the palette list and a pointer to the current palette being used */

struct over_list {	/* structure for a list of things to overlays on a window */
	int	over_num,					/* the number of the overlay */
		type,						/* what type of overlay this is (0 - line, 1 - grid, 2 - circle, 3 - rectangle, 4 - point, 5 - ellipse, 6 - window grid, 7 - image grid) */
		x0,y0,						/* location of the upper left hand corner of the overlay */
		x1,y1,						/* location of the lower right hand corner of the overlay */
		rad,						/* the radius of the circle */
		xtype,ytype,				/* what type of grid spacing in the x and y directions (0 - linear, 1 - log)*/
		xspace,yspace;				/* spacing (in pixels) between lines of a grid */
	struct over_list *next_node;	/* pointer to the next button */
  };

struct button_list {	/* structure for a list of button (in a window?) */
	int x0,y0,						/* location of the upper left hand corner of the button */
		x1,y1,						/* location of the lower right hand corner of the button */
		num;						/* the button number */
	struct button_list *next_node;	/* pointer to the next button */
  }**menu_button;

struct menu_list {	/* structure for the list of menu functions available */
	byte *menustr;			/* pointer to a string for the menu function */
	byte char_choice;		/* character which corresponds to the menu choice */
	int		level_num;				/* a code for the level of the menu this function is at */
	int		command_num;			/* a code for the command number on a level */
	unsigned int	data_length;	/* the length of the string */
	struct menu_list *next_node,	/* pointer to the next node on this level */
		*prev_node;					/* pointer to the previous node on this level */
	struct menu_list *up_node,		/* pointer to the next level up */
		*down_node;					/* pointer to the next level down */
  }*head_menu;

struct data_list {	/* structure for a list of data blocks for a window */
	byte *dataptr;			/* pointer to a block of image */
	int		block_num;				/* the block number in the list of blocks */
	int		hi_lite;				/* boolean to determine if text should be high lighted on the screen */
	unsigned int data_height,		/* the number of lines in this block */
		data_length;				/* the length (in bytes) of a line in this block */
	struct data_list *next_node;	/* pointer to the next node in the list */
  }*head_data,*scrap_data;
  
struct info {	/* window information structure */
	int	x0,y0,					/* the location of the window on the screen */
		print_width,			/* the width of the screen which can have data in it */
		print_height,			/* the height of the screen which can have data in it */
		print_x0,				/* the actual screen location to print data at in the x direction */
		print_y0,				/* the actual screen location to print data at in the y direction */
		xwhere,ywhere,			/* where in the window data is being shown */
		save_width,				/* the width of the saved window data */
		save_height,			/* the height of the saved window data */
		border,					/* whether this window has a border around it */
		x_width,y_height;		/* the width and height of the window */
	byte hidden,		/* boolean variable whether the window is hidden or not */
		text_type,				/* whether the text in the window should be printed horizontally ('h') or vertically ('v') */
		wind_id,				/* the window id character */
		info1,					/* a character which has info about the window */
		info2;					/* another character for info about the window */
  };
  
struct wind_list {
	struct info wind_info;			/* information about the window */
	struct data_list *wind_data;	/* a list of window data */
	struct over_list *wind_over;	/* a list of overlays for the window */
	palette *wind_pal;				/* pointer to the palette for this window */
	int	wind_num;					/* the window number */
	byte *wind_name,		/* the name of the window */
		data[3];					/* a 3 character wide data field for info about the window */
	struct wind_list *next_node,	/* pointer to the next node in the linked list */
		*prev_node;					/* pointer to the previous node in the linked list */
  }*head_list, *tail_list;

struct anim_list {
	char *name;						/* the name of the file */
	int	is_hdf_file,				/* whether a file is hdf or not */
		imtag,imref,				/* the tag and ref numbers for hdf files */
		width,height,				/* the x and y dimensions of the image */
		count;						/* the number of this file in the sequence of animations */
	struct anim_list *next_node,	/* the pointer to the next node in the linked list */
		*prev_node;					/* pointer to the previous node in the list */
  }*head_anim;

struct anim_queue {					/* a linked list of animations */
	char *name;						/* the name of the animation */
	int active,						/* whether to service this animation list */
		dir,						/* whether we are going forward (1) or backward (0) in the list */
		anim_num;					/* the window of the animation */
	struct anim_list *first_node,	/* the pointer to the first node in the animation */
		*curr_node;					/* pointer to the current node in the list */
	struct anim_queue *next_node;	/* pointer to the next animation list to service */
  }*head_queue;

struct mouse_list {				/* a linked list of bad mouse positions */
	int x0,y0,						/* coor. of a bad area for the mouse */
		x1,y1;
	struct mouse_list *next_node;	/* pointer to the next animation list to service */
  }*head_mouse;

struct err_code {					/* an error code structure for better windows() error reporting */
	int	error,where;
  }err_type;

/* HDF stuff */
DF *dff;			/* HDF file descriptor */
DFdesc ddstr;		/* HDF structure for the tag & ref of something */

/* arrays and stuff for unrleit */
byte *save, *savestart=NULL, *saveend=NULL;
			/* save has a list of decompressed bytes not returned in
				previous call.  savestart and saveend specify the position
				at which this list starts and ends in the array save */

extern void vgaline1(),vgapt(),outlinev(),inversev(),vgablock(),line13(),invert13(),circle13(),incirc13();	/* vga routines */
extern void no9line(),no9pt(),outline9(),inverse9(),no9block(),lineno9(),invert9(),circle9(),incirc9();		/* no9 routines */
extern void egaline(),egapt(),outlinee(),inversee(),egablock(),line10(),invert10(),circle10(),incirc10();		/* ega routines */

void (*drawline)(),(*drawpt)(),(*outline)(),(*inverse)(),(*drawblock)(),(*invert_line)(),(*b_line)(),(*invert_ellipse)(),(*draw_ellipse)();	/* function pointers for drawing lines, points, etc. */

byte *wind_message="window_num";
