home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / irafpipe.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  6KB  |  180 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    irafpipe.c (IRAF Pipe Connection)
  6.  * Purpose:    Read an image in from the IRAF pipe
  7.  * Subroutine:    read_imtool_packet()        returns: void
  8.  * Copyright:    1990 Smithsonian Astrophysical Observatory
  9.  *        You may do anything you like with this file except remove
  10.  *        this copyright.  The Smithsonian Astrophysical Observatory
  11.  *        makes no representations about the suitability of this
  12.  *        software for any purpose.  It is provided "as is" without
  13.  *        express or implied warranty.
  14.  * Modified:    {0} based on code from Doug Tody's IRAF Imtool (NOAO)
  15.  *        {1} Michael VanHilst    adapted            9 July 1989
  16.  *        {2} MVH rearranged structure            29 March 1990
  17.  *        {n} <who> -- <does what> -- <when>
  18.  * "For the moment we take an IIS model 70 command/data stream as input; this
  19.  * is used to load images into the image display.  This is a kludge interface
  20.  * for the prototype, convenient since the high level software is written for
  21.  * the IIS." - explanation by Doug Tody
  22.  */
  23.  
  24. #ifdef IMTOOL
  25.  
  26. #include <stdio.h>        /* stderr, FILE, NULL, etc. */
  27. #include <X11/Xlib.h>        /* get X types and constants */
  28. #include "hfiles/control.h"    /* struct connectRec */
  29. #include "hfiles/imtool.h"    /* struct imtoolRec, codes */
  30.  
  31. /*
  32.  * Subroutine:    read_imtool_packet()
  33.  * Purpose:    event handler for packet input from iraf
  34.  */
  35. void read_imtool_packet( port )
  36.      struct connectRec *port;
  37. {
  38.   int ndatabytes;
  39.   int bytes;
  40.   int last_packet;
  41.   struct imtoolRec imhead;
  42.   static int byteswap = 0;    /* l: flag to byteswap packets */
  43.   static int errcnt=0;        /* l: count packet errors (before giving up) */
  44.   static int noblock=0;        /* l: check if in non-blocking loop */
  45. #ifdef DEBUG
  46.   int pkt;            /* l: make packet code discrete variable */
  47. #endif
  48.   int read_connection();
  49.   void imtool_response(), swap_bytes();
  50.   static int check_packet_sum();
  51.  
  52.   /* read the header */
  53.   bytes = read_connection(port, (char *)&imhead, sizeof(struct imtoolRec));
  54.   /* check size of read */
  55.   if( bytes < sizeof(struct imtoolRec) ) {
  56.     /* if EOF, must be problem with this end of pipe, call plumber */
  57.     /* read 0 may happen once on EOF, happens forever if non-blocking */
  58.     if( bytes == 0 ) {
  59.       /* pipe mustn't be blocking, count the bad passes through here */
  60.       /* noblock is set to zero everywhere but here */
  61.       ++noblock;
  62.       return;
  63.     } else {
  64.       noblock = 0;
  65. #ifdef DEBUG
  66.       (void)fprintf(stderr, "Pipe read error\n");
  67. #endif
  68.       return;
  69.     }
  70.   }
  71.  
  72. #if DEBUG
  73.   if( getenv("DEBUG_IMTOOL") ) {
  74.     char  *s;
  75.     short su;
  76.  
  77.     (void)fprintf(stderr, "\n=== Imtool header ===\n");
  78.     (void)fprintf(stderr, "  tid:     %d  -- ", imhead.tid);
  79.     if(imhead.tid & PACKED)        (void)fprintf(stderr, "PACKED ");
  80.     if( imhead.tid & COMMAND )    (void)fprintf(stderr, "COMMAND ");
  81.     if( imhead.tid & IIS_READ )    (void)fprintf(stderr, "IIS_READ ");
  82.     if( imhead.tid & IMC_SAMPLE )    (void)fprintf(stderr, "IMC_SAMPLE ");
  83.     (void)fprintf(stderr, "\n");
  84.     (void)fprintf (stderr, "  count:   %d\n", -imhead.thingct);
  85.     su = imhead.subunit & 077;
  86.     if( su == MEMORY )        s = "MEMORY";
  87.     else if( su == LUT )        s = "LUT";
  88.     else if( su == FEEDBACK )    s = "FEEDBACK";
  89.     else if( su == WCS )        s = "WCS";
  90.     else if( su == IMCURSOR )    s = "IMCURSOR";
  91.     else if( su == 12 )        s = "ZOOM/PAN (ignored)";
  92.     else                s = "???";
  93.     (void)fprintf(stderr, "  subunit: %d -- %s\n", su, s);
  94.     /* (void)fprintf(stderr, "  chksum:  %x\n", imhead.checksum); */
  95.     (void)fprintf(stderr, "  x: %d  y: %d  z: %d  t: %d\n",
  96.               imhead.x & 077777, imhead.y & 077777,
  97.               imhead.z & 07777, imhead.t & 077);
  98.   }
  99. #endif
  100.  
  101.   noblock = 0;
  102.   if( byteswap )
  103.     swap_bytes((char *)&imhead, sizeof(struct imtoolRec));
  104.   /* check the packet checksum (debug it after 2nd try), don't die */
  105.   if( !check_packet_sum((char *)&imhead, sizeof(struct imtoolRec),
  106.              &byteswap, (errcnt > 2), 0) ) {
  107.     /* no hope of syncing on next read, better flush out pipe */
  108.     flush_connection(port);
  109.     errcnt++;
  110.     return;
  111.   }
  112.   /* read the count field */
  113.   if( (ndatabytes = -(int)imhead.thingct) < 0 ) {
  114.     (void)fprintf(stderr,"bad packet\n");
  115.     return;
  116.   }
  117.   /* check for packed short's (we wish) */
  118.   if( !(imhead.tid & PACKED) )
  119.     ndatabytes *= 2;
  120.   imtool_response(port, &imhead, ndatabytes);
  121. }
  122.  
  123.  
  124. /*
  125.  * Subroutine:    check_packet_sum
  126.  * Purpose:    Check packet sum against checksum (and verify byteswap)
  127.  * Returns:    1
  128.  */
  129. static int check_packet_sum ( packet, size, byteswap, errmsg, die )
  130.      char *packet;
  131.      int size;
  132.      int *byteswap, errmsg, die;
  133. {
  134.   int i, sum, second_try=0;
  135.   register short *p;
  136.   void swap_bytes(), exit_errmsg();
  137.  
  138.   while( 1 ) {
  139.     /* add up the sum */
  140.     for( i=0, sum=0, p=(short *)packet;  i < 8;  i++ )
  141.       sum += *p++;
  142.     /* sum (including checksum) should roll at 0177777 */
  143.     if( (sum & 0177777) == 0177777 )
  144.       return( 1 );
  145.     /* if not, bswap and try again (note danger of swab in place) */
  146.     swap_bytes(packet, size);
  147.     *byteswap = !(*byteswap);
  148.     if( second_try++ ) {
  149.       /* after retry, report the error */
  150.       if( errmsg ) {
  151.     (void)fprintf(stderr, "imtool: bad data header checksum\n");
  152. #ifdef DEBUG
  153.     /* display first few bytes both swapped and unswapped */
  154.     if( *byteswap )
  155.       /* if was byteswapped, start unswapped */
  156.       swap_bytes(packet, size);
  157.     /* what it looks like unswapped */
  158.     (void)fprintf(stderr, "noswap:");
  159.     for( i=0, p=(short *)packet;  i < 8;  i++ )
  160.       (void)fprintf(stderr, " %6o", p[i]);
  161.     (void)fprintf(stderr, "\n");
  162.     /* what it looks like swapped */
  163.     swap_bytes(packet, size);
  164.     (void)fprintf(stderr, "  swap:");
  165.     for( i=0, p=(short *)packet;  i < 8;  i++ )
  166.       (void)fprintf(stderr, " %6o", p[i]);
  167.     (void)fprintf(stderr, "\n");
  168. #endif
  169.       }
  170.       /* imtool dies at this point */
  171.       if( die )
  172.         exit_errmsg("FATAL ERROR: imtool pipe datastream synch lost\n");
  173.       else
  174.     return( 0 );
  175.     }
  176.   }
  177. }
  178.  
  179. #endif
  180.