!c  disbytefile - fortran version

      character*60 str,file
      integer statb(13),fstat
      integer*1 data(65536)
      integer*1, allocatable :: rgb(:,:,:)
      integer*1 b255

      b255=-1

      if(iargc().lt.2)then
         print *,'Usage: disbytefile infile length <firstline=0> <nlines> <flipflag=0> <min=0> <max=255> <flipflag=0>'
         stop
      end if

      call getarg(1,file)
      call getarg(2,str)
      read(str,*)len
      ifirst=1
      if(iargc().ge.3)then
         call getarg(3,str)
         read(str,*)ifirst
      end if
      lines=10000000
      if(iargc().ge.4)then
         call getarg(4,str)
         read(str,*)lines
      end if
      iflipflag=0
      if(iargc().ge.5)then
         call getarg(5,str)
         read(str,*)iflipflag
      end if
      imin=0
      if(iargc().ge.6)then
         call getarg(6,str)
         read(str,*)imin
      end if
      imax=255
      if(iargc().ge.7)then
         call getarg(7,str)
         read(str,*)imax
      end if
      range=255./(imax-imin)

      open(21,file=file,access='direct',recl=len)
      ierr=fstat(21,statb)
      nd=statb(8)/len
      write(*,*)'Lines in file: ',nd
      if(nd.lt.ifirst+lines-1)lines=nd-ifirst+1

!c  allocate rgb array
      allocate (rgb(len,lines,4))

!c  now process each line
      do i=ifirst,ifirst+lines-1
         read(21,rec=i)(data(k),k=1,len)
         do j=1,len
            k=float(iand(data(j),255)-imin)*range
            if(k.lt.0)k=0
            if(k.gt.255)k=255

            rgb(j,i-ifirst+1,1)=k
            rgb(j,i-ifirst+1,2)=k
            rgb(j,i-ifirst+1,3)=k
            rgb(j,i-ifirst+1,4)=b255
            if(k.le.0)rgb(j,i-ifirst+1,4)=0
         end do
      end do

!c  save as a tiff
      call writetiff(rgb,len,lines,'disbyte.tif')

      end


