/* A graphic libruary for GNU FORTRAN Emulator f2c. (Copyright by A. Kaneko) */
/* This is an emulator of the Graphic libruary for ProFORTRAN-77 */
/* published from the University of Tokyo Press (1991) by the author. */
/* According to the general public license agreement of GNU, */
/* this program is subject to the same rule of GNU. */
/* It calls the graphic libruary functions for XWindow in C language and */
/* passes it to f2c adapting them to FORTRAN subroutine calling rule. */ 
/* The usage of each subroutine is the same as that of the original one in */
/* graphic libruary for ProFORTRAN-77, written in "Kyouyou no Suugaku-Keisanki" */
/* published from the University of Tokyo Press, 1991. */
/* There are some extensions and restrictions caused by XWindow system. */
/* This version gives output to file "xgrfps.eps" of important graphic */
/* instructions in PostScript format. */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#define _MaxScreenX 799
#define _MaxScreenY 599
#define _IMUL 4096
/* GRX.H */
/* TYPES.H */
double dmod_(double,double);
double random_(int *);/* float" does not return a correct value to f2c main in Sun */
void GrLine(int,int,int,int,int);
void GrLineSub(int ,int ,int ,int ,int );
void _paint_up(int ,int ,int );
void _paint_down(int ,int ,int );
void _transkj(char *,char *,int);
void _color_(int,int,int,int);
static int ESC=27;
static int _GrMaxX=_MaxScreenX;
static int _GrMaxY=_MaxScreenY;
static int _GrMaxYc=_MaxScreenY+1;
static int _MODE=3;
static int _LCOLOR=15;
static int _BLCOLOR=15;
static int _GCOLOR=0;
static int _PMODE=0;
static int _PCOLOR=0;
static int _LSTYLE=1;
static int _HATCHSTYLE=1;
static int _TILESTYLE=0;
static int _BLMODE=1;
static int _BLSTYLE=1;
static int _CHCOLOR=15;
static int _LASTX=0;
static int _LASTY=0;
static float _PI=3.141592653589793;
static int _VMINX=0;
static int _VMAXX=_MaxScreenX;
static int _VMINY=0;
static int _VMAXY=_MaxScreenY;
static int _WMINX=0;
static int _WMAXX=_MaxScreenX;
static int _WMINY=0;
static int _WMAXY=_MaxScreenY;
static int _WRTSCREEN=0;
static int _DSPSCREEN=0;
static  Display  *_DISPLAY=NULL;
static  Window _WINDOW;
static GC _GCONTEXT;
static XEvent _EVENT;
static XSetWindowAttributes _ATTR;
static XImage *_XIMAGE;
static Pixmap _PIXMAP;
static _PIXEL[16];
static Colormap _CMAP;
static int _LWIDTH=1;
static char _LSTYLELIST2[]={6,2};
static char _LSTYLELIST3[]={3,5};
static char _LSTYLELIST4[]={8,3,2,3};
static char _LSTYLELIST5[]={6,2,2,2,2,2};
static Pixmap _HATCH[7];
static FILE *_outfile;
static int _NEWPATH=0;
/* */
/* void s_paus(char *message,long il){char c;
     if (il!=0) printf("%s\n",message);
     else printf("Pause\n");
     c=getc(stdin);
     }*/
void pause_(char *message,long il){char c;
     if (il!=0) printf("%s\n",message);
     else printf("Pause\n");
     c=getc(stdin);
     }
void gpause_(int *ix,int *iy,char *message){char c;
     XSetForeground(_DISPLAY,_GCONTEXT,_CHCOLOR);
/*     XSetBackground(_DISPLAY,_GCONTEXT,_GCOLOR); */
     XDrawString(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxYc-(*iy),message,strlen(message));
     c=getchar();
     }
void gtime_(long *ih,long *im,long *is,long *ims){
  unsigned long t0,t1,h,m,s;
    struct timeval tv;
    struct timezone tz;
    gettimeofday(&tv,&tz);
    t0=tv.tv_sec;
    t1=tv.tv_usec;
    m=t0/60; s=t0%60; /* *is=t0%60 does not work. I don't know why. */
    h=m/60; *im=m%60;
    *ih=h%24; *ims=t1; *is=s; 
	//    printf("usec=%ld\n",t1);
}
#if 0
void time_(long *ih,long *im,long *is,long *ics){
     char *p; char it[3];
     time_t t;
     it[2]=0;
     time(&t);
     p=ctime(&t);
     it[0]=*(p+11);
     it[1]=*(p+12);
     *ih=atol(it);
     it[0]=*(p+14);
     it[1]=*(p+15);
     *im=atol(it);
     it[0]=*(p+17);
     it[1]=*(p+18);
     *is=atol(it);
     }
#endif
void textmode_(void){
     }
void init_(int *ix,int *iy,int *ix2,int *iy2){
     XColor xcolor, c;
     XEvent event; //added
     XSetWindowAttributes at; //added
     int i;
     _GrMaxX=*ix2-*ix+1;
     _GrMaxY=*iy2-*iy+1;
     _outfile=fopen("xgrfps.eps","w");
     if (_DISPLAY==NULL) {
           _DISPLAY = XOpenDisplay ( NULL );
           _WINDOW = XCreateSimpleWindow (_DISPLAY, RootWindow(_DISPLAY,0),
                                            0,0,
                                           _GrMaxX+1,_GrMaxY+1,1,0,1);
           at.backing_store=Always; //added
           XChangeWindowAttributes(_DISPLAY,_WINDOW,CWBackingStore,&at); //added
           XSelectInput(_DISPLAY,_WINDOW,ExposureMask); //added
           XMapWindow(_DISPLAY,_WINDOW);
           _PIXMAP=XCreatePixmap(_DISPLAY,_WINDOW,800,600,DefaultDepth(_DISPLAY,0));
           _CMAP=DefaultColormap(_DISPLAY,0);
           _GCONTEXT = XCreateGC (_DISPLAY,_PIXMAP,0,0);
           }
     _color_(0,0,0,0);
     _color_(1,1,1,1);
     _color_(2,0,0,8);
     _color_(3,0,0,15);
     _color_(4,8,0,0);
     _color_(5,15,0,0);
     _color_(6,8,0,8);
     _color_(7,15,0,15);
     _color_(8,0,8,0);
     _color_(9,0,15,0);
     _color_(10,0,8,8);
     _color_(11,0,15,15);
     _color_(12,8,8,0);
     _color_(13,15,15,0);
     _color_(14,8,8,8);
     _color_(15,15,15,15);
     _MODE=3;
     _LCOLOR=15;
     _BLCOLOR=15;
     _GCOLOR=0;
     _PMODE=0;
     _PCOLOR=0;
     _LSTYLE=1;
     _HATCHSTYLE=1;
     _TILESTYLE=0;
     _BLMODE=1;
     _BLSTYLE=1;
     _CHCOLOR=15;
     _LASTX=0;
     _LASTY=0;
     XClearWindow(_DISPLAY,_WINDOW);
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_GCOLOR]);
     for (i=1;i<=6;i++){
         _HATCH[i]=XCreatePixmap(_DISPLAY,_WINDOW,4,4,DefaultDepth(_DISPLAY,0));
         XFillRectangle(_DISPLAY,_HATCH[i],_GCONTEXT,0,0,4,4);
         }
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_GCOLOR]);
     XFillRectangle(_DISPLAY,_WINDOW,_GCONTEXT,0,0,_MaxScreenX,_MaxScreenY);
     XSetBackground(_DISPLAY,_GCONTEXT,_PIXEL[_GCOLOR]);
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_LCOLOR]);
//     XSync(_DISPLAY,0);
     XFlush(_DISPLAY); 
     fprintf(_outfile,"%%!PS-Adobe-2.0 EPSF-2.0\n");
     fprintf(_outfile,"%%%%Creator: xgrfps\n");
     fprintf(_outfile,"%%%%DocumentFonts: Helvetica\n");
     fprintf(_outfile,"%%%%BoundingBox: 0 0 800 600\n");
     fprintf(_outfile,"%%%%EndComments\n");
     }
void view_(int *ix,int *iy,int *ix2,int *iy2){
     _VMINX=*ix;
     _VMAXX=*ix2;
     _VMINY=*iy;
     _VMAXY=*iy2;
     }
void window_(int *ix,int *iy,int *ix2,int *iy2){
     _WMINX=*ix;
     _WMAXX=*ix2;
     _WMINY=*iy;
     _WMAXY=*iy2;
     }
void screen_(int *ip,int *iq){
     _WRTSCREEN=*ip;
     _DSPSCREEN=*iq;
     }
void scroll_(int *ix,int *iy){
     }
void cls_(void){
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_GCOLOR]);
     XFillRectangle(_DISPLAY,_WINDOW,_GCONTEXT,0,0,_MaxScreenX,_MaxScreenY);
     XFlush(_DISPLAY);
     }
void cls2_(int *ix,int *iy,int *ix2,int *iy2,int *ip){int iyc,iy2c;
     iyc=*iy;
     iy2c=*iy2;
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_GCOLOR]);
     XFillRectangle(_DISPLAY,_WINDOW,_GCONTEXT,*ix,iyc,*ix2-*ix,iy2c-iyc);
     XFlush(_DISPLAY);
     }
void mode_(int *m){
     _MODE=*m;
     }
int ipdata_(int *ix,int *iy,int *ic1,int *ic2,int *ic3,int *n){
     return 0;
     }
void get_(int *ix1,int *iy1,int *ix2,int *iy2,char *iddata){
     }
void put_(int *ix1,int *iy1,int *ix2,int *iy2,char *iddata){
     }
void copy_(int *ix,int *iy,int *ix2,int *iy2,int *ix3,int *iy3,int *ip,int *iq,int *m){
     }
void color_(int *ic,int *ir,int *ig,int *ib){
     XColor xcolor;
     xcolor.red=(*ir)*_IMUL+_IMUL-1;
     xcolor.green=(*ig)*_IMUL+_IMUL-1;
     xcolor.blue=(*ib)*_IMUL+_IMUL-1;
     XAllocColor(_DISPLAY,_CMAP,&xcolor);
     _PIXEL[*ic]=xcolor.pixel;
     }
void _color_(int ic,int ir,int ig,int ib){
     XColor xcolor;
     xcolor.red=ir*_IMUL+_IMUL-1;
     xcolor.green=ig*_IMUL+_IMUL-1;
     xcolor.blue=ib*_IMUL+_IMUL-1;
     XAllocColor(_DISPLAY,_CMAP,&xcolor);
     _PIXEL[ic]=xcolor.pixel;
     }
void gcolor_(int *ic){
     _GCOLOR=*ic;
     XSetBackground(_DISPLAY,_GCONTEXT, _PIXEL[_GCOLOR]);
     }
void lcolor_(int *ic){
     float r,g,b;
     _LCOLOR=*ic;
     r=(float)((_PIXEL[_LCOLOR] & 0xff0000)>>16)/256;
     g=(float)((_PIXEL[_LCOLOR] & 0x00ff00)>>8)/256;
     b=(float)(_PIXEL[_LCOLOR] & 0x0000ff)/256;
     fprintf(_outfile,"%5.3f %5.3f %5.3f setrgbcolor\n",r,g,b);
     }
void lstyle_(int *is){
     int cap,join,style;
     _LSTYLE=*is;
     if (_NEWPATH==1){
        fprintf(_outfile,"stroke\n");
        _NEWPATH=0;
        }
     switch(_LSTYLE){
            case 1: XSetLineAttributes(_DISPLAY,_GCONTEXT,_LWIDTH,LineSolid,CapButt,JoinMiter);
                    fprintf(_outfile,"[] 0 setdash\n");
                    break;
            case 2: XSetLineAttributes(_DISPLAY,_GCONTEXT,_LWIDTH,LineOnOffDash,CapButt,JoinMiter);
                    XSetDashes(_DISPLAY,_GCONTEXT,0,_LSTYLELIST2,2); 
                    fprintf(_outfile,"[6 3] 3 setdash\n");
                    break;
            case 3: XSetLineAttributes(_DISPLAY,_GCONTEXT,_LWIDTH,LineOnOffDash,CapButt,JoinMiter);
                    XSetDashes(_DISPLAY,_GCONTEXT,0,_LSTYLELIST3,2); 
                    fprintf(_outfile,"[2 4] 0 setdash\n");
                    break;
            case 4: XSetLineAttributes(_DISPLAY,_GCONTEXT,_LWIDTH,LineOnOffDash,CapButt,JoinMiter);
                    XSetDashes(_DISPLAY,_GCONTEXT,0,_LSTYLELIST4,4); 
                    fprintf(_outfile,"[4 2 2 2] 2 setdash\n");
                    break;
            case 5: XSetLineAttributes(_DISPLAY,_GCONTEXT,_LWIDTH,LineOnOffDash,CapButt,JoinMiter);
                    XSetDashes(_DISPLAY,_GCONTEXT,0,_LSTYLELIST5,6); 
                    fprintf(_outfile,"[4 2 2 2 2 2] 2 setdash\n");
                    break;
            } 
     XFlush(_DISPLAY);
     fprintf(_outfile,"stroke\n");
     }
void line_(int *ix,int *iy,int *ix2,int *iy2){
     if (_NEWPATH==1){
        fprintf(_outfile,"stroke\n");
        }
     fprintf(_outfile,"newpath\n");
     fprintf(_outfile,"%d %d moveto\n",*ix,*iy);
     GrLineSub(*ix,_GrMaxY-(*iy),*ix2,_GrMaxY-(*iy2),_LCOLOR);
     _LASTX=*ix2;                /* last ref data should not be scissored */
     _LASTY=_GrMaxY-(*iy2);
     XFlush(_DISPLAY);
     fprintf(_outfile,"stroke\n");
     _NEWPATH=0;
     }
void line2_(int *ix,int *iy,int *ix2,int *iy2,int *ic,int *is){
     GrLineSub(*ix,_GrMaxY-*iy,*ix2,_GrMaxY-*iy2,*ic);
     _LASTX=*ix2;
     _LASTY=_GrMaxY-(*iy2);
     XFlush(_DISPLAY);
     }
void move_(int *ix,int *iy){
     _LASTX=*ix;
     _LASTY=_GrMaxY-(*iy);
     if (_NEWPATH==1){
        fprintf(_outfile,"stroke\n");
        }
     fprintf(_outfile,"newpath\n");
     fprintf(_outfile,"%d %d moveto\n",*ix,*iy);
     _NEWPATH=1;
     }
void lineto_(int *ix,int *iy){
     GrLineSub(_LASTX,_LASTY,*ix,_GrMaxY-(*iy),_LCOLOR);
     _LASTX=(*ix);
     _LASTY=_GrMaxY-(*iy);
     XFlush(_DISPLAY);
     }
void arc_(int *ix,int *iy,int *ix1,int *iy1,int *ix2,int *iy2,int *ir){
     int r,it1,it2;
     r=2*(*ir);
     it1=atan2(*iy1,*ix1)*180/_PI*64;
     it2=atan2(*iy2,*ix2)*180/_PI*64;
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_LCOLOR]);
     XDrawArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxY-*iy,r,r,it1,it2);
     XFlush(_DISPLAY);
     XFlush(_DISPLAY);
     if (_NEWPATH==0){
        fprintf(_outfile,"stroke\n");
        }
     fprintf(_outfile,"newpath\n");
     fprintf(_outfile,"%d %d %d %d %d arc\n",*ix,*iy,*ir,it1/64,it2/64);
     fprintf(_outfile,"stroke\n");
     }
void sector_(int *ix,int *iy,int *ix1,int *iy1,int *ix2,int *iy2,int *ir){
     int r,it1,it2;
     r=2*(*ir);
     it1=atan2(*iy1,*ix1)*180/_PI*64;
     it2=atan2(*iy2,*ix2)*180/_PI*64;
     if (_PMODE!=0){
        XSetArcMode(_DISPLAY,_GCONTEXT,ArcPieSlice);
        XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_PCOLOR]);
        XFillArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxY-*iy,r,r,it1,it2);
        }
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_BLCOLOR]);
     XDrawArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxY-*iy,r,r,it1,it2);
     XFlush(_DISPLAY);
     }
void zigzag_(int *n,short *id){int i,ix,iy,ix2,iy2;
     XPoint d_data[*n+1];
     ix=*id;
     iy=_GrMaxY-*(id+1);
     for (i=0;i<=*n;i++) {
        d_data[i].x=*(id+i+i);
        d_data[i].y=_GrMaxY-*(id+i+i+1);
        }
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_LCOLOR]);
     XDrawLines(_DISPLAY,_WINDOW,_GCONTEXT,d_data,*n+1,CoordModeOrigin);
     _LASTX=*(id+2*(*n)+1);
     _LASTY=*(id+2*(*n)+2);
/*   for(i=2;i<=(*n)*2;i+=2){
        ix2=*(id+i);
        iy2=_GrMaxY-*(id+i+1);
        GrLineSub(ix,iy,ix2,iy2,_LCOLOR);
        ix=ix2;
        iy=iy2;
        }
     _LASTX=ix;
     _LASTY=iy; */
     XFlush(_DISPLAY);
     }
void linep_(int *n,int *id){
     }
void pcolor_(int *ic){
     _PCOLOR=*ic;
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_PCOLOR]);
     XDrawLine(_DISPLAY,_HATCH[1],_GCONTEXT,0,2,4,2);
     XDrawLine(_DISPLAY,_HATCH[5],_GCONTEXT,0,2,4,2);
     XDrawLine(_DISPLAY,_HATCH[2],_GCONTEXT,2,0,2,4);
     XDrawLine(_DISPLAY,_HATCH[5],_GCONTEXT,2,0,2,4);
     XDrawLine(_DISPLAY,_HATCH[3],_GCONTEXT,0,4,4,0);
     XDrawLine(_DISPLAY,_HATCH[6],_GCONTEXT,0,4,4,0);
     XDrawLine(_DISPLAY,_HATCH[4],_GCONTEXT,0,0,4,4);
     XDrawLine(_DISPLAY,_HATCH[6],_GCONTEXT,0,0,4,4);
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_LCOLOR]);
     }
void pmode_(int *m){
     _PMODE=*m;
     if (_PMODE==1) XSetFillStyle(_DISPLAY,_GCONTEXT,FillSolid);
     else if (_PMODE>1) XSetFillStyle(_DISPLAY,_GCONTEXT,FillTiled);
     }
/* accelerates GrPixel by utilizing already prepared _XIMAGE */
int _POINT_COLOR(int ix){
     int i;
     long pixel;
     pixel=_XIMAGE->data[_XIMAGE->xoffset+ix];
     for (i=0;i<16;i++) {
          if (_PIXEL[i]==pixel) break;
          }
     return i;
     }
/* test program called by FORTRAN as integer function POINT_COLOR_TEST(IX,IY)  */
int point_color_test__(int *ix,int *iy){
     return GrPixel(*ix,_GrMaxY-*iy);
     }
void paint_(int *ix,int *iy,int *ibc){
     int ixw,iyw,ibcw;
     if (_PMODE==0) return;
     ibcw=*ibc;
     iyw=_GrMaxY-(*iy);
     ixw=*ix;
     _paint_up(ixw,iyw,ibcw);
     _paint_down(ixw,iyw-1,ibcw);
     XFlush(_DISPLAY);
     }
void paint2_(int *ix,int *iy,int *ibc,int *m,int *ic){
     }
void _paint_up(int ix,int iy,int ibc){
     int ixwl,ixwr,iyw,ixwls,ixwrs,ixwlc,ixwrc,color;
     iyw=iy;
     ixwls=ix; ixwlc=ix;
     _XIMAGE=XGetImage(_DISPLAY,_WINDOW,0,iyw,_GrMaxX,1,AllPlanes,ZPixmap);
     while(_POINT_COLOR(ixwls)!=ibc && ixwls>=0) ixwls--;
     while(_POINT_COLOR(ixwlc)==_PCOLOR && ixwlc>=0) ixwlc--;
     ixwrs=ix; ixwrc=ix;
     while(_POINT_COLOR(ixwrs)!=ibc && ixwrs<=_GrMaxX) ixwrs++;
/* determines if already painted by the concordance of the total interval */
/* of the painting color with the interval found between the pair of points */
/* with the border color */
     while(_POINT_COLOR(ixwrc)==_PCOLOR && ixwrc<=_GrMaxX) ixwrc++;
     XDestroyImage(_XIMAGE);
     if (ixwrs<ixwls+2 || ixwls==ixwlc && ixwrs==ixwrc) return;
/*     GrLine(ixwls+1,iyw,ixwrs,iyw,_PCOLOR); */
     GrLine(ixwls+1,iyw,ixwrs-1,iyw,_PCOLOR);
     while(iyw<_GrMaxY) {
        iyw++;
        ixwl=ix; ixwlc=ix;
        _XIMAGE=XGetImage(_DISPLAY,_WINDOW,0,iyw,_GrMaxX,1,AllPlanes,ZPixmap);
        while(_POINT_COLOR(ixwl)!=ibc && ixwl>=0) ixwl--;
        while(_POINT_COLOR(ixwlc)==_PCOLOR && ixwlc>=0) ixwlc--;
        ixwr=ix; ixwrc=ix;
        while(_POINT_COLOR(ixwr)!=ibc && ixwr<=_GrMaxX) ixwr++;
        while(_POINT_COLOR(ixwrc)==_PCOLOR && ixwrc<=_GrMaxX) ixwrc++;
        XDestroyImage(_XIMAGE);
        if (ixwl<ixwls-1) {
            ixwlc=ixwls-1;
            while(ixwlc>ixwl){
                 while(((color=GrPixel(ixwlc,iyw-1))==ibc || color==_PCOLOR ) && ixwlc>ixwl+1) ixwlc--;
                 if (ixwlc>ixwl+1) _paint_down(ixwlc,iyw-1,ibc);
                 ixwlc--;
                 }
            }
        else if (ixwl>ixwls+1) {
            ixwlc=ixwl-1;
            while(ixwlc>ixwls){
                 while(((color=GrPixel(ixwlc,iyw))==ibc || color==_PCOLOR ) && ixwlc>ixwls+1) ixwlc--;
                 if (ixwlc>ixwls+1) _paint_up(ixwlc,iyw,ibc);
                 ixwlc--;
                 }
            }
        if (ixwr<ixwrs-1) {
            ixwrc=ixwr+1;
            while(ixwrc<ixwrs){
                 while(((color=GrPixel(ixwrc,iyw))==ibc || color==_PCOLOR ) && ixwrc<ixwrs-1) ixwrc++;
                 if (ixwrc<ixwrs-1) _paint_up(ixwrc,iyw,ibc);
                 ixwrc++;
                 }
            }
        else if (ixwr>ixwrs+1) {
            ixwrc=ixwrs+1;
            while(ixwrc<ixwr){
                 while(((color=GrPixel(ixwrc,iyw-1))==ibc || color==_PCOLOR ) && ixwrc<ixwr-1) ixwrc++;
                 if (ixwrc<ixwr-1) _paint_down(ixwrc,iyw-1,ibc);
                 ixwrc++;
                 }
            }
        if (ixwr<ixwl+2 || ixwl==ixwlc && ixwr==ixwrc) return;
/*        GrLine(ixwl+1,iyw,ixwr,iyw,_PCOLOR);  */
        GrLine(ixwl+1,iyw,ixwr-1,iyw,_PCOLOR);
        ixwls=ixwl; ixwrs=ixwr;
        }
     }
void _paint_down(int ix,int iy,int ibc){
     int ixwl,ixwr,iyw,ixwls,ixwrs,ixwlc,ixwrc,color;
     iyw=iy;
     ixwls=ix; ixwlc=ix;
     _XIMAGE=XGetImage(_DISPLAY,_WINDOW,0,iyw,_GrMaxX,1,AllPlanes,ZPixmap);
     while(_POINT_COLOR(ixwls)!=ibc && ixwls>=0) ixwls--;
     while(_POINT_COLOR(ixwlc)==_PCOLOR && ixwlc>=0) ixwlc--;
     ixwrs=ix; ixwrc=ix;
     while(_POINT_COLOR(ixwrs)!=ibc && ixwrs<=_GrMaxX) ixwrs++;
     while(_POINT_COLOR(ixwrc)==_PCOLOR && ixwrc<=_GrMaxX) ixwrc++;
     XDestroyImage(_XIMAGE);
     if (ixwrs<ixwls+2 || ixwls==ixwlc && ixwrs==ixwrc) return;
/*     GrLine(ixwls+1,iyw,ixwrs,iyw,_PCOLOR);*/
     GrLine(ixwls+1,iyw,ixwrs-1,iyw,_PCOLOR);
     while(iyw>0) {
        iyw--;
        ixwl=ix; ixwlc=ix;
        _XIMAGE=XGetImage(_DISPLAY,_WINDOW,0,iyw,_GrMaxX,1,AllPlanes,ZPixmap);
        while(_POINT_COLOR(ixwl)!=ibc && ixwl>=0) ixwl--;
        while(_POINT_COLOR(ixwlc)==_PCOLOR && ixwlc>=0) ixwlc--;
        ixwr=ix; ixwrc=ix;
        while(_POINT_COLOR(ixwr)!=ibc && ixwr<=_GrMaxX) ixwr++;
        while(_POINT_COLOR(ixwrc)==_PCOLOR && ixwrc<=_GrMaxX) ixwrc++;
        XDestroyImage(_XIMAGE);
        if (ixwl<ixwls-1) {
            ixwlc=ixwls-1; ixwrc=ixwls;
            while(ixwrc>ixwl){
                 while(((color=GrPixel(ixwlc,iyw+1))==ibc || color==_PCOLOR ) && ixwlc>ixwl+1) ixwlc--;
                 if (ixwlc>ixwl+1) _paint_up(ixwlc,iyw+1,ibc);
                 ixwrc=ixwlc; ixwlc--;
                 }
            }
        else if (ixwl>ixwls+1) {
            ixwlc=ixwl-1; ixwrc=ixwl;
            while(ixwlc>ixwls){
                 while(((color=GrPixel(ixwlc,iyw))==ibc || color==_PCOLOR ) && ixwlc>ixwls+1) ixwlc--;
                 if (ixwlc>ixwls+1) _paint_down(ixwlc,iyw,ibc);
                 ixwrc=ixwlc; ixwlc--;
                 }
            }
        if (ixwr<ixwrs-1) {
            ixwrc=ixwr+1; ixwlc=ixwr;
            while(ixwrc<ixwrs){
                 while(((color=GrPixel(ixwrc,iyw))==ibc || color==_PCOLOR ) && ixwrc<ixwrs-1) ixwrc++;
                 if (ixwrc<ixwrs-1) _paint_down(ixwrc,iyw,ibc);
                 ixwlc=ixwrc; ixwrc++;
                 }
            }
        else if (ixwr>ixwrs+1) {
            ixwrc=ixwrs+1; ixwlc=ixwrs;
            while(ixwrc<ixwr){
                 while(((color=GrPixel(ixwrc,iyw+1))==ibc || color==_PCOLOR ) && ixwrc<ixwr-1) ixwrc++;
                 if (ixwrc<ixwr-1) _paint_up(ixwrc,iyw+1,ibc);
                 ixwlc=ixwrc; ixwrc++;
                 }
            }
        if (ixwr<ixwl+2 || ixwl==ixwlc && ixwr==ixwrc) return;
/*        GrLine(ixwl+1,iyw,ixwr,iyw,_PCOLOR); */ 
        GrLine(ixwl+1,iyw,ixwr-1,iyw,_PCOLOR);
        ixwls=ixwl; ixwrs=ixwr;
        }
     }
void hatchs_(int *is){
     _HATCHSTYLE=*is;
     XSetTile(_DISPLAY,_GCONTEXT,_HATCH[*is]);
     }
void tiles_(int *is){
     _TILESTYLE=*is;
     }
void blmode_(int *m){
     _BLMODE=*m;
     }
void blstyl_(int *is){
     _BLSTYLE=*is;
     }
void blcolr_(int *ic){
     _BLCOLOR=*ic;
     }
void box_(int *ix1,int *iy1,int *ix2,int *iy2){
     int iy,iy0,i;
     iy0=_GrMaxY-(*iy1);
     iy=_GrMaxY-(*iy2);
     if (_PMODE!=0) {
          XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_PCOLOR]);
          XFillRectangle(_DISPLAY,_WINDOW,_GCONTEXT,*ix1,iy,*ix2-*ix1,iy0-iy);
          XFlush(_DISPLAY);
          }
     fprintf(_outfile,"stroke\n");
/*     lstyle_(&_BLSTYLE); */
     GrLineSub(*ix1,iy0,*ix2,iy0,_BLCOLOR);
     GrLineSub(*ix1,iy,*ix2,iy,_BLCOLOR);
     GrLineSub(*ix1,iy0,*ix1,iy,_BLCOLOR);
     GrLineSub(*ix2,iy0,*ix2,iy,_BLCOLOR);
/*     lstyle_(&_LSTYLE); */
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_LCOLOR]);
     XFlush(_DISPLAY);
     }
void circle_(int *ix,int *iy,int *ir){char c;
     int r;
     r=2*(*ir);
     if (_PMODE!=0){
        XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_PCOLOR]);
        XFillArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix-*ir,_GrMaxY-*iy-*ir,r,r,0,360*64);
        }
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_BLCOLOR]);
     XDrawArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix-*ir,_GrMaxY-*iy-*ir,r,r,0,360*64);
     XFlush(_DISPLAY);
     if (_NEWPATH==0){
        fprintf(_outfile,"stroke\n");
        }
     fprintf(_outfile,"newpath\n");
     fprintf(_outfile,"%d %d %d 0 360 arc\n",*ix,*iy,*ir);
     fprintf(_outfile,"stroke\n");
     }
void ellips_(int *ix,int *iy,int *irx,int *iry){
     int r1,r2;
     r1=2*(*irx);
     r2=2*(*iry);
     if (_PMODE!=0){
        XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_PCOLOR]);
        XFillArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix-*irx,_GrMaxY-*iy-*iry,r1,r2,0,360*64);
        }
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[_BLCOLOR]);
     XDrawArc(_DISPLAY,_WINDOW,_GCONTEXT,*ix-*irx,_GrMaxY-*iy-*iry,r1,r2,0,360*64);
     XFlush(_DISPLAY);
     }
void pset_(int *ix,int *iy,int *ic){
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[*ic]);
     XDrawPoint(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxY-(*iy));
     XFlush(_DISPLAY);
     }
void tilptn_(int *m,int *n,int *id){
     }
void hchptn_(int *m,int *n,int *id){
     }
void chcolr_(int *ic){
     _CHCOLOR=*ic;
     }
void charac_(int *ix,int *iy,char *moji){
     int i;
     char jis[100];
     Font ft;

     XSetForeground(_DISPLAY,_GCONTEXT, _PIXEL[_CHCOLOR]);
/*     XSetBackground(_DISPLAY,_GCONTEXT, _PIXEL[_GCOLOR]); */
     if (((*moji & 128)==0) && (*moji>=0x20)) {
         ft=XLoadFont(_DISPLAY,"12x24");
         XSetFont(_DISPLAY,_GCONTEXT,ft);
         XDrawString(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxY-*iy,moji,strlen(moji));
         fprintf(_outfile,"/Courier-Bold findfont 24 scalefont setfont\n");
         fprintf(_outfile,"%d %d moveto\n(",*ix,*iy);
         for (i=0;i<strlen(moji);i++){
             fprintf(_outfile,"%c",*(moji+i));
             }
         fprintf(_outfile,") show\n");
         }
     else {
         _transkj(jis,moji,0);
         ft=XLoadFont(_DISPLAY,"kanji24");
         XSetFont(_DISPLAY,_GCONTEXT,ft);
         XDrawString16(_DISPLAY,_WINDOW,_GCONTEXT,*ix,_GrMaxY-*iy,(XChar2b *)moji,strlen(moji)/2);
         fprintf(_outfile,"/Ryumin-Light-H findfont 24 scalefont setfont\n");
         fprintf(_outfile,"%d %d moveto\n(",*ix,*iy);
         for (i=0;i<strlen(jis);i++){
             fprintf(_outfile,"%c",jis[i]);
             }
         fprintf(_outfile,") show\n");
         }
     XFlush(_DISPLAY);
     } 
void csrdef_(int *ic){
     }
void csrmod_(int *m){
     }
void cursor_(int *ix,int *iy){
     }
void csrptn_(int *ic,int *ix,int *iy,int *ixn,int *iyn,int *id){
     }
int kgcolr_(void){
     return _GCOLOR;
     }
int kdspsc_(void){
     return _DSPSCREEN;
     }
int kwrtsc_(void){
     return _WRTSCREEN;
     }
int kupgno_(void){
     return 1;
     }
void GrLineSub(int ix,int iy,int ix2,int iy2,int ic){
     int igx1,igy1,igx2,igy2; int c;
     int igx2c,igy2c;
     if(scissor(ix,iy,ix2,iy2,&igx1,&igy1,&igx2,&igy2) != 0) return;
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[ic]);
     igx2c=igx2; igy2c=igy2;
     XDrawLine(_DISPLAY,_WINDOW,_GCONTEXT,igx1,igy1,igx2,igy2);
     fprintf(_outfile,"%d %d lineto\n",igx2c,_GrMaxY-igy2c);
     }
void GrLine(int ix,int iy,int ix2,int iy2,int ic){
     XSetForeground(_DISPLAY,_GCONTEXT,_PIXEL[ic]);
     XDrawLine(_DISPLAY,_WINDOW,_GCONTEXT,ix,iy,ix2,iy2);
     }
int scissor(int ix,int iy,int ix2,int iy2,\
            int *igx1,int *igy1,int *igx2,int *igy2){
     float xdif,ydif,yoverx,xovery;
     int igx1c,igy1c,igx2c,igy2c;
     if(ix>ix2){igx1c=ix2;igy1c=iy2;igx2c=ix;igy2c=iy;}
     else {igx1c=ix;igy1c=iy;igx2c=ix2;igy2c=iy2;}
     if(igx1c>=0 && igx2c>=0 && igx1c<=_GrMaxX && igx2c<=_GrMaxX && \
        igy1c>=0 && igy2c>=0 && igy1c<=_GrMaxY && igy2c<=_GrMaxY) {
           *igx1=ix;*igy1=iy;*igx2=ix2;*igy2=iy2; return 0;
           }
     if(igx2c<0 || igx1c>_GrMaxX) return -1;
     xdif=igx2c-igx1c; ydif=igy2c-igy1c;
     if(xdif!=0) yoverx=ydif/xdif;
     if(ydif!=0) xovery=xdif/ydif;
     if(igx1c<0){igy1c=igy1c+yoverx*(-igx1c);igx1c=0;}
     if(igx2c>_GrMaxX){igy2c=igy1c+yoverx*(_GrMaxX-igx1c);igx2c=_GrMaxX;}
     if(igy1c<0 && igy2c<0 || igy1c>_GrMaxY && igy2c>_GrMaxY) return -1;
     if(igy1c<0) {igx1c=igx1c+xovery*(-igy1c);igy1c=0;}
     if(igy2c<0) {igx2c=igx2c+xovery*(-igy2c);igy2c=0;}
     if(igy1c>_GrMaxY) {igx1c=igx1c+xovery*(_GrMaxY-igy1c);igy1c=_GrMaxY;}
     if(igy2c>_GrMaxY) {igx2c=igx2c+xovery*(_GrMaxY-igy2c);igy2c=_GrMaxY;}
     if(ix>ix2){*igx1=igx2c;*igy1=igy2c;*igx2=igx1c;*igy2=igy1c;}
     else {*igx1=igx1c;*igy1=igy1c;*igx2=igx2c;*igy2=igy2c;}
     return 0;
     }
int max(int i,int j){
    if (i>=j) return i;
    else return j;
    }
int min(int i,int j){
    if (i<=j) return i;
    else return j;
    }
double random_(int *i){
static long INI_=123456789;    /* detarame na kazu */
static long LAML_=63149;
static long LAMH_=12867;
static long MUL_=45429;
static long MUH_=6924;
static long POWER0_=65536;
static double POWER2_=65536.0;
static long RANL_;
static long RANH_;
      double X,Y; float RANDOM;
      if (*i>0) {RANL_=dmod_((double)*i,(double)POWER0_);
		RANH_=*i/POWER0_;}
      else if (INI_!=0) {INI_=0;
                       RANL_=0;
                       RANH_=0;}
      X=RANL_;
      Y=RANH_;
      Y=dmod_(Y*LAML_+X*LAMH_+MUH_,POWER2_);
      X=X*LAML_+MUL_;
      RANL_=dmod_(X,POWER2_);
      RANH_=dmod_(Y+(long)(X/POWER2_),POWER2_);
      RANDOM=((float)RANH_)/POWER2_+((float)RANL_)/POWER2_/POWER2_;
/*      printf("%f",RANDOM); */
      return (float)RANDOM;
      }
double dmod_(double x,double y){
     return x-(long)(x/y)*y;
     }
int GrPixel(int ix,int iy) {
     int i;
     long pixel;
     XImage *image;
     if (ix<0 || iy<0 || ix>_GrMaxX ||iy>_GrMaxY) return -1;
     image=XGetImage(_DISPLAY,_WINDOW,ix,iy,1,1,AllPlanes,ZPixmap);
     pixel=image->data[image->xoffset];
     for (i=0;i<16;i++) {
          if (_PIXEL[i]==pixel) break;
          }
     XDestroyImage(image);
     return i;
     }
void _transkj(char *jis,char *euc,int ind){
     int i,k;
     k=strlen(euc);
     for (i=0;i<k;i++){
          *(jis+i)=*(euc+i) & 0x7f;
          }
     *(jis+k)=0;
     }
void lwidth_(int *iw){
     fprintf(_outfile,"%d setlinewidth\n",*iw);
     }
void closex_(void){
     if (_NEWPATH==1) {
        fprintf(_outfile,"stroke\n");
        }
/*     fprintf(_outfile,"end\n"); */
/*     fprintf(_outfile,"showpage\n"); */
     fprintf(_outfile,"%%%%Trailer\n");
     fclose(_outfile);
     }

