/*
TYPE = PIE_CHART
DATA_TYPES = 4
	Gravel   7  red
	Sand     9  yellow
	Silt    10  green
	Clay    11  blue
SYMBOL_SIZE = 18
  -1  -1  -1  -1  -1  -1  95  97  99   1   3   5  -1  -1  -1  -1  -1  -1
  -1  -1  -1  -1  91  93  95  97  99   1   3   5   7   9  -1  -1  -1  -1
  -1  -1  -1  89  90  92  94  96  99   1   4   6   8  10  11  -1  -1  -1
  -1  -1  86  87  89  91  93  96  99   1   4   7   9  11  13  14  -1  -1
  -1  84  85  86  87  89  92  95  98   2   5   8  11  13  14  15  16  -1
  -1  82  83  84  86  87  90  94  98   2   6  10  13  14  16  17  18  -1
  80  80  81  82  83  85  87  91  97   3   9  13  15  17  18  19  20  20
  78  78  79  79  80  81  84  87  95   5  13  16  19  20  21  21  22  22
  76  76  76  76  77  77  78  80  87  13  20  22  23  23  24  24  24  24
  74  74  74  74  73  73  72  70  62  38  30  28  27  27  26  26  26  26
  72  72  71  71  70  69  66  62  55  45  38  34  31  30  29  29  28  28
  70  70  69  68  67  65  62  59  53  47  41  38  35  33  32  31  30  30
  -1  68  67  66  64  62  60  56  52  48  44  40  38  36  34  33  32  -1
  -1  66  65  64  62  61  58  55  52  48  45  42  39  38  36  35  34  -1
  -1  -1  64  62  61  59  57  54  51  49  46  43  41  39  38  36  -1  -1
  -1  -1  -1  61  60  58  56  54  51  49  46  44  42  40  39  -1  -1  -1
  -1  -1  -1  -1  59  57  55  53  51  49  47  45  43  41  -1  -1  -1  -1
  -1  -1  -1  -1  -1  -1  55  53  51  49  47  45  -1  -1  -1  -1  -1  -1
END
9B       42.3616667  -87.7283333   0.00 84.80 11.36  3.84

----------------------------------------------------------------------

TYPE = RECTANGLE+TEXT   ['*' does not plot]
PIXEL_RANGE = 1.0 0.0 km
COLORS = 2
  0 = black
  9 = yellow
TEXT_COLOR = 0
TEXT_SIZE  = 2
SYMBOL_ROWS = 17   HOT_SPOT  8
SYMBOL_COLS = 35   HOT_SPOT 17
BEGIN_SYMBOL
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
END
42015    42.143293     -80.069364    text_lat_lon_comment
42014    40.872097     -79.788539    Comment_all_in_1_string


*/
#include <stdio.h>
#include <string.h>
#include "mouseg.h"
#include <malloc.h>

#define MENUXC 550
#define MAX 50
#define S_SIZE 40
#define MAX_COLORS 16
#define STR_LEN 100
#define NUM_TYPE 25
#define NUM_SIZE 25
#define NUM_PSIZE 3
#define NUM_FILL  5
#define NUM_SYM  25
#define MAX_X  640
#define B_LINES 48
#define SYM_SIZE 11
#define YELLOW    9
#define RED       7
#define MAXDO 1000
#define MIN_PIX  0.000

extern int Mouse;
extern float PixScale;
extern int Rxc,Ryc,Repeat;		/* multiple screens if Repeat>1 */
struct menu Menu_Mouse2[8]=
{
	{
		"System",' ',1,
		6,
		"Zoom",'Z',1,
		"Del Mark",-1,1,
		"Attrib ON",-2,1,
		"Attrib OFF",-3,1,
		"Hide Menu",-4,1,
		"Del Outline",'R',1
	},
	{
		"Locate",'L',1,
		0
	},
	{
		"Label",' ',1,
		9,
		"Lines vsml",4,1,
		"Lines sml",3,1,
		"Lines med",2,1,
		"Lines larg",1,1,
		"  Icons->",'p',1,
		"<-Icons",'t',1,
		"Mark Points",'M',1,
		"All Colors",'N',1,
		"One Color",'n',1
	},
	{
		"Color",' ',1,
		5,
		"Edit",'E',1,
		"Flash",'F',1,
		"Darker",'D',1,
		"Brighter",'B',1,
		"Paint",'P',1
	},
	{
		"Key",' ',1,
		2,
		"Small",'k',1,
		"Large",'K',1
	},
	{
		"Plot",' ',1,
		6,
		"Lines",'V',1,
		"Graphs",'G',1,
		"Patterns",'a',1,
		"Names",'m',0,
		"Icons",'T',1,
		"Pictures",'U',1,
		"Text",'q',1
	},
	{
		"Exit",'X',1,
		0
	},
	{
		"*"
	}
};
struct menu Menu_MouseZ[7]=
{
	{
		"Move Box",'m',1,
		0
	},
	{
		"Larger",'l',1,
		0
	},
	{
		"Smaller",'s',1,
		0
	},
	{
		"Status",'S',1,
		0
	},
	{
		"Quit Zoom",27,1,
		0
	},
	{
		"Exit & Plot",'X',1,
		0
	},
	{
		"*"
	}
};
extern char LastMap[STR_LEN],LastFile[STR_LEN],Auto;
extern int NumPicture,PictureOn[NUM_SYM];
extern char String[STR_LEN];
extern int todo[2][MAXDO];
extern FILE *FpLab,*FpGrd;
extern float BackBright;
extern int AttribOn;
struct map_screen
{
	int xc,yc,xs,ys;
};
extern struct map_screen map_pos;
struct map_input
{
	char *name,type;
	double indat[6];
};
extern struct map_input miv;
struct symbol_name
{
	char name[50],filename[50];
	float max_pix,min_pix;
	int color;
	char on;
	char Symbol[SYM_SIZE][SYM_SIZE];
};
extern struct symbol_name PlaceFile[NUM_SYM];
extern char *Menu_V[NUM_TYPE+3];
extern unsigned char MT_Buff[12][MAX_X],Mbuff[B_LINES][MAX_X],Buffer[MAX_X];
extern int MaxXs,MaxYs;
extern char VideoType;
extern FILE *Fpout;

struct   Color 
{ 
	unsigned char  r, g, b; 
};
struct file_id
{
	char name[S_SIZE];
	long size;
	unsigned date,time;
};
extern struct file_id GF_File[50];
struct map
{
	int KeyPlotted;
	char Fill_Name[256][20];
	int Fill_In;
	int Fill_After;
	int Fill_Color[256][2];
	int Max_Fill;
	int Fill_Color_RGB[256][3];
	float Fill_Color_IHS[256][3];
	int Fill_Order[256];
	int Comp_Color[256];
	int Num_Fill;
	float FI_min[NUM_FILL],FI_max[NUM_FILL];
	char Fill_In_Name[NUM_FILL][STR_LEN];
	int White,Num_Color;
	int screen_xs,screen_ys;
	int Num_Type,Out;
	float Pixsize,Clat,Clon,Space;
	int Mback,Text,High,Back,Grat;
	int GratOn;
	int LatLon,Black;
	char Color_Name[MAX_COLORS][30];
	int Color_Val[MAX_COLORS][3];
	int Type;
	int Ns[NUM_TYPE];
	int Mm[NUM_TYPE];
	int NumSymbol;
	int Range;
	int Pattern;
	char Group_Name[NUM_TYPE][30];
	struct line_name
	{
		char filename[54];
		float max_pix,min_pix;
		int color;
	}MapFile[NUM_TYPE][NUM_SIZE];
	struct place_name
	{
		char *name,*filename;
		float max_pix,min_pix;
		int color;
	}PlaceFile[3][3];	/* size = 18*3*3 = 162 bytes */
	struct Color lut[256];
};
int *G[MAX];
extern struct map M;
extern int ScreenXs,ScreenYs;
extern int Font[128][25];
extern int FontSize;
struct pattern
{
	int status;
	char pattern[8][8];
};
extern struct pattern Pat[240];

char fsplit(char *,char *,char *,char *);
FILE *open_to_read_text(char *);
FILE *open_to_read_binary(char *);
int pix_locate_help(int lsize,char *string);
int pixel_key(int ans);
int pixel_label_all(int lsize,int screen_xs);
int pixel_label_vector(int *x,int *y,int *speed,int *v_num,int size);
char mouse_Zoom(int xc,int yc,int xs,int ys,
			float *pixsize,float *clat,float *clon,float *space);
multiple_maps(int xc,int yc,int xs,int ys,float pixsize,
		float clat,float clon,float space);
FILE *open_to_read_binary(char *);
FILE *open_to_read_text(char *);
FILE *open_to_write_text(char *);
FILE *open_to_write_binary(char *);


/********************************************************************
**
**	locate a point
**
********************************************************************* */

void pix_loc(fx,fy,px,py,sp)

int fx,fy;			/* starting point of cursor */
int *px,*py,*sp;		/* pointers to final location */

{
	int i,j,l,m,p;
	int ans,ans1,size=0,speed,x,y,xo,yo,k,screen_xs=ScreenXs;
	static unsigned char tbuff[200];
	int lsize=FontSize,val,ival,val1,val2;
	char string[100],pth[STR_LEN],nam[10],typ[10],str1[20],str2[50];
	int idc[2],clr,num;
	double lat,lon;
	FILE *fpmark;
	double dx,dy;
	int ix,iy,color=M.White,txs,v_num=0;
	int cursize=15/FontSize,labelsize=FontSize;
	float pixsize=M.Pixsize,space,clat,clon;
	long lxs,lys,buffersize=(long)B_LINES*(long)MAX_X;
	int left,right,row=ScreenYs/2,col=ScreenXs/2,rowo,colo,num_times,ro,co;
	int mrow=5,mcol=640;
	int xc=0,yc=0,xs=ScreenXs,ys=ScreenYs;

	if(VideoType=='S')
		lsize=3;
	ScreenXs=MaxXs;
	lxs=MaxXs;
	lys=32/lsize;
	if(lxs*lys>buffersize)
	{
		printf("Box 1 too large in 'pix_loc()'\n");
		printf("buffersize = %ld  lxs = %ld  lys = %ld\n",buffersize,lxs,lys);
		exit(0);
	}
	if(lxs*lys>buffersize)
	{
		printf("Box 2 too large in 'pix_loc()'\n");
		exit(0);
	}
	if(Mouse==0)
	{
		speed=*sp;
		size=0;
		xo=x=fx;
		yo=y=fy;
		tcursor(0,x,y,5,tbuff);
		lxs=ScreenXs;
		lys=32/lsize;
		save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
		while((ans=getpad(&size,&x,&y,&speed,0))!=27&&ans!=13)
		{
			if(ans=='h'||ans=='H'||ans==59)		/* F1 */
			{
				txs=ScreenXs;
				pix_locate_help(lsize,string);
				screen_xs=txs;
			}
			uncursor(0,xo,yo,5,tbuff);
			val=getpt(0,x,y)-16;
			color=M.Comp_Color[val+16];
			tcursor(0,x,y,5,tbuff);
			cursor(0,x,y,color,5);
			dx=xo=x;
			dy=yo=y;
			if(ans=='G')
			{
				uncursor(0,xo,yo,5,tbuff);
				unsave_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				graph_plot();
				save_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				tcursor(0,x,y,5,tbuff);
				cursor(0,x,y,color,5);
			}
			if(ans=='a')
				make_pattern(0,ScreenXs-1);
			if(ans=='L')
			{
				unsave_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				uncursor(0,x,y,5,tbuff);
				pixel_label_vector(&x,&y,&speed,&v_num,7);
				save_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				tcursor(0,x,y,5,tbuff);
			}
			xy_ll(dx,dy,&lat,&lon);
			if(ans=='l')
			{
				for(i=0;i<M.NumSymbol;i++)
				{
					if(PlaceFile[i].on==1&&
						pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
					{
						symbol_plot(i,1);
					}
				}
			}
			if(ans=='a')
			{
				if(AttribOn==0)
					AttribOn=1;
				else
					AttribOn=0;
			}
			if(ans=='i')
			{
				for(i=0;i<M.NumSymbol;i++)
				{
					if(PlaceFile[i].on==1&&
						pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
					{
						symbol_plot(i,2);
					}
				}
			}
			if(ans=='m')
			{
				cursor(0,x,y,M.White,5);
				tcursor(0,x,y,5,tbuff);
				cursor(0,x,y,M.White,5);
				if(M.Out==1&&Fpout)
					fprintf(Fpout,"%10.6lf %11.6lf\n",lat,lon);
			}
			if(ans=='M')
			{
				cursor(0,x,y,M.White,5);
				tcursor(0,x,y,5,tbuff);
				cursor(0,x,y,M.White,5);
				k=0;
				while((string[k++]=getch())!=13);
				string[k-1]='\0';
				fclose(fpmark);
				fpmark=fopen("mapper.mrk","at");
				if(fpmark)
					fprintf(fpmark,"%s %10.6lf %11.6lf %s\n",string,lat,lon,string);
			}
			paint_box(0,M.Back,MT_Buff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			if(val>0)
			{
				sprintf(string,"%3d %s %9.6lf %11.6lf",val,
					M.Fill_Name[val],lat,lon);
				if(strlen(string)>65)
				{
					printf("string too long -- strlen(string) = %d\n",
						strlen(string));
					exit(0);
				}
			}
			else
			{
				sprintf(string,"        %9.6lf %11.6lf",lat,lon);
				if(strlen(string)>65)
				{
					printf("string too long -- strlen(string) = %d\n",
						strlen(string));
					exit(0);
				}
			}
			plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
			if(ans=='n'&&(VideoType=='X'||VideoType=='S'||VideoType=='Y'))
			{
				uncursor(0,xo,yo,5,tbuff);
				if(val>=0&&strlen(M.Fill_Name[val])>0)
					plot_font_h(color,x-strlen(M.Fill_Name[val])*15/lsize/2,
						y-13/lsize,M.Fill_Name[val],lsize,Font);
				tcursor(0,x,y,5,tbuff);
				cursor(0,x,y,color,5);
				xo=x;yo=y;
			}
			if(ans=='D'&&(BackBright>0.11))
			{
				BackBright-=0.10;
				set_back();
			}
			if(ans=='B'&&(BackBright<2.0))
			{
				BackBright+=0.10;
				set_back();
			}
			if(ans=='P')
			{
				unsave_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				uncursor(0,xo,yo,5,tbuff);
				i=0;
				while((string[i++]=getch())!=13);
				string[i]='\0';
				sscanf(string,"%d",&clr);	
				paint_area(clr,xo,yo);
				tcursor(0,xo,yo,5,tbuff);
				save_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				cursor(0,xo,yo,color,5);
			}
			if(ans=='p'&&(VideoType=='X'||VideoType=='S'||VideoType=='Y'))
			{
				uncursor(0,xo,yo,5,tbuff);
				clr=getpt(0,xo,yo)-16;
				dx=xo;dy=yo;
				xy_ll(dx,dy,&lat,&lon);
				if(FpLab)
					fprintf(FpLab,"   0     0  %9.6lf %11.6lf %3d %3d\n",
						lat,lon,clr,clr);
				tcursor(0,xo,yo,5,tbuff);
				cursor(0,xo,yo,color,5);
			}
			if(ans=='f'||ans=='F')
				pixel_flash(val);
			if(ans=='E'&&val>0) /* edit colors */
			{
				unsave_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				pixel_color_edit(lsize);
				save_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			}
			if(ans=='C')	/* print data in 'mapper.out' */
				set_lut('c',0);
			if(ans=='A')
			{
				for(i=0;i<ScreenYs;i++)
				{
					for(j=0;j<ScreenXs;j++)
					{
						if((i+j)%2==0)
							MT_Buff[0][j]=(j*16)/ScreenXs;
						else
							MT_Buff[0][j]=(i*16)/ScreenYs;
					}
					plotrow(0,0,ScreenXs-1,i,MT_Buff[0]);
				}
			}
			if((ans=='k'||ans=='K')&&M.Fill_In>0)
				screen_xs=pixel_key(ans);
			if(ans=='S'&&M.NumSymbol>0)
			{
				if(search_points(&x,&y,M.Pixsize,string)>0)
				{
					paint_box(0,M.Back,MT_Buff,
							0,ScreenYs-32/lsize,ScreenXs,32/lsize);
					uncursor(0,xo,yo,5,tbuff);
					val=getpt(0,x,y)-16;
					color=M.Comp_Color[val+16];
					tcursor(0,x,y,5,tbuff);
					cursor(0,x,y,color,5);
					xo=x;yo=y;
					plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
				}
			}
							/* look at pictures or text */
			if((ans=='x'||ans=='q')&&M.NumSymbol>0)	
			{
				if(search_points(&x,&y,M.Pixsize,string)>0)
				{
					if(ans=='q')
					{
						sscanf(string,"%s%s%s%s",str2,str2,str2,str1);
						SetVideoMode(0);
						text_view(str1,1000);
						video_on();
					}
					else
						picture_view(string);
					if(unsave_map(LastMap)>0)
					{
						save_box(0,M.Back,Mbuff,
							0,ScreenYs-32/lsize,ScreenXs,32/lsize);
						uncursor(0,xo,yo,5,tbuff);
						val=getpt(0,x,y)-16;
						color=M.Comp_Color[val+16];
						tcursor(0,x,y,5,tbuff);
						cursor(0,x,y,color,5);
						xo=x;yo=y;
						plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
					}
				}
			}
			if(ans=='N')
			{
				unsave_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				pixel_label_all(lsize,screen_xs);
				save_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			}
			if(ans=='R'&&(VideoType=='X'||VideoType=='S'||VideoType=='Y'))
			{
				unsave_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				remove_boundaries(M.MapFile[0][0].color);
				save_box(0,M.Back,Mbuff,
					0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			}
			xo=x;yo=y;
		}
		uncursor(0,xo,yo,5,tbuff);
		unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
		*px=x;*py=y;*sp=speed;
	}
	else		/* mouse routine */
	{
		set_menu(Menu_Mouse2);
		uncursor(0,x,y,5,tbuff);
		do
		{
			ans=mouse_menu(Menu_Mouse2,M.Mback,M.Text,M.High,M.Black,
					0,ScreenXs-1,0,ScreenYs-1,
					row,col,&mrow,&mcol,Mouse,0,BUTTON_UP);
			if(ans=='R'&&(VideoType=='X'||VideoType=='S'||VideoType=='Y'))
				remove_boundaries(M.MapFile[0][0].color);
			if(ans=='a')
				make_pattern(0,ScreenXs-1);
			if(ans=='Z')
			{
				pixsize=M.Pixsize;
				clat=M.Clat;
				clon=M.Clon;
				space=M.Space;
				if(mouse_Zoom(xc,yc,ScreenXs,ScreenYs,&pixsize,&clat,&clon,&space)
					!=27)
					plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
			}
			if(ans=='m')	/* find an icon and ID it */
			{
				save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				do
				{
					val=mouse_get_point(&right,&left,&row,&col,BUTTON_DOWN)-16;
					mouse_times_released(LEFT_BUTTON,&num_times,&row,&col);
					if(right==0)
					{
						if(search_points(&col,&row,M.Pixsize,string)>0)
						{
							mouse_move_cursor(row,col);
							paint_box(0,M.Back,MT_Buff,0,
								ScreenYs-32/lsize,ScreenXs,32/lsize);
							plot_font_h(M.White,2,ScreenYs-30/lsize,string,
								lsize,Font);
						}
					}
				}while(right==0);
				unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			}
			if(ans==27)
				return;
			if((ans=='q'||ans=='U')&&M.NumSymbol>0)/* look at text or pictures */
			{
				mouse_get_point(&right,&left,&row,&col,BUTTON_UP);
				x=col;y=row;
				if(search_points(&x,&y,M.Pixsize,string)>0)
				{
					col=x;row=y;
					if(ans=='q')
					{
						sscanf(string,"%s%s%s%s",str2,str2,str2,str1);
						SetVideoMode(0);
						text_view(str1,1000);
					}
					else
						picture_view(string);
					video_on();
					if(unsave_map(LastMap)>0)
					{
						save_box(0,M.Back,Mbuff,
							0,ScreenYs-32/lsize,ScreenXs,32/lsize);
						uncursor(0,xo,yo,5,tbuff);
						val=getpt(0,x,y)-16;
						color=M.Comp_Color[val+16];
						tcursor(0,x,y,5,tbuff);
						cursor(0,x,y,color,5);
						xo=x;yo=y;
						plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
					}
				}
			}
			if(ans=='T')	/* plot icons */
				point_plot(M.Pixsize,0);
			if(ans=='p')		/* label points */
				for(i=0;i<M.NumSymbol;i++)
					if(PlaceFile[i].on==1&&
						pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
						symbol_plot(i,2);
			if(ans=='t')		/* plot points */
				for(i=0;i<M.NumSymbol;i++)
					if(PlaceFile[i].on==1&&
						pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
						symbol_plot(i,1);
			if(ans=='V')		/* vector plot */
			{
				pixsize=M.Pixsize;
				vector_plot(pixsize);
			}
			if(ans=='P') /* paint a color */
			{
				get_string(string,BarBuffer,
						"Enter","Color",0,0,100,47,1,1,Font,M.White);
				sscanf(string,"%d",&val);
				do
				{
					mouse_get_point(&right,&left,&row,&col,BUTTON_UP)-16;
					if(right==0&&val>=0&&strlen(M.Fill_Name[val])>0)
						paint_area(val,col,row);
				}while(right==0);
			}
			if(ans=='G')	/* plot graphs */
				graph_plot();
			if(ans=='L')			/* locate */
			{
				save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
				do
				{
					val=mouse_get_point(&right,&left,&row,&col,BUTTON_UP)-16;
					dx=col;dy=row;
					xy_ll(dx,dy,&lat,&lon);
					if(val>0)
						sprintf(string,"%3d %s %9.6lf %11.6lf",val,
							M.Fill_Name[val],lat,lon);
					else
						sprintf(string,"        %9.6lf %11.6lf",lat,lon);
					paint_box(0,M.Back,MT_Buff,0,
						ScreenYs-32/lsize,ScreenXs,32/lsize);
					if(right==0)
						plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
				}while(right==0);
				unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			}
			if(ans==-1)
			{
				fclose(fpmark);
				unlink("mapper.mrk");
			}	
			if(ans==-2)	/* plot attibutes with vectors */
					AttribOn=1;
			if(ans==-3)	/* don't plot attibutes with vectors */
					AttribOn=0;
			if(ans==-4) /* hide menu */
				mouse_get_button(&right,&left);
			if(ans=='M') /* mark points */
			{
				get_string(string,BarBuffer,
						"Enter","Text",0,0,100,47,1,1,Font,M.White);
				fclose(fpmark);
				fpmark=fopen("mapper.mrk","at");
				do
				{
					mouse_get_point(&right,&left,&row,&col,BUTTON_UP)-16;
					if(right==0)
					{
						dx=col;dy=row;
						xy_ll(dx,dy,&lat,&lon);
						if(fpmark)
							fprintf(fpmark,"%s %10.6lf %11.6lf %s\n",
								string,lat,lon,string);
						plot_font_h(M.White,col-(strlen(string)*7-2)/2,
							row-5,string,2,Font);
					}
				}while(right==0);
			}
			if(ans=='f'||ans=='F') /* flash colors */
			{
				do
				{
					val=mouse_get_point(&right,&left,&row,&col,BUTTON_DOWN)-16;
					mouse_times_released(LEFT_BUTTON,&num_times,&row,&col);
					if(right==0)
						pixel_flash(val);
				}while(right==0);
			}
			if((ans=='k'||ans=='K')&&M.Fill_In>0)	/* Plot Keys on right */
				screen_xs=pixel_key(ans);
			if(ans=='n'&&(VideoType=='X'||VideoType=='S'||VideoType=='Y'))
			{
				do
				{
					val=mouse_get_point(&right,&left,&row,&col,BUTTON_UP)-16;
					if(right==0&&val>=0&&strlen(M.Fill_Name[val])>0)
						plot_font_h(M.Comp_Color[val+16],
							col-strlen(M.Fill_Name[val])*15/lsize/2,
							row-13/lsize,M.Fill_Name[val],lsize,Font);
				}while(right==0);
			}
			if(ans=='N')
				pixel_label_all(lsize,screen_xs);
			if(ans=='E') /* edit colors */
			{
				do
				{
					val=mouse_get_point(&right,&left,&row,&col,BUTTON_UP)-16;
					if(right==0&&val>=0&&strlen(M.Fill_Name[val])>0)
						pixel_color_edit(lsize,val);
				}while(right==0);
			}
			if(ans>0&&ans<5)	/* label lines */
				pixel_label_vector(&x,&y,&speed,&v_num,ans);
			if(ans==p&&M.NumSymbol>0) /* Label Points */
			{
				do
				{
					val=mouse_get_point(&right,&left,&row,&col,BUTTON_UP)-16;
					if(right==0&&val>=0&&strlen(M.Fill_Name[val])>0)
					{
						for(i=0;i<M.NumSymbol;i++)
						{
							if(PlaceFile[i].on==1&&
										  pixsize>=PlaceFile[i].min_pix&&
										   pixsize<PlaceFile[i].max_pix)
								symbol_plot(i,ans);
						}
					}
				}while(right==0);
			}
			if(ans=='D'&&(BackBright>0.09))
			{
				do
				{
					mouse_get_button(&right,&left);
					if(right==0)
					{
						BackBright-=0.10;
						set_back();
					}
				}while(right==0&&BackBright>0.09);
			}
			if(ans=='B'&&(BackBright<2.0))
			{
				do
				{
					mouse_get_button(&right,&left);
					if(right==0)
					{
						BackBright+=0.10;
						set_back();
					}
				}while(right==0&&BackBright<2.0);
			}
		}while(ans!='X');
	}
	ScreenXs=screen_xs;
	return;
}

/***********************************************************************
**
**
**
**
************************************************************************/

int make_pattern(int x1,int x2)

{
	int i,j,k,m,n;
	int val;
	int screenxs=ScreenXs,nx=x2-x1;

	ScreenXs=MaxXs;
	for(i=0;i<ScreenYs;i++)
	{
		m=i%8;
		getrow(0,x1,x2,i,Buffer);
		for(j=0;j<=nx;j++)
		{
			n=j%8;
			val=Buffer[j];
			val-=16;
			if(val>0&&Pat[val].status>0)
			{
				if(Pat[val].pattern[m][n]>=0)
					plotpt(0,j+x1,i,Pat[val].pattern[m][n]);
			}
		}
	}
	ScreenXs=screenxs;
}




/***********************************************************************
**
**
**
**
************************************************************************/

multiple_maps(int xc,int yc,int xs,int ys,float pixsize,
		float clat,float clon,float space)

{
	int i,j,k;
	char string[100];
	FILE *fpdat,*fplbl,*fppal;
	long offset;
	float ps=PixScale;

	ScreenXs=MaxXs;
	ScreenYs=MaxYs;
	get_string(string,BarBuffer,"Enter","Repeat",
			0,0,100,47,1,1,Font,M.White);
	if(sscanf(string,"%d",&Repeat)<1)
		return(-1);
	if(Repeat<=1||Repeat>9)
	{
		Repeat=0;
		return(-2);
	}
	sprintf(string,"map_comp.lbl");
	fplbl=open_to_write_text(string);
	if(fplbl)
	{
		fprintf(fplbl,"FILE_TYPE          = IMAGE\n");
		fprintf(fplbl,"IMAGE_LINES        = %5d\n",ys*Repeat);
		fprintf(fplbl,"LINE_SAMPLES       = %5d\n",xs*Repeat);
		fprintf(fplbl,"IMAGE_POINTER      = 'map_comp.dat'\n");
		fprintf(fplbl,"PAL_POINTER        = 'map_comp.pal'\n");
		fprintf(fplbl,"PALETTE_POINTER    = 'map_comp.pal'\n");
		fclose(fplbl);
	}
	else
		return(-3);
	sprintf(string,"map_comp.pal");
	fppal=open_to_write_text(string);
	if(fppal)
	{
		for(i=0;i<256;i++)
			fprintf(fppal,"%3d  %3d %3d %3d\n",i,M.lut[i].r,M.lut[i].g,M.lut[i].b);
		fclose(fppal);
	}
	else
		return(-3);
	sprintf(string,"map_comp.dat");
	fpdat=open_to_write_binary(string);
	if(!fpdat)
		return(-3);
	unlink("map_comp.xmg");
	PixScale*=(float)Repeat;
	for(i=0;i<Repeat;i++)
	{
		for(j=0;j<Repeat;j++)
		{
			Rxc=j*ScreenXs;
			Ryc=i*ScreenYs;
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
			ScreenXs=MaxXs;
			ScreenYs=MaxYs;
			for(k=0;k<ys;k++)
			{
				getrow(0,0,xs-1,k,Buffer);
				offset=	(long)xs*(long)Repeat*			/* pixels / line */
							((long)i*(long)ys+(long)k)+	/* lines */
							(long)j*(long)xs;					/* pixels in line so far */
				fseek(fpdat,offset,SEEK_SET);
				fwrite((char *)Buffer,sizeof(char),xs,fpdat);
			}
		}
	}
	fclose(fpdat);
	PixScale=ps;
	Repeat=0;
}



/***********************************************************************
**
**
**
**
************************************************************************/

int set_menu(struct menu m[])

{
	int i,j,k;
	FILE *fpmark;

	for(i=0;i<8;i++)
	{
		for(j=0;j<m[i].num_entry;j++)
		{
			m[i].menu[j].status=1;
			if(m[i].menu[j].ret=='a'&&M.Pattern==0)
				m[i].menu[j].status=0;
			if((m[i].menu[j].ret=='E'||
				 m[i].menu[j].ret=='F'||
				 m[i].menu[j].ret=='D'||
				 m[i].menu[j].ret=='B'||
				 m[i].menu[j].ret=='P'||
				 m[i].menu[j].ret=='N'||
				 m[i].menu[j].ret=='n')&&
				 M.Fill_In<=0)
				m[i].menu[j].status=0;
			if(m[i].menu[j].ret=='n'&&
				 (VideoType=='V'||VideoType=='E'))
				m[i].menu[j].status=0;
			if(m[i].menu[j].ret=='M')
			{
				fpmark=fopen("mapper.mrk","at");
				if(!fpmark)
					m[i].menu[j].status=0;
				fclose(fpmark);
			}
			if(m[i].menu[j].ret=='G'&&
					load_names("*.grf")<=0)
				m[i].menu[j].status=0;
		}
	}
}


/**************************************************************************
**
**
**
************************************************************************* */

int mouse_draw_line(color)

int *color;

{
	int i,j,k;
	char string[STR_LEN],ans,ans2;
	int row1,col1,row2,col2,rowo,colo,times,right,left,row,col;
	int row0,col0,times2;
	int val;
	long li=0;

	mouse_move_cursor(ScreenYs/2,ScreenXs/2);
	mouse_get_point(&right,&left,&row1,&col1,BUTTON_UP);
	mouse_times_released(LEFT_BUTTON,&times,&row,&col);
	rowo=row0=row1;colo=col0=col1;
	do
	{
		rowo=row2=row1;colo=col2=col1;
		mouse_times_released(LEFT_BUTTON,&times,&row,&col);
		mouse_get_point(&right,&left,&row1,&col1,BUTTON_DOWN);
		do
		{
			mouse_information(&right,&left,&row1,&col1);
			mouse_times_released(LEFT_BUTTON,&times,&row,&col);
			if(row2!=row1||col2!=col1)
			{
				if(li>0)
					unplotln(0,col2,row2,colo,rowo,Mbuff);
				tplotln_inv_dot(0,col1,row1,colo,rowo,Mbuff,ScreenXs);
				row2=row1;col2=col1;
				li+=1;
			}
		}while(times==0&&right==0);
		if(li++>0)
			unplotln(0,col2,row2,colo,rowo,Mbuff);
		if(right==0)
			plotln(0,col1,row1,colo,rowo,*color);
		i=0;
	}while(right==0);
	if(left!=0&&right!=0)
	{
		plotln(0,col0,row0,colo,rowo,*color);
		do
		{
			mouse_times_released(RIGHT_BUTTON,&times,&row,&col);
			mouse_times_released(LEFT_BUTTON,&times2,&row,&col);
		}while(times>0&&times2>0);
	}
	return(1);
}


/********************************************************************
**
**
**
********************************************************************* */

char mouse_Zoom(int xc,int yc,int xs,int ys,
			float *pixsize,float *clat,float *clon,float *space)

{
	int i,j,k,k1,k2;
	char ans,ans2,string[100];
	int ix[4],iy[4],speed=16;
	float m_ps=*pixsize,m_clon=*clon,m_clat=*clat,m_space=*space;
	float cur_ps=*pixsize/2.0,cur_clon=*clon,cur_clat=*clat,cur_space=*space;
	float cur_scale=1.090507732;
	int cur_ix=xc+xs/2,cur_iy=yc+ys/2;
	double dlat,dlon;
	int txs,screen_xs,lsize=FontSize;
	int row=ScreenYs/2,col=ScreenXs/2,right,left;
	int mrow=5,mcol=640,trow,tcol;

	save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
	if(VideoType=='S')
		lsize=3;
	if(cur_ps<MIN_PIX)cur_ps=MIN_PIX;

	xy_ll((double)cur_ix,(double)cur_iy,&dlat,&dlon);
	cur_clat=dlat;cur_clon=dlon;
	get_corners(xc,yc,xs,ys,m_ps,m_clat,m_clon,cur_ps,cur_clat,cur_clon,ix,iy);

	tplotln_val(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0],M.White);
	tplotln_val(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1],M.White);
	tplotln_val(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2],M.White);
	tplotln_val(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3],M.White);
	mouse_move_cursor(row,col);
	do
	{
		ans=mouse_menu(Menu_MouseZ,M.Mback,M.Text,M.High,M.Black,
				0,ScreenXs-1,0,ScreenYs-1,
				row,col,&mrow,&mcol,Mouse,0,BUTTON_UP);
		if(ans!='X')
		{
			do
			{
				if(ans=='l')
				{
					trow=row;tcol=col;
					do
					{
						mouse_information(&right,&left,&trow,&tcol);
						if(left!=0)
						{
							cur_ps*=cur_scale;
							cur_iy=trow;
							cur_ix=tcol;
						}
					}while(right==0&&left==0);
				}
				if(ans=='s')
				{
					trow=row;tcol=col;
					do
					{
						mouse_information(&right,&left,&trow,&tcol);
						if(left!=0)
						{
							cur_ps/=cur_scale;
							cur_iy=trow;
							cur_ix=tcol;
						}
					}while(right==0&&left==0);
				}
				if(ans=='S')
				{
					paint_box(0,M.Back,MT_Buff,0,ScreenYs-32/lsize,
						ScreenXs,32/lsize);
					if(cur_ps>1.0)
						sprintf(string,
					"Box Center:  pixel size = %0.2f km   lat = %9.5f  lon = %10.5f",
							cur_ps,cur_clat,cur_clon);
					else if(cur_ps>0.01)
						sprintf(string,
					"Box Center:  pixel size = %0.2f m   lat = %9.5f  lon = %10.5f",
							cur_ps*1000.0,cur_clat,cur_clon);
					else if(cur_ps>0.0001)
						sprintf(string,
					"Box Center:  pixel size = %0.4f m   lat = %9.5f  lon = %10.5f",
							cur_ps*1000.0,cur_clat,cur_clon);
					else
						sprintf(string,
					"Box Center:  pixel size = %0.6f m   lat = %9.5f  lon = %10.5f",
							cur_ps*1000.0,cur_clat,cur_clon);
					plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
				}	
				if(ans=='m')
				{
					mouse_get_point(&right,&left,&row,&col,BUTTON_DOWN);
					if(right==0)
					{
						cur_iy=row;
						cur_ix=col;
					}					
				}
				unplotln(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0]);
				unplotln(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1]);
				unplotln(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2]);
				unplotln(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3]);
				if(ans==27)
				{
					unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
					return(ans);
				}
				xy_ll((double)cur_ix,(double)cur_iy,&dlat,&dlon);
				cur_clat=dlat;cur_clon=dlon;
				get_corners(xc,yc,xs,ys,
					m_ps,m_clat,m_clon,cur_ps,cur_clat,cur_clon,ix,iy);
				tplotln_val(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0],M.White);
				tplotln_val(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1],M.White);
				tplotln_val(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2],M.White);
				tplotln_val(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3],M.White);
			}while(right==0);
		}
	}while(ans!='X');
	unplotln(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0]);
	unplotln(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1]);
	unplotln(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2]);
	unplotln(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3]);
	M.Pixsize=*pixsize=cur_ps;
	M.Clat=*clat=cur_clat;
	M.Clon=*clon=cur_clon;
	unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
	if(cur_ps<20)
	{	
		*space=30.0;
		return(ans);
	}
	if(cur_ps<10)
	{	
		*space=15.0;
		return(ans);
	}
	if(cur_ps<2)
	{	
		*space=5.0;
		return(ans);
	}
	*space=1.0;
	return(ans);
}





/***********************************************************************
**
**
**
**
************************************************************************/

int paint_area(int clr,int x,int y)

{
	double dx,dy,lat,lon;
	int val1,val2;

	if(clr>=0&&clr<=M.Max_Fill)
	{
		dx=x;dy=y;
		xy_ll(dx,dy,&lat,&lon);
		if(FpLab)
			fprintf(FpLab,"   0     0  %9.6lf %11.6lf %3d %3d\n",
				lat,lon,clr,clr);
		if(VideoType=='X'||VideoType=='S'||VideoType=='Y')
			auto_fill_in(0,clr+16,MT_Buff,todo,x,y);
		else
		{
			val1=M.Fill_Color[clr][0];
			val2=M.Fill_Color[clr][1];
			if(val1==M.Back)
				val1=M.Black;
			if(val2==M.Back)
				val2=M.Black;
			auto_fill_in_2(0,val1,val2,Mbuff,todo,x,y);
		}
	}
}




/***********************************************************************
**
**
**
**
************************************************************************/

int pixel_label_vector(int *x,int *y,int *speed,int *v_num,int size)

{
	int i,j,k;
	int color,ans1,xo=*x,yo=*y;
	int cursize=7,labelsize=FontSize,row,col,right,left;

	color=YELLOW;
	for(i=0;i<M.Num_Type;i++)
	Menu_V[i]=M.Group_Name[i];
	*v_num=M_do_menu(Menu_V,M.Num_Type,MENUXC,25,*v_num,
							M.Mback,M.Text,M.High,Font);
	if(Mouse==0)
	{
		tcursor(0,*x,*y,cursize,MouseBuffer);
		cursor(0,*x,*y,color,cursize);
		while((ans1=getpad(&size,x,y,speed,0))!=83&&ans1!=27)
		{
			i=*v_num;
			uncursor(0,xo,yo,cursize,MouseBuffer);
			if(ans1==13)
				vector_label(i,*x,*y,labelsize);
			if(ans1=='l'&&labelsize>1)
			{
				labelsize-=1;
				cursize=15/labelsize;
			}
			if(ans1=='s'&&labelsize<4)
			{
				labelsize+=1;
				cursize=15/labelsize;
			}
			tcursor(0,*x,*y,cursize,MouseBuffer);
			cursor(0,*x,*y,color,cursize);
			xo=*x;
			yo=*y;
		}
		uncursor(0,xo,yo,cursize,MouseBuffer);
	}
	else
	{
		do
		{
			mouse_get_point(&right,&left,&row,&col,BUTTON_UP);
			if(right==0)
				vector_label(*v_num,col,row,size);
		}while(right==0);
	}
}




/***********************************************************************
**
**
**
**
************************************************************************/

int pixel_flash(val)

{
	int tcolor[3];
	int row,col,times;

	tcolor[0]=M.lut[val+16].r;
	tcolor[1]=M.lut[val+16].g;
	tcolor[2]=M.lut[val+16].b;
	M.lut[val+16].r=255;
	M.lut[val+16].g=255;
	M.lut[val+16].b=255;
	WritePalette(M.lut);
	M.lut[val+16].r=tcolor[0];
	M.lut[val+16].g=tcolor[1];
	M.lut[val+16].b=tcolor[2];
	if(Mouse==0)
	{
		if(getch()==0)
			getch();
	}
	else
	{
		do
		{
			mouse_times_released(LEFT_BUTTON,&times,&row,&col);
		}while(times==0);
	}
	WritePalette(M.lut);
}


/***********************************************************************
**
**
**
**
************************************************************************/

int pixel_color_edit(int lsize,int val)

{
	int cspeed=8,ans2,ans3;
	float fspeed=0.05;

	save_box(0,M.Back,Mbuff,0,ScreenYs-64/lsize,20*15/lsize,64/lsize);
	do
	{
		paint_box(0,M.Back,MT_Buff,0,ScreenYs-64/lsize,20*15/lsize,64/lsize);
		sprintf(String,"I=%4.2f H=%3.0f S=%4.2f",
			M.Fill_Color_IHS[val][0],M.Fill_Color_IHS[val][1],
			M.Fill_Color_IHS[val][2]);
		plot_font_h(M.White,1,ScreenYs-64/lsize+1,String,lsize,Font);
		sprintf(String," %4.2f    %3d   %4.2f",fspeed,cspeed,fspeed*2.0);
		plot_font_h(M.White,1,ScreenYs-32/lsize+1,String,lsize,Font);
		ans2=getch();
		if(ans2=='-')
		{
			if(cspeed>1)
				cspeed/=2;
			if(fspeed>0.01)
				fspeed/=2.0;
		}
		if(ans2=='+')
		{
			if(cspeed<64)
				cspeed*=2;
			if(fspeed<0.16)
				fspeed*=2.0;
		}
		if(ans2==0)
		{
			ans3=getch();
			if(ans3==71)
				M.Fill_Color_IHS[val][0]+=fspeed;
			if(ans3==79)
				M.Fill_Color_IHS[val][0]-=fspeed;
			if(M.Fill_Color_IHS[val][0]>1.0)
				M.Fill_Color_IHS[val][0]=1.0;
			if(M.Fill_Color_IHS[val][0]<0.0)
				M.Fill_Color_IHS[val][0]=0.0;
			if(ans3==73)
				M.Fill_Color_IHS[val][2]+=fspeed*2.0;
			if(ans3==81)
				M.Fill_Color_IHS[val][2]-=fspeed*2.0;
			if(M.Fill_Color_IHS[val][2]>1.0)
				M.Fill_Color_IHS[val][2]=1.0;
			if(M.Fill_Color_IHS[val][2]<0.0)
				M.Fill_Color_IHS[val][2]=0.0;
			if(ans3==72)
				M.Fill_Color_IHS[val][1]+=cspeed;
			if(ans3==80)
				M.Fill_Color_IHS[val][1]-=cspeed;
			if(M.Fill_Color_IHS[val][1]>255)
				M.Fill_Color_IHS[val][1]-=256;
			if(M.Fill_Color_IHS[val][1]<0)
				M.Fill_Color_IHS[val][1]+=256;
			set_lut('c',1);
		}
	}while(ans2!=27&&ans2!=13);
	unsave_box(0,M.Back,Mbuff,0,ScreenYs-64/lsize,20*15/lsize,64/lsize);
}



/***********************************************************************
**
**
**
**
************************************************************************/

int pixel_label_all(int lsize,int screen_xs)

{
	int m,idc[2];
	FILE *fpblb;
	double lat,lon,dx,dy;
	int ix,iy,ival,color;

	ScreenXs=screen_xs;
	for(m=0;m<M.Num_Fill;m++)
	{
		fpblb=open_to_read_binary(M.Fill_In_Name[m]);
		if(fpblb)
		{
			while(fread((char *)idc,sizeof(int),2,fpblb)==2)
			{
				fread((char *)&lat,sizeof(double),1,fpblb);
				fread((char *)&lon,sizeof(double),1,fpblb);
				ll_xy(&dx,&dy,lat,lon);
				ix=dx;iy=dy;
				if(Repeat>1||(ix>=0&&ix<ScreenXs&&iy>=0&&iy<ScreenYs))
				{
					ival=idc[1];
					color=M.Comp_Color[ival+16];
					if(ival>0)
					{
						if(strlen(M.Fill_Name[ival])>0)
							plot_font_h(color,
								ix-strlen(M.Fill_Name[ival])*15/lsize/2,
								iy-13/lsize,M.Fill_Name[ival],lsize,Font);
					}
				}
			}
		}
	}
	ScreenXs=MaxXs;
	fclose(fpblb);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int pixel_key(int ans)

{
	int val,screen_xs;
	double lat,lon;

	ScreenXs=MaxXs;
	M.KeyPlotted=0;
	val=3;
	if(ans=='k'&&VideoType=='S')
		val=4;
	if(ans=='K'&&VideoType!='S')
		val=2;
	plot_key(ScreenXs,0,val);
	screen_xs=ScreenXs;
	xy_ll((double)(ScreenXs/2),(double)(ScreenYs/2),&lat,&lon);
	miv.indat[0]=M.Pixsize;
	miv.indat[1]=M.Clat=lat;
	miv.indat[2]=M.Clon=lon;
	miv.indat[3]=M.Clon;
	miv.indat[4]=M.Clat+10;
	miv.indat[5]=M.Clat-10;
	map_pos.xc=0;	
	map_pos.yc=0;
	map_pos.xs=ScreenXs;
	map_pos.ys=ScreenYs;
	auto_set_cc(miv.type,miv.indat);
	ScreenXs=MaxXs;
	return(screen_xs);
}



/***********************************************************************
**
**
**
**
************************************************************************/

int pix_locate_help(int lsize,char *string)

{
	unsave_box(0,M.Back,Mbuff,
		0,ScreenYs-32/lsize,ScreenXs,32/lsize);
	save_box(0,M.Back,Mbuff,
		0,0,MaxXs,32/lsize);
	paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
	if(VideoType=='X'||VideoType=='S'||VideoType=='Y')
	{
		sprintf(string,
	"(k)ey  (K)ey  (n)ame 1  (N)ame all  (P)aint area  (R)emove lines");
		plot_font_h(M.White,1,1,string,FontSize,Font);
		if(getch()==0)
			getch();
		if(M.Fill_In>0)
		{
			paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
			sprintf(string,"(D)arker map colors  (B)righter map colors");
			plot_font_h(M.White,1,1,string,FontSize,Font);
			if(getch()==0)
				getch();
		}
		paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
		sprintf(string,"(E)dit colors  (C)olor table output  (f)lash");
	}
 	else
		sprintf(string,"(k)ey  (K)ey  (n)ame 1  (N)ame all  (P)aint area");
	plot_font_h(M.White,1,1,string,FontSize,Font);
	if(getch()==0)
		getch();
	if(load_names("*.grf")>0)
	{
		paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
		sprintf(string,"(G)raph Data Points");
		plot_font_h(M.White,1,1,string,FontSize,Font);
		if(getch()==0)
			getch();
	}
	if(M.NumSymbol>0)
	{
		paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
		sprintf(string,"(l)abel points   (i)ndex points");
		plot_font_h(M.White,1,1,string,FontSize,Font);
		getch();
		paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
		sprintf(string,"(S)earch for nearest point  e(x)it to picture");
		plot_font_h(M.White,1,1,string,FontSize,Font);
		getch();
		paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
	}
	unsave_box(0,M.Back,Mbuff,0,0,MaxXs,32/lsize);
	save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
}


/***********************************************************************
**
**
**
**
************************************************************************/

graph_plot()

{
	int i,j,k;
	int num_files,g_num=0,type;
	FILE *fp;
	char string1[100],string2[100],string3[100],string[100];
	char *menu_g[50];

	for(i=0;i<MAX;i++)
	{
		G[i]=(int *)malloc(MAX*2*sizeof(int));
		if(G[i]==NULL)
		{
			SetVideoMode(0);
			printf("at i = %d   Not enough space for graphs to be loaded.\n\n",i);
			exit(0);
		}
	}
	num_files=load_names("*.grf");
	if(num_files<1)
	{
		free_G();
		return(0);
	}
	for(k=0;k<num_files;k++)
		menu_g[k]=GF_File[k].name;
	g_num=M_do_menu(menu_g,num_files,MENUXC,25,g_num,
					M.Mback,M.Text,M.High,Font);
	fp=fopen(menu_g[g_num],"rt");
	if(!fp)
	{
		free_G();
		return(-1);
	}
	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"TYPE")==0)
		type=get_type(string1,string2,string3);
	if(type<1)
	{
		free_G();
		fclose(fp);
		return(-2);
	}
	do
	{
		if(type==1)
			type=plot_pie(fp);
		if(type==2)
			type=plot_rectangle_text(fp);
	}while(type>0);
	fclose(fp);
	free_G();
	return(1);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int free_G()

{
	int i;

	for(i=0;i<MAX;i++)
		free(G[i]);
}



/***********************************************************************
**
**
**
**
************************************************************************/

int get_type(string1,string2,string3)

char *string1,*string2,*string3;

{
	int type=-1;

	if(strcmpi(string3,"PIE_CHART")==0||strcmpi(string2,"PIE_CHART")==0)
		type=1;
	else if(strcmpi(string3,"RECTANGLE+TEXT")==0||
				strcmpi(string2,"RECTANGLE+TEXT")==0)
		type=2;
	return(type);
}


/***********************************************************************
**
**
**
**
************************************************************************/

int plot_rectangle_text(fp)

FILE *fp;

{
	int i,j,k;
	char string1[100],string2[100],string3[100],string[100];
	char string4[50],string5[50];
	int num,size,radius,type=0;
	int hot_col=-1,hot_row=-1;
	int row,col,text,mx,my,tx,ty;
	double lat,lon,x,y;
	int ix,iy,limit[10],val,end=0,numread;
	float maxpix,minpix,pixsize=M.Pixsize;

	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"PIXEL_RANGE")==0)
	{
		sscanf(string,"%s%s%f%f",string1,string2,&maxpix,&minpix);
	}
	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"COLORS")==0)
	{
		sscanf(string3,"%d",&num);
		if(num>10)
		{
			fclose(fp);
			return(-3);
		}
		for(k=0;k<num;k++)
			fgets(string,100,fp);
	}	
	else
		return(-2);
	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"TEXT_COLOR")==0)
	{
		sscanf(string3,"%d",&text);
	}
	else
		return(-2);
	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"TEXT_SIZE")==0)
	{
		sscanf(string3,"%d",&size);
		ty=23/size;
		ty/=2;
	}
	else
		return(-2);
	fgets(string,100,fp);
	numread=sscanf(string,"%s%s%s%s%s",
		string1,string2,string3,string4,string5);
	if(strcmpi(string1,"SYMBOL_ROWS")==0)
	{
		sscanf(string3,"%d",&row);
		if(row>MAX)
		{
			printf("Too many rows in symbol.\n\n");
			exit(0);
		}
		my=row/2;
		if(numread>4)
		{
			sscanf(string5,"%d",&hot_row);
			my=hot_row;
			ty+=row/2-my;
		}
	}
	else
		return(-2);
	fgets(string,100,fp);
	numread=sscanf(string,"%s%s%s%s%s",
		string1,string2,string3,string4,string5);
	if(strcmpi(string1,"SYMBOL_COLS")==0)
	{
		sscanf(string3,"%d",&col);
		if(col>MAX*2)
		{
			printf("Too many columns in symbol.\n\n");
			exit(0);
		}
		mx=col/2;
		if(numread>4)
		{
			sscanf(string5,"%d",&hot_col);
			mx=hot_col;
		}
	}
	else
		return(-2);
	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"BEGIN_SYMBOL")==0)
	{
		for(i=0;i<row;i++)
		{
			for(j=0;j<col;j++)
			{
				fscanf(fp,"%s",string);
				if(strcmpi(string,"END")==0)
				{
					end=1;
					break;
				}
				if(string[0]=='*')
					G[i][j]=-1;
				else if(string[0]>='a'&&string[0]<='f')
					G[i][j]=string[0]-'a'+10;
				else
					G[i][j]=string[0]-'0';
			}
			if(end==1)
				break;
		}
	}
	else
		return(-2);
	fgets(string,100,fp);
	fgets(string,100,fp);
	while(fscanf(fp,"%s",string)>0)
	{
		if(strcmpi(string,"TYPE")==0)
		{
			fgets(string1,100,fp);
			sscanf(string1,"%s%s",string2,string3);
			type=get_type(string,string2,string3);
			return(type);
		}
		fscanf(fp,"%lf%lf%s",&lat,&lon,string1);
		if(maxpix>pixsize&&minpix<=pixsize)
		{
			ll_xy(&x,&y,lat,lon);
			ix=x;iy=y;
			tx=strlen(string);
			tx*=15/size;
			tx-=5/size;
			tx/=2;
			if(Repeat>1||(ix+mx>=0&&ix-mx<ScreenXs&&iy+my>=0&&iy-my<ScreenYs))
			{
				for(i=0;i<row;i++)
					for(j=0;j<col;j++)
						if(G[i][j]>=0)
							plotpt(0,ix-mx+j,iy-my+i,G[i][j]);
				plot_font_h(text,ix-tx,iy-ty,string,size,Font);
			}
		}
	}
	return(-1);
}


/***********************************************************************
**
**
**
**
************************************************************************/

int video_off()

{
	SetVideoMode(0);
}


/***********************************************************************
**
**
**
**
************************************************************************/

int plot_pie(fp)

FILE *fp;

{
	int i,j,k;
	float dat[10],sum;
	char name[10][50];
	char string1[100],string2[100],string3[100],string[100];
	int num,size,radius,type=0;
	int color[10];
	double lat,lon,x,y;
	int ix,iy,limit[10],val;
	int keyxs=0,keyys,keyxc=0,keyyc=0,fontsize=FontSize;
	int len,num_files,g_num=0;

	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"DATA_TYPES")==0)
	{
		sscanf(string3,"%d",&num);
		if(num>10)
		{
			fclose(fp);
			return(-3);
		}
		for(k=0;k<num;k++)
		{
			fgets(string,100,fp);
			sscanf(string,"%s%s%s",name[k],string2,string3);
			sscanf(string2,"%d",color+k);
			len=strlen(name[k])*15/fontsize;
			if(len>keyxs)
				keyxs=len;
		}
		keyxs+=2;
		keyys=30/fontsize*num;
		for(k=0;k<num;k++)
		{
			paint_box(0,color[k],BarBuffer,
				keyxc,keyyc+k*30/fontsize,keyxs,30/fontsize);
			plot_font_h(0,keyxc+1,keyyc+k*30/fontsize+1,name[k],fontsize,Font);
		}
	}	
	fgets(string,100,fp);
	sscanf(string,"%s%s%s",string1,string2,string3);
	if(strcmpi(string1,"SYMBOL_SIZE")==0)
	{
		sscanf(string3,"%d",&size);
		radius=size/2;
	}
	else
		return(-2);
	for(i=0;i<size;i++)
		for(j=0;j<size;j++)
			fscanf(fp,"%d",&G[i][j]);
	fscanf(fp,"%s",string);
	if(strcmpi(string,"END")!=0)
	{
		fclose(fp);
		return(-4);
	}
	while(fscanf(fp,"%s",string)>0)
	{
		if(strcmpi(string,"TYPE")==0)
		{
			fgets(string,100,fp);
			sscanf(string,"s%s",string2,string3);
			type=get_type(string,string2,string3);
			return(type);
		}
		fscanf(fp,"%lf%lf",&lat,&lon);
		for(k=0;k<num;k++)
			fscanf(fp,"%f",dat+k);
		ll_xy(&x,&y,lat,lon);
		ix=x;
		iy=y;
		if(ix>=-size&&ix<ScreenXs+size&&iy>=-size&&iy<ScreenYs+size)
		{
			sum=0.0;
			for(k=0;k<num;k++)
			{
				sum+=dat[k];
				limit[k]=sum+0.5;
			}
			for(i=0;i<size;i++)
				for(j=0;j<size;j++)
				{
					if(G[i][j]>0)
					{
						for(k=0;k<num;k++)
						{
							val=color[k];
							if(G[i][j]<limit[k])
								break;
						}
						plotpt(0,ix-radius+j,iy-radius+i,val);
					}
				}
		}
	}
	return(-1);
}





/**************************************************************************
**
**
**
************************************************************************* */

char fsplit(instring,path,name,type)

char *instring,*path,*name,*type;

{
	int i,j,k;
	int dot=-1,last_slash=-1,len=strlen(instring),first_slash=-1;
	int num=0,colon=-1;
	int disk=0;
	char string[STR_LEN];

	strcpy(string,instring);
	path[0]='\0';
	name[0]='\0';
	type[0]='\0';

	for(i=0;i<len;i++)
	{
		if(string[i]==':')
			colon=i;
		else if(string[i]=='\\')
		{
			last_slash=i;
			if(first_slash<0)
				first_slash=i;
		}	
		else if(string[i]=='.')
			dot=i;
	}
	if(dot<0&&first_slash>=0&&last_slash<len-1)  /* d:test */
	{
		string[len]='\\';
		last_slash=len;
		len+=1;
		string[len]='\0';
	}
	if(dot<0&&first_slash<0&&len>colon-1)
	{
		string[len]='.';
		dot=len;
		len+=1;
		string[len]='\0';
	}
	if(colon>0)
		disk=string[0];
	if(first_slash>=0)
	{
		for(i=first_slash;i<=last_slash;i++)
			path[i-first_slash]=string[i];
		path[last_slash-first_slash+1]='\0';
	}
	if(last_slash>0&&last_slash<len-1&&dot>0)
	{
		for(i=last_slash+1;i<dot;i++)
			name[i-last_slash-1]=string[i];
		name[dot-last_slash-1]='\0';
	}
	if(colon>0&&last_slash<0&&dot>0)	/* d:test.img */
	{
		for(i=colon+1;i<dot;i++)
			name[i-colon-1]=string[i];
		name[dot-colon]='\0';
	}
	if(colon<0&&last_slash<0&&dot>0)	/* test.img */
	{
		for(i=0;i<dot;i++)
			name[i]=string[i];
		name[dot]='\0';
	}
	if(dot>0&&dot<len-1)
	{
		for(i=dot+1;i<len;i++)
			type[i-dot-1]=string[i];
		type[len-dot-1]='\0';
		if(strlen(type)>3)
			type[3]='\0';
	}
	return(disk);
}
/***********************************************************************
**
**
**
**
************************************************************************/

int print_help()

{
	int i,j,k=1;
	FILE *fphlp;
	char string[100];

	fphlp=open_to_read_text("mapper.hlp");
	if(!fphlp)
	{
	printf("mapper plotting software:\n");
	printf("   by Russell A. Ambroziak, Ph. D.\n");
	printf("   Reston, Virginia,  Phone 703-648-6168\n");
	printf("\n");
	printf("command line options:\n");
	printf("\n");
	printf("/e = EGA\n");
	printf("/v = VGA 640x480x16 colors\n");
	printf("/s = VGA 320x200x256 colors\n");
	printf("/y = Extended VGA 640x400x256 colors\n");
	printf("/x = Extended VGA 640x480x256 colors\n");
	printf("\n");
	printf("/l [path][file_name].[file_type] = output '.LAB' file of points\n");
	printf("/o [path][file_name].[file_type] = output run information file\n");
	printf("/a [path][file_name] = automatic map load on start\n");
	printf(
		"/p [program_name] = passes control to program from point (S)earch\n");
	printf(
	"/r creates a '.MPR' file with ranges of data for all files then stops\n");
	printf("/h help -- prints this message\n");
	printf("\n");
	printf(
		"Any path name for data search can by added also, e.g. D:\\DATA or\n");
	printf("\\MAPPER\\DATA\\MAP\n");
	printf("\n Hit Any Key to Continue\n");
	}
	else
	{
		while(fgets(string,100,fphlp))
		{
			if(string[0]=='*')
			{
				printf("Hit Any Key to Continue.");
				getch();
				printf("\n");
			}
			else if(string[0]=='#')
			{
				fclose(fphlp);
				return(2);
			}
			else
				printf("%s",string);
		}
	}
	fclose(fphlp);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int output_image(row,col,fpout,buffer,outbuff)

int row,col;
FILE *fpout;
unsigned char *buffer,*outbuff;

{
	int i,j,k;
	int num,val;
	int p,p1,p2,pval,pnum,bytes;
	unsigned char unum;

	/* Run_Length Encoded Color-Mapped Image */
	for(i=0;i<row;i++)
	{
		getrow(0,0,col-1,i,buffer);
		j=bytes=0;
		do
		{
			pval=buffer[j];
			p1=j;
			p2=p1+128;
			if(p2>col)
				p2=col;
			for(p=p1+1;p<p2;p++)	/* count pixels of value pval */
			{
				if(buffer[p]!=pval)
					break;
			}
			pnum=p-p1;	/* number of pixels of value pval */
			if(pnum>2)	 /* 3 or pixels of the same color */
			{
				pnum=p-p1;	/* number of pixels of color pval */
				unum=pnum-1;
				unum=unum|128;			     /* turn sign bit on */
				outbuff[bytes++]=unum;
				outbuff[bytes++]=pval;
				j=p;
			}
			else
			{
				p1=j;
				p2=p1+128;
				if(p2>col)
					p2=col;
				for(p=p1+1;p<p2-2;p++)	/* count pixels of changing value */
				{
					if(buffer[p+1]==buffer[p]&&buffer[p+2]==buffer[p])
						break;
				}
				if(p==p2-2)	/* still changing at end of 128 packette */
					p=p2;		/* go to end of packette */
				pnum=p-p1;
				unum=pnum-1;
				outbuff[bytes++]=unum;
				for(k=0;k<pnum;k++)
					outbuff[bytes++]=buffer[j+k];
				j=p;
			}
		}while(j<col);
		fwrite((char *)outbuff,sizeof(char),bytes,fpout);
	}
}


/***********************************************************************
**
**
**
**
************************************************************************/

int input_image(row,col,fp,buffer)

int row,col;
FILE *fp;
unsigned char *buffer;

{
	int i,j,k;
	int num,unum,val;
	unsigned char uval,cval;

	i=num=0;
	do
	{
		fread((char *)&uval,sizeof(char),1,fp);
		val=uval;
/*		uval=fgetc(fp); */
		if(val>127)		/* run length */
		{
			unum=val-128;
			unum+=1;
			fread((char *)&cval,sizeof(char),1,fp);
			val=cval;
/*			val=fgetc(fp); */
			for(k=0;k<unum;k++)
				buffer[num++]=val;
		}
		else					/* block of pixels */
		{
			unum=val;
			unum+=1;
			fread((char *)buffer+num,sizeof(char),unum,fp);
			num+=unum;
		}
		if(num>=col)
		{
			plotrow(0,0,col-1,i,buffer);
			num=0;
			i+=1;
		}
	}while(i<row);
}




                                                 