FFLAGS = -O -Zf -Zg 
#LIBS = -lrcgos -lmlib -lutil -lgrafixf -lvdif -lgrafix 

OBJECT = blackwhite.o change.o check.o conrgb.o dmpbuff.o grayscale.o headrgb.o initkeys.o makemap.o pacbuff.o pltras.o readras.o tekrgb.o

change: $(OBJECT)
	cc $(DEBUG) $(FFLAGS) -o change $(OBJECT) $(LIBS)

clean:
	rm -f *.o made

.c.o:
	cc $(DEBUG) $(FFLAGS) -c $*.c
/* end Makefile */

#include <gl.h>
#include <device.h>
#include <stdio.h>

blackwhite(paper)
Boolean paper;
{
/*
        Brent L. Bates
        NASA-Langley Research Center
        M.S. 294
        Hampton, Virginia  23665-5225
        (804) 864-2854
        E-mail: blbates@aero4.larc.nasa.gov or blbates@aero2.larc.nasa.gov
*/
   static Boolean fileopen=FALSE;
   register short y;
   register unsigned short i,temp,x;
   unsigned char value;
   char fileout[81];
   static FILE *ounit;
   extern char outfile[81];
   extern RGBvalue image[1024][3][1280];
   extern Scoord xlen,ylen;
   ringbell();
   if(!fileopen) {
      strcpy(fileout,outfile);
      strcat(fileout,"bw.ps");
      ounit=fopen(fileout,"w");
      fseek(ounit,0L,0);
      fileopen=TRUE;
      if(!paper) fprintf(ounit,"%c!\nshowpage\n",'%');
   }
   fprintf(ounit,"/picstr %d string def\n",ylen);
   if(paper) {
      fprintf(ounit,"15.0 20.0 translate\n");
      fprintf(ounit,"0.0 rotate\n");
      fprintf(ounit,"%f %f scale\n",0.407*1024/xlen,
                                    0.407*1024/xlen);
   }
   else {
      fprintf(ounit,"15.0 20.0 translate\n");
      fprintf(ounit,"90.0 rotate\n");
      fprintf(ounit,"%f %f scale\n",0.74*1024/xlen,
                                    0.74*1024/xlen);
   }
   fprintf(ounit,"%d %d 1 [1 0 0 -1 0 1]",4*(xlen/4),ylen);
   fprintf(ounit,"{currentfile picstr readhexstring pop}\n");
   fprintf(ounit,"image\n");
   for(y=ylen-1;y>=0;y--) {
      for(x=0;x<xlen;x+=4) {
         value=0;
         for(i=1;i<=4;i++) {
            temp=x+i;
            value=value<<1;
            if(image[y][0][temp]==0 && image[y][1][temp]==0 &&
               image[y][2][temp]==0) value=value|1;
         }
         if(value<10) {
            value+=48;
         }
         else {
            value+=87;
         }
         fwrite(&value,sizeof value,1,ounit);
      }
      fprintf(ounit,"\n");
   }
   if(!paper) fprintf(ounit,"showpage\n");
}
#include <gl.h>
#include <device.h>
#include <math.h>
#include <stdio.h>

/* Globals */

Boolean mex,overlayflag,picture;
char outfile[81];
unsigned char bbck,gbck,rbck;
Colorindex map[10][10][10],mapimage[1024][1280];
float cmapscale;
long oldwin,winchange;
long confirm,mainmenu,overlays,page,pageA;
RGBvalue image[1024][3][1280];
Scoord xlen,ylen;

main()
{
   Boolean endofile,ex;
   char command[13],infile[81],scratch[10],test;
   FILE *inunit;
   extern char outfile[81];
   extern Boolean mex,overlayflag,picture;
   strcpy(scratch,"TMPXXXXXX");
   mktemp(scratch);
   if((inunit=fopen(scratch,"w"))==NULL) {
      printf("Can not open file for output. Continue anyways? ");
      test=getchar();
      if( test=='n' || test=='N' ) exit();
   }
   else{
      close(inunit);
      strcpy(command,"rm ");
      strcat(command,scratch);
      system(command);
   }
   mex=ismex();
   printf("Lamps show program status:\n");
   printf("L1 - Reading File       L1 & L2 - Plotting Image\n");
   printf("L2 - Celco Dump\n");
   printf("L3 - Tektronix Dump     L4 - Postscript Dump\n\n");
   if(!mex) {
      printf("When image is drawn and console beeps:\n");
      printf("    left mouse button:  convert to celco format\n");
      printf("  middle mouse button:  convert to tektronix format\n");
      printf("   right mouse button:  skip to next image\n");
      printf("           b or w key:  convert to b/w postscript\n");
      printf("                g key:  convert to grayscale postscript\n");
      printf("                o key:  overlay next frame on current\n");
      printf("           q or e key:  quit/exit\n");
   }
inputfile:
   printf("Enter input file name: ");
   scanf("%80s",infile);
   if((inunit=fopen(infile,"r"))==NULL){
      printf("Input file does not exist!  Please enter new name.\n");
      goto inputfile;
   }
   fseek(inunit,0L,0);
   printf("Enter output file name: ");
   scanf("%80s",outfile);
   endofile=FALSE;
   overlayflag=FALSE;
   picture=FALSE;
   pltras();
   qreset();
noeof:
   endofile=readras(inunit,endofile);
/*      inunit=10 */
   qreset();
   if(!endofile) goto noeof;
   gflush();
   lampoff(15);
   singlebuffer();
   gconfig();
   greset();
   gexit();
   fclose(inunit);
}
#include <gl.h>
#include <device.h>
#include <stdio.h>
check()
{
   Boolean endofile,inunitopen;
   static Boolean currentwin=TRUE;
   char newfile[81];
   FILE *inunit;
   long dev,nullwin;
   short val;
   extern Boolean mex,overlayflag;
   extern long confirm,mainmenu,oldwin,overlays,winchange;
   inunitopen=FALSE;
   ringbell();
   gflush();
   if(mex) {
/*
  MEX Mode
*/
      while(1) {
         if(qtest()==0) {
            swapbuffers();
         }
         else {
            dev=qread(&val);
            if(dev==REDRAW&&val==winchange) {
               reshapeviewport();
               pltras();
               ringbell();
               qreset();
            }
            else if(dev==INPUTCHANGE) {
               if(val==winchange) {
                  currentwin=TRUE;
               }
               else {
                  currentwin=FALSE;
               }
	    }
            else if(currentwin && dev==RIGHTMOUSE && val>0) {
               dev=dopup(mainmenu);
               if(dev==10) {
/*    Advance to Next Frame */
                  dev=dopup(confirm);
                  if(dev==901) {
                     ringbell();
                     qreset();
                     return;
                  }
	       }
               else if(dev==20) {
/*    Celco Dump */
                  lampon(2);
                  conrgb();
                  ringbell();
                  lampoff(15);
               }
               else if(dev==30) {
/*    Tektronix Dump */
                  lampon(4);
                  tekrgb();
                  ringbell();
                  lampoff(15);
               }
               else if(dev==40) {
/*    Black & White Postscript (full page size)*/
                  lampon(8);
                  blackwhite(FALSE);
		  ringbell();
                  lampoff(15);
               }
               else if(dev==41) {
/*    Black & White Postscript (paper figure size, no showpages)*/
                  lampon(8);
                  blackwhite(TRUE);
                  ringbell();
                  lampoff(15);
               }
               else if(dev==50) {
/*    Grayscale Postscript (normal, full page size) */
                  lampon(8);
                  grayscale(FALSE,FALSE);
                  ringbell();
                  lampoff(15);
               }
               else if(dev==51) {
/*    Grayscale Postscript (inverse grayscale, full page size) */
                  lampon(8);
                  grayscale(TRUE,FALSE);
                  ringbell();
                  lampoff(15);
               }
               else if(dev==52) {
/*    Grayscale Postscript (normal, paper figure size, no showpages) */
                  lampon(8);
                  grayscale(FALSE,TRUE);
                  ringbell();
                  lampoff(15);
               }
               else if(dev==53) {
/*    Grayscale Postscript (inverse gray, paper figure size, no showpages) */
                  lampon(8);
                  grayscale(TRUE,TRUE);
                  ringbell();
                  lampoff(15);
               }
               else if(dev==60) {
/*    Overlay Frames */
                  dev=dopup(overlays);
                  if(dev==61) {
                     overlayflag=TRUE;
                  }
                  else if(dev==62||dev==63) {
                     winset(winchange);
                     oldwin=winattach();
                     winpush();
                     noport();
                     nullwin=winopen("null");
                     oldwin=winattach();
                     winclose(nullwin);
inputfile:
                     printf("Enter second file name:");
                     scanf("%80s",newfile);
                     if((inunit=fopen(newfile,"r"))==NULL) {
                        printf("Input file does not exist!  Please enter new name.\n");
                        goto inputfile;
                     }
                     fseek(inunit,0L,0);
		     inunitopen=TRUE;
                     winset(winchange);
                     oldwin=winattach();
                     winpop();
                     qreset();
                     if(dev==62) {
                        endofile=FALSE;
                        overlayflag=TRUE;
                        endofile=readras(inunit,endofile);
                        if(endofile) fclose(inunit);
                     }
                     else {
                        endofile=FALSE;
                        overlayflag=FALSE;
                        endofile=readras(inunit,endofile);
                        if(endofile) fclose(inunit);
                        overlayflag=TRUE;
                     }
                  }
                  ringbell();
               }
               else if(dev==70) {
/*    Close Input File */
                  dev=dopup(confirm);
                  if(dev==901) {
		     if(inunitopen) {
                        fclose(inunit);
		     }
                     ringbell();
                     qreset();
                     return;
                  }
               }
               else if(dev==900) {
/*    Exit Program */
                  dev=dopup(confirm);
                  if(dev==901) {
                     gflush();
                     lampoff(15);
                     singlebuffer();
                     gconfig();
                     greset();
                     gexit();
                     exit(0);
                  }
                  qreset();
               }
            }
         }
      }
   }
   else {
/*
c RGB mode
*/
      if(getbutton(RIGHTMOUSE)) {
/* Advance to Next Frame  */
         ringbell();
         return;
      }
      if(getbutton(LEFTMOUSE)) {
/* Celco Dump  */
         lampon(2);
         conrgb();
         ringbell();
         lampoff(15);
      }
      if(getbutton(MIDDLEMOUSE)) {
/* Tektronix Dump  */
         lampon(4);
         tekrgb();
         ringbell();
         lampoff(15);
      }
      if(qtest()!=0) {
         dev=qread(val);
         if((dev==BKEY||dev==WKEY)&&val>0) {
/* Black & White Postscript  */
            lampon(8);
            blackwhite();
            ringbell();
            lampoff(15);
         }
         else if(dev==GKEY&&val>0) {
/* Grayscale Postscript  */
            lampon(8);
            grayscale();
            ringbell();
            lampoff(15);
         }
         else if(dev==OKEY&&val>0) {
/* Overlay Frames  */
            overlayflag=TRUE;
/*           else if(dev==62||dev==63) {
              winset(winchange)
              oldwin=winatt()
              winpus
              noport
              nullwin=winope('null',4)
              oldwin=winatt()
              winclo(nullwin)
              write(*,'(''Enter second file name:'',$)')
              read(*,'(80a)') newfile
              open(inunit,file=newfile,form='binary',
    .                 status='old')
              winset(winchange)
              oldwin=winatt()
              winpop
              qreset
              if(dev==62) {
                 endofile=.false.
                 overlayflag=.true.
                 readras(inunit,endofile)
                 if(endofile) close(inunit)
               }
              else
                 endofile=.false.
                 overlayflag=.false.
                 readras(inunit,endofile)
                 if(endofile) close(inunit)
                 overlayflag=.true.
              }
/*         }
            ringbell();
         }
         else if((dev==QKEY||dev==EKEY)&&val==0) {
/*  Exit Program  */
            gflush();
            lampoff(15);
            singlebuffer();
            gconfig();
            greset();
            gexit();
            exit(0);
         }
      }
   }
}
conrgb()
{
}
dmpbuff()
{
}
#include <gl.h>
#include <device.h>
#include <stdio.h>

grayscale(inverse,paper)
Boolean inverse,paper;
{
/*
         Brent L. Bates
         NASA-Langley Research Center
         M.S. 294
         Hampton, Virginia  23665-5225
         (804) 864-2854
         E-mail: blbates@aero4.larc.nasa.gov or blbates@aero2.larc.nasa.gov
*/
   static Boolean fileopen=FALSE;
   static unsigned char mask=0x0f;
   register short y;
   register unsigned short x;
   register unsigned char value;
   unsigned char vtemp;
   char fileout[81];
   static FILE *ounit;
   register float b,g,r;
   extern char outfile[81];
   extern RGBvalue image[1024][3][1280];
   extern Scoord xlen,ylen;
   ringbell();
   if(!fileopen) {
      strcpy(fileout,outfile);
      strcat(fileout,"gr.ps");
      ounit=fopen(fileout,"w");
      fseek(ounit,0L,0);
      fileopen=TRUE;
      if(!paper) fprintf(ounit,"%c!\nshowpage\n",'%');
/*      fprintf(ounit,"/spotproc {abs 1 sub dup mul exch abs 1 sub dup");
      fprintf(ounit," mul add 1 exch sub dup mul 2 mul 1 exch sub} def\n");
      fprintf(ounit,"20.0 45.0 /spotproc load setscreen\n");  */
   }
   fprintf(ounit,"/picstr %d string def\n",ylen);
   if(paper) {
      fprintf(ounit,"15.0 20.0 translate\n");
      fprintf(ounit,"0.0 rotate\n");
      fprintf(ounit,"%f %f scale\n",0.407*1024/xlen,
                                    0.407*1024/xlen);
   }
   else {
      fprintf(ounit,"15.0 20.0 translate\n");
      fprintf(ounit,"90.0 rotate\n");
      fprintf(ounit,"%f %f scale\n",0.74*1024/xlen,
                                    0.74*1024/xlen);
   }
   fprintf(ounit,"%d %d 8 [1 0 0 -1 0 1]",xlen,ylen);
   fprintf(ounit,"{currentfile picstr readhexstring pop}\n");
   fprintf(ounit,"image\n");
   for(y=ylen-1;y>=0;y--) {
      value=0;
      for(x=0;x<xlen;x++) {
         r=(float)image[y][0][x]*0.30;
         g=(float)image[y][1][x]*0.59;
         b=(float)image[y][2][x]*0.11;
         if(inverse) {
            value=(int)(255.5-(r+g+b));
         }
	 else {
            value=(int)(r+g+b+0.5);
         }
         vtemp=(value>>4)&mask;
         if(vtemp<10) {
            vtemp+=48;
         }
         else {
            vtemp+=87;
         }
         fwrite(&vtemp,sizeof vtemp,1,ounit);
         vtemp=value&mask;
         if(vtemp<10) {
            vtemp+=48;
         }
         else {
            vtemp+=87;
         }
         fwrite(&vtemp,sizeof vtemp,1,ounit);
      }
      fprintf(ounit,"\n");
   }
   if(!paper) fprintf(ounit,"showpage\n");
}
headrgb()
{
}
#include <gl.h>
#include <device.h>
#include <math.h>
#include <stdio.h>

initkeys()
{
   extern long confirm,mainmenu,overlays,page,pageA;
   extern Boolean mex;

   if(mex) {
      qdevice(INPUTCHANGE);
      qdevice(REDRAW);
      qdevice(RIGHTMOUSE);
      page=newpup();
      addtopup(page,"Black & White Postscript %t%x40");
      addtopup(page,"Page size (landscape) %x40");
      addtopup(page,"Figure size (portrate) %x41");
      pageA=newpup();
      addtopup(pageA,"Grayscale Postscript %t%x50");
      addtopup(pageA,"Page size (landscape) %x50");
      addtopup(pageA,"Page size (landscape), inverse gray %x51");
      addtopup(pageA,"Figure size (portrate) %x52");
      addtopup(pageA,"Figure size (portrate), inverse gray %x53");
      confirm=newpup();
      addtopup(confirm,"Confirm %t");
      addtopup(confirm," Do it. I'm sure. %x901");
      addtopup(confirm,"Nah... forget it. %x902");
      mainmenu=newpup();
      addtopup(mainmenu,"Change Menu %t");
      addtopup(mainmenu,"Advance to Next Frame %x10");
      addtopup(mainmenu,"Celco Format %x20");
      addtopup(mainmenu,"Tektronix 4693D Format %x30");
      addtopup(mainmenu,"Black & White Postscript %m%x40",page);
      addtopup(mainmenu,"Grayscale Postscript %m%x50",pageA);
      addtopup(mainmenu,"Overlay Frames %x60");
      addtopup(mainmenu,"Exit Program %x900");
      addtopup(mainmenu,"Close Input File %x70");
      overlays=newpup();
      addtopup(overlays,"Overlay Submenu %t");
      addtopup(overlays,"Overlay Next Frame %x61");
      addtopup(overlays,"Overlay Another File on Last Frame %x62");
      addtopup(overlays,"Overlay Next Frame on Another File %x63");
   }
   else {
      qdevice(BKEY);
      qdevice(EKEY);
      qdevice(GKEY);
      qdevice(OKEY);
      qdevice(QKEY);
      qdevice(WKEY);
   }
}
#include <gl.h>
#include <device.h>
#include <math.h>
#include <stdio.h>

makemap()
{
   Colorindex i=23;
   short blue,green,iblue,igreen,ired,max=9,red;
   extern Colorindex map[10][10][10];
   extern Boolean mex;
   extern float cmapscale;
   cmapscale=(float) max/255.0;
   for(iblue=0;iblue<=max;iblue++){
      for(igreen=0;igreen<=max;igreen++){
         for(ired=0;ired<=max;ired++,i++){
            red=(short)(ired/cmapscale+0.5);
            green=(short)(igreen/cmapscale+0.5);
            blue=(short)(iblue/cmapscale+0.5);
            map[ired][igreen][iblue]=i;
            mapcolor(i,red,green,blue);
         }
      }
   }
}
pacbuff()
{
}
#include <gl.h>
#include <device.h>
#include <math.h>
#include <stdio.h>

pltras()
{
   Scoord i,j;
   static Boolean first=TRUE;
   long xorigin,xsize,yorigin,ysize;
   float aspect;
   extern Boolean mex;
   extern Colorindex mapimage[1024][1280];
   extern long oldwin,winchange;
   extern RGBvalue image[1024][3][1280];
   extern Scoord xlen,ylen;
   if(first) {
      if(mex) {
         foreground();
         keepaspect(XMAXSCREEN,YMAXSCREEN);
         winchange=winopen("change");
         oldwin=winattach();
         doublebuffer();
         gconfig();
         frontbuffer(TRUE);
         makemap();
      }
      else {
         ginit();
         RGBmode();
         gconfig();
      }
      initkeys();
      qreset();
      first=FALSE;
   }
   else {
      lampon(3);
      if(mex) {
         getorigin(&xorigin,&yorigin);
         getsize(&xsize,&ysize);
         aspect=(float)xlen/(float)ylen;
         if(ysize*aspect>xsize) {
            ysize=(int)((float)xsize/aspect+0.5);
         }
         else {
            xsize=(int)((float)ysize*aspect+0.5);
         }
         keepaspect(xsize,ysize);
         winconstraints();
	 viewport(0,xsize-1,0,ysize-1);
         winposition(xorigin,xorigin+xsize,yorigin,yorigin+ysize);
         ortho2(0.0,(float) (xlen-1),0.0,(float) (ylen-1));
         lookat(0.0,0.0,1.0,0.0,0.0,0.0,0.0);
         curson();
         color(BLACK);
         clear();
         for(j=0;j<ylen;j++){
            for(i=0;i<xlen;i++){
               color(mapimage[j][i]);
               pnt2s(i,j);
            }
         }
      }
      else {
         ortho2(0.0,(float) (xlen-1),0.0,(float) (ylen-1));
         lookat(0.0,0.0,1.0,0.0,0.0,0.0,0.0);
         cursoff();
         RGBcolor(0,0,0);
         clear();
         for(j=0;j<ylen;j++){
            cmov2s(0,j);
            writeRGB(xlen,image[j][0][0],image[j][1][0],image[j][2][0]);
         }
      }
      lampoff(15);
      qreset();
   }
}
#include <gl.h>
#include <device.h>
#include <math.h>
#include <stdio.h>

readras(inunit,endofile)
Boolean endofile;
FILE *inunit;
{
   register unsigned char ilen,tempb,tempg,tempr;
   unsigned char rlergbl[1280][4];
   float rred,rgre,rblu;
   unsigned long buffer[512],i,icode,j,nspan,nwords,xpos;
   unsigned short ixpos,iypos,xorigin,yorigin;
   register unsigned short lilen,tempmc;
   static unsigned short xmax=1279,ymax=1023;
   extern unsigned char bbck,gbck,rbck;
   extern Boolean mex,overlayflag,picture;
   extern Colorindex map[10][10][10],mapimage[1024][1280];
   extern float cmapscale;
   extern RGBvalue image[1024][3][1280];
   extern Scoord xlen,ylen;
   xorigin=0;
   yorigin=0;
   lampoff(15);
   lampon(1);
   qreset;
nexticode:
/*100   fread(icode,inunit,err=800,end=900); */
   while((fread(&icode,sizeof icode,1,inunit))==1){
      switch(icode)
      {
      case 500 :
/*
      User OP-CODE (USEROP)
*/
         if(fread(&nwords,sizeof nwords,1,inunit)!=1) goto eoforerr;
         if(nwords>=512) printf("icode=%lu  nwords=%lu\n",icode,nwords);
         if(fread(buffer,sizeof buffer[0],nwords,inunit)!=nwords)
                  goto eoforerr;
	 break;
      case 200 :
/*
      Raster dimension (RASDIM), image size
*/
         if(fread(&xlen,sizeof xlen,1,inunit)!=1) goto eoforerr;
         if(fread(&ylen,sizeof ylen,1,inunit)!=1) goto eoforerr;
	 break;
      case 210 :
/*
  Set raster origin (RASORI)
*/
         if(fread(&xorigin,sizeof xorigin,1,inunit)!=1) goto eoforerr;
         if(fread(&yorigin,sizeof yorigin,1,inunit)!=1) goto eoforerr;
         xorigin=xorigin-1;
         yorigin=yorigin-1;
	 break;
      case 220 :
/*
  Set raster background color (BACRAS)
*/
         picture=TRUE;
         if(fread(&rred,sizeof rred,1,inunit)!=1) goto eoforerr;
         if(fread(&rgre,sizeof rgre,1,inunit)!=1) goto eoforerr;
         if(fread(&rblu,sizeof rblu,1,inunit)!=1) goto eoforerr;
         if(!overlayflag) {
            rbck=(unsigned short)(rred*255.+0.5);
            gbck=(unsigned short)(rgre*255.+0.5);
            bbck=(unsigned short)(rblu*255.+0.5);
            if(mex) tempmc=map[(unsigned short)(rred*255.0*cmapscale+0.5)]
                              [(unsigned short)(rgre*255.0*cmapscale+0.5)]
                              [(unsigned short)(rblu*255.0*cmapscale+0.5)];
            for(j=0;j<ylen;j++) {
               for(i=0;i<xlen;i++) {
                  image[j][0][i]=rbck;
                  image[j][1][i]=gbck;
                  image[j][2][i]=bbck;
                  if(mex) mapimage[j][i]=tempmc;
               }
            }
         }
	 break;
      case 230 :
/*
  Encoded raster line (RASENL)
*/
         picture=TRUE;
         if(fread(&ixpos,sizeof ixpos,1,inunit)!=1) goto eoforerr;
         if(fread(&iypos,sizeof iypos,1,inunit)!=1) goto eoforerr;
         if(fread(&nspan,sizeof nspan,1,inunit)!=1) goto eoforerr;
         if(nspan>=1280) printf("nspan=%lu \n",nspan);
         if(fread(&rlergbl[0][0],sizeof rlergbl[0][0],nspan*4,inunit)!=nspan*4)
                  goto eoforerr;
         ixpos=(ixpos--)+xorigin;
         iypos=(iypos--)+yorigin;
         if(iypos<=ymax) {
            for(i=0;i<nspan&&ixpos<=xmax;i++) {
               lilen=rlergbl[i][3];
               if(lilen==0) lilen=256;
               tempr=rlergbl[i][0];
               tempg=rlergbl[i][1];
               tempb=rlergbl[i][2];
               if(mex) tempmc=map[(int)((float)tempr*cmapscale+0.5)]
                                 [(int)((float)tempg*cmapscale+0.5)]
                                 [(int)((float)tempb*cmapscale+0.5)];
               for(j=1;j<=lilen&&ixpos<=xmax;j++,ixpos++) {
                  image[iypos][0][ixpos]=tempr;
                  image[iypos][1][ixpos]=tempg;
                  image[iypos][2][ixpos]=tempb;
                  if(mex) mapimage[iypos][ixpos]=tempmc;
               }
            }
         }
	 break;
      case 240 :
/*
  Relative raster line (RASREL)
*/
         picture=TRUE;
         lampoff(1);
         if(fread(&iypos,sizeof iypos,1,inunit)!=1) goto eoforerr;
         if(fread(lilen,sizeof lilen,1,inunit)!=1) goto eoforerr;
         iypos=(iypos--)+yorigin;
         for(i=0;i<xlen;i++) {
            image[iypos][0][i]=image[iypos-1][0][i];
            image[iypos][1][i]=image[iypos-1][1][i];
            image[iypos][2][i]=image[iypos-1][2][i];
            if(mex) mapimage[iypos][i]=mapimage[iypos-1][i];
         }
         for(i=1;i<=lilen;i++) {
            if(fread(&xpos,sizeof xpos,1,inunit)!=1) goto eoforerr;
            if(fread(ilen,sizeof ilen,1,inunit)!=1) goto eoforerr;
            if(fread(tempr,sizeof tempr,1,inunit)!=1) goto eoforerr;
            if(fread(tempg,sizeof tempg,1,inunit)!=1) goto eoforerr;
            if(fread(tempb,sizeof tempb,1,inunit)!=1) goto eoforerr;
            xpos=(xpos--)+xorigin;
            image[iypos][0][xpos]=tempr;
            image[iypos][1][xpos]=tempg;
            image[iypos][2][xpos]=tempb;
            if(mex) mapimage[iypos][xpos]=
			   map[(int)((float)tempr*cmapscale+0.5)]
                              [(int)((float)tempg*cmapscale+0.5)]
                              [(int)((float)tempb*cmapscale+0.5)];
         }
         lampon(1);
	 break;
      case 98 :
/*
  end of frame
*/
         ringbell();
         lampoff(15);
         overlayflag=FALSE;
         if(picture) {
            pltras();
            check();
         }
         xorigin=0;
         yorigin=0;
         picture=FALSE;
         return endofile;
	 break;
      case 99 :
/*
  end of metafile
*/
         ringbell();
         lampoff(15);
         overlayflag=FALSE;
         if(picture) {
            pltras();
            check();
         }
         picture=FALSE;
         goto eoforerr;
	 break;
      default :
         printf("illegal icode\n");
      }
   }
eoforerr:/* 900, 800 */
   if(ferror(inunit)) {
      printf("error during read\n");
      lampoff(15);
      exit();
   }
   else {
      if(picture) {
         pltras();
         check();
      }
      endofile=TRUE;
      overlayflag=FALSE;
      picture=FALSE;
      printf("end of file\n");
      fclose(inunit);
   }
   return endofile;
}
#include <gl.h>
#include <device.h>
#include <stdio.h>

tekrgb()
{
/*
        Brent L. Bates
        NASA-Langley Research Center
        M.S. 294
        Hampton, Virginia  23665-5225
        (804) 864-2854
        E-mail: blbates@aero4.larc.nasa.gov or blbates@aero2.larc.nasa.gov
*/
   static Boolean fileopen=FALSE;
   static unsigned char eol=2,eot=1;
   unsigned char checksum,temp;
   char fileout[81];
   register short j;
   register unsigned short i;
   static FILE *ounit;
   unsigned long sumcheck;
   extern char outfile[81];
   extern RGBvalue image[1024][3][1280];
   extern Scoord xlen,ylen;
   if(!fileopen) {
      strcpy(fileout,outfile);
      strcat(fileout,".tek");
      ounit=fopen(fileout,"w");
      fseek(ounit,0L,0);
      fileopen=TRUE;
   }
   sumcheck=0;
   ringbell();
/*                       print-request  */
   temp=0x14;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       length, # of bytes in option list (20)
                         (primary integer format)
                 byte 1: 11xxxxxx     byte x: 10xxxxxx  */
   temp=0xd4;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       # pixels per line (xlen)
                         (primary integer format)  */
   temp=0xc0+(xlen>>6);
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
   temp=0x80+(xlen&0x3f);
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       # lines in image (ylen)
                         (primary integer format)  */
   temp=0xc0+(ylen>>6);
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
   temp=0x80+(ylen&0x3f);
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       communication option (handshake)
                         (primary option format)  */
   temp=0xc1;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       # copies (printer default)
                         (primary integer format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       pixel size/aspect ratio (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       pixel encoding (arbitrary 8)
                         (primary option format)  */
   temp=0xcd;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       pixel ordering (pixel-data order 1)
                         (primary option format)  */
   temp=0xc1;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       image sizing (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       image rendering (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       color conversion, gamma correction (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       color manipulation (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       black/white inversion, color exchange (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       hardcopy orientation (printer default)
                         (primary option format)  */
   temp=0xc0;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       copier identification (4693D printer)
                         (identification option format)  */
   temp=0xc9;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
   temp=0x95;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       suboption
                         (primary option format)  */
   temp=0x81;
   fwrite(&temp,sizeof temp,1,ounit);
   sumcheck=sumcheck+temp;
/*                       checksum calculation
                         (checksum option format)  */
   sumcheck=sumcheck%0x80;
   checksum=sumcheck&0x7f;
   checksum=checksum|0x80;
   fwrite(&checksum,sizeof checksum,1,ounit);
   fwrite(&eol,sizeof eol,1,ounit);
/*
   write image
*/
   for(j=ylen-1;j>=0;j--) {
      for(i=0;i<xlen;i++) {
         fwrite(&image[j][0][i],sizeof image[0][0][0],1,ounit);
         fwrite(&image[j][1][i],sizeof image[0][0][0],1,ounit);
         fwrite(&image[j][2][i],sizeof image[0][0][0],1,ounit);
      }
      fwrite(&eol,sizeof eol,1,ounit);
   }
   fwrite(&eot,sizeof eot,1,ounit);
}
