c 94,12,28  add X11 window gray_scale
	subroutine pgxgray(map,n1,n2,nx1,nx2,ny1,ny2,white,black,irat)
c rat for hp, if rat.le.1,then soltion=9; if rat.ge.2, solutin=19
	integer*2 map(n1,n2)
	character*16 c
	call pgqinf('TYPE',c,k)
	if(nx2.le.nx1.or.ny2.le.ny1)goto 100
	if(n1.le.1.or.n2.le.1)goto 100
	if(black.eq.white)goto 100
	if(c(1:2).eq.'XW'.or.c(1:2).eq.'JG')then
        call pg_xw_gray(map,n1,n2,nx1,nx2,ny1,ny2,black,white)
	endif
	if(c(1:2).eq.'HP'.or.c(1:2).eq.'LH'.or.c(1:2).eq.'JP')then
	call pg_hp_gray(map,n1,n2,nx1,nx2,ny1,ny2,white,black,irat)
	endif
     	return
100	write(*,*)
	write(*,*)'bad parameter for "pg_gray"'
	write(*,*)'pg_gray(map,n1,n2,nx1,nx2,ny1,ny2,white,black,irat)'
	write(*,*)'        INTEGER*2 map(n1,n2)'
	write(*,*)'        INTEGER nx1,nx2,ny1,ny2      ! X,Y range' 
	write(*,*)'        REAL white,black             ! gray cutoff'
	write(*,*)'        INTEGER irat         !for "HP" only, =1 or 2' 
	stop
	end

	subroutine pg_hp_gray(map,n1,n2,nx1,nx2,ny1,ny2,white,black,irat)
	integer*2 map(n1,n2)
	real rbuf(6)
	character*1 cdummy
	character*16 c
	call pgqinf('TYPE',c,k)
	call pgqwin(x1,x2,y1,y2)
	call grtxy0(.false.,x1,y1,x1,y1)
        ix1=nint(x1)
        iy1=nint(y1)
	call grtxy0(.false.,x2,y2,x2,y2)
	ix2=nint(x2)
	iy2=nint(y2)
	sx=(nx2-nx1)/(x2-x1)
	sy=(ny2-ny1)/(y2-y1)
c
	ip=2
	scale=8.
	if(irat.ge.2)ip=3
	if(irat.ge.2)scale=18.
	ip2=ip+ip
	ip05=ip/2
        if(ix2.gt.ix1)then
                ix11=ix1+ip
                ix22=ix2-ip05
                ixstep=ip
        else
                ix11=ix1-ip
                ix22=ix2+ip05
                ixstep=-ip
        endif
        if(iy2.gt.iy1)then
                iy11=iy1+ip
                iy22=iy2-ip05
                iystep=ip2
                iyd=ip
        else
                iy11=iy1-ip
                iy22=iy2+ip05
                iystep=-ip2
                iyd=-ip
        endif
c
        ratio=scale/(black-white)
        k=-1
        do 10 ix=ix11,ix22,ixstep
        k=-k
        kd=0
        if(k.eq.1)kd=iyd
	dx=sx*(ix-ix1)
	i1=dx
	i1=i1+nx1
	i2=i1+1
	x2=mod(dx,1.)
	x1=1.-x2
	rbuf(1)=ix
        do 10 iy=iy1+kd,iy22,iystep
	rbuf(2)=iy
	dy=sy*(iy-iy1)
	j1=dy
	j1=j1+ny1
	j2=j1+1
	y2=mod(dy,1.)
	y1=1.-y2
	z=map(i1,j1)*x1*y1+map(i1,j2)*x1*y2+
     *    map(i2,j1)*x2*y1+map(i2,j2)*x2*y2
        rbuf(3)=ratio*(z-white)
	rbuf(4)=scale
	if(c(1:2).eq.'HP')call hpdriv(28,rbuf,6,cdummy,1)
	if(c(1:2).eq.'LH')call lhdriv(28,rbuf,6,cdummy,1)
	if(c(1:2).eq.'JP')call jpdriv(28,rbuf,6,cdummy,1)
10	continue
        end
        
	subroutine pg_xw_gray(map,n1,n2,nx1,nx2,ny1,ny2,white,black)
	integer*2 map(n1,n2)
	character dummy
	character*16 c
	real rbuf(900)
	call pgqinf('TYPE',c,k)
	n16=84
	i1=1
	call pgqwin(x1,x2,y1,y2)
	call grtxy0(.false.,x1,y1,x1,y1)
	ibx1=nint(x1)
	iby1=nint(y1)
	call grtxy0(.false.,x2,y2,x2,y2)
	ibx2=nint(x2)
	iby2=nint(y2)
	sx=(nx2-nx1)/(x2-x1)
	sy=(ny2-ny1)/(y2-y1)
	d=n16/(black-white)
	rbuf(1)=ibx1
	do 20 j=iby1,iby2
	dy=sy*(j-iby1)
	j1=dy				! cannot be j1=dy+ny1
	j1=j1+ny1
	j2=j1+1
	if(j2.gt.n2)j2=n2
	y2=mod(dy,1.)
	y1=1.-y2	
	rbuf(2)=j
	i6=2
	do 10 i=ibx1,ibx2
	dx=sx*(i-ibx1)
	i1=dx
	i1=i1+nx1
	i2=i1+1
	if(i2.gt.n1)i2=n1
	x2=mod(dx,1.)
	x1=1.-x2
	z=map(i1,j1)*x1*y1+map(i1,j2)*x1*y2+
     *    map(i2,j1)*x2*y1+map(i2,j2)*x2*y2
	k=nint(d*(z-white))
	if(k.lt.1)k=1
	if(k.gt.n16)k=n16
	i6=i6+1
10	rbuf(i6)=k+15
	if(c(1:2).eq.'XW')then
	call xwdriv(26,rbuf,i6,dummy)
	call pgebuf
	endif
20	if(c(1:2).eq.'JG')call jgdriv(26,rbuf,i6,dummy)
	end
