home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / irafio.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  7KB  |  263 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    irafio.c (IRAF Input Output)
  6.  * Purpose:    Read an image in from the IRAF pipe
  7.  * Subroutine:    open_imtool_connection()    returns: void
  8.  * Subroutine:    close_imtool_connection()    returns: void
  9.  * Subroutine:    rename_imtool_connection()    returns: void
  10.  * Subroutine:    imtool_input()            returns: int
  11.  * Subroutine:    imtool_output()            returns: int
  12.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  13.  *        You may do anything you like with this file except remove
  14.  *        this copyright.  The Smithsonian Astrophysical Observatory
  15.  *        makes no representations about the suitability of this
  16.  *        software for any purpose.  It is provided "as is" without
  17.  *        express or implied warranty.
  18.  * Modified:    {0} based on code from Doug Tody's IRAF Imtool (NOAO)
  19.  *        {1} Michael VanHilst    adapted            9 July 1989
  20.  *        {2} Michael VanHilst added output        2 Nov 1989
  21.  *        {3} MVH used generalized remote connection IO    28 March 1990
  22.  *        {n} <who> -- <does what> -- <when>
  23.  * "For the moment we take an IIS model 70 command/data stream as input; this
  24.  * is used to load images into the image display.  This is a kludge interface
  25.  * for the prototype, convenient since the high level software is written for
  26.  * the IIS." - explanation by Doug Tody
  27.  */
  28.  
  29. #include <stdio.h>        /* stderr, FILE, NULL, etc. */
  30.  
  31. #ifndef VMS
  32. #ifdef SYSV
  33. #include <string.h>
  34. #else
  35. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  36. #endif
  37. #else
  38. #include <string.h>
  39. #endif
  40.  
  41. #include <X11/Xlib.h>        /* X window stuff */
  42. #include "hfiles/control.h"    /* define struct connectRec */
  43. #include "hfiles/imtool.h"    /* define struct imtoolRec, codes */
  44. extern struct controlRec control;
  45.  
  46. /*
  47.  * Subroutine:    open_imtool_connection
  48.  * Purpose:    open the connection for IRAF
  49.  */
  50. void open_imtool_connection ()
  51. {
  52. #ifdef IMTOOL
  53.   int open_connection();
  54.   extern void read_imtool_packet();
  55.  
  56.   control.IRAF_in.func = read_imtool_packet;
  57.   if( open_connection(&control.IRAF_in) != -1 ) {
  58.     if( control.IRAF_in.type != IOP_socket )
  59.       (void)open_connection(&control.IRAF_out);
  60.     if( control.verbose )
  61.       (void)printf("Open to accept IRAF input\n");
  62.   }
  63. #else
  64.   (void)fprintf(stderr, "IRAF imtool support not compiled\n");
  65. #endif
  66. }
  67.  
  68. /*
  69.  * Subroutine:    close_imtool_connection
  70.  * Purpose:    close the connection for IRAF
  71.  */
  72. void close_imtool_connection ()
  73. {
  74.   close_connection(&control.IRAF_in);
  75.   if( control.IRAF_in.type != IOP_socket )
  76.     close_connection(&control.IRAF_out);
  77.   if( control.verbose )
  78.     (void)printf("Closed IRAF input channel\n");
  79. }
  80.  
  81. /*
  82.  * Subroutine:    rename_imtool_connection
  83.  * Purpose:    assign a new name to the imtool connection
  84.  */
  85. void rename_imtool_connection ( name, in )
  86.      char *name;
  87.      int in;
  88. {
  89.   char *string, *calloc_errchk();
  90.  
  91.   string = calloc_errchk(strlen(name)+2, 1, "string buffer");
  92.   strcpy(string, name);
  93.   if( in )
  94.     control.IRAF_in.name = string;
  95.   else
  96.     control.IRAF_out.name = string;
  97. }
  98.  
  99. #ifdef IMTOOL
  100.  
  101. /*
  102.  * Subroutine:    imtool_output
  103.  * Purpose:    Write image data back to iraf device channel
  104.  */
  105. void imtool_output ( imhead, odev, imagebuf, width, height )
  106.      struct imtoolRec *imhead;
  107.      struct connectRec *odev;
  108.      short *imagebuf;
  109.      int width, height;
  110. {
  111.   int ndatabytes;
  112.   int x, y;
  113.   int offset;
  114.   int nleft;
  115.   int bytes;
  116.   int buferr = 0;
  117.   char fifobuf[SZ_FIFOBUF + 4];
  118.   int write_connection();
  119.   static int compact_short_to_byte();
  120.  
  121.   ndatabytes = -(int)imhead->thingct;
  122.   x = imhead->x & 077777;
  123.   y = imhead->y & 077777;
  124.   offset = (y * width) + x;
  125.   /* if requested segment overflows,must be in error */
  126.   if( (offset + ndatabytes) > (width * height) ) {
  127.     (void)fprintf(stderr, "attempted read out of bounds on buffer\n");
  128.     (void)fprintf(stderr, "Sending NULL data\n");
  129.     imagebuf = NULL;
  130.   } else
  131.     imagebuf += offset;
  132.   bzero(fifobuf, SZ_FIFOBUF + 4);
  133.   bytes = SZ_FIFOBUF;
  134.   for( nleft = ndatabytes; nleft > 0; nleft -= bytes ) {
  135.     if( nleft < SZ_FIFOBUF )
  136.       bytes = nleft;
  137.     /* load buffer for writing */
  138.     if( imagebuf != NULL ) {
  139.       if( compact_short_to_byte(imagebuf, (unsigned char *)fifobuf, bytes) )
  140.     buferr = 1;
  141.     }
  142.     if( write_connection(odev, fifobuf, bytes) < 0 )
  143.       return;
  144.     imagebuf += bytes;
  145.   }
  146.   if( buferr )
  147.     (void)fprintf(stderr, "WARNING: data not IRAF compatible\n");
  148. }
  149.  
  150. /*
  151.  * Subroutine:    imtool_input
  152.  * Purpose:    Load image data from iraf input device channel
  153.  * Called by:    imtool_response() in RemoteImtool.c
  154.  * Returns:    -1 if read no bytes, 1 if trouble, else 0
  155.  */
  156. int imtool_input ( imhead, idev, readbuf, iwdth, owdth, y1, y2,
  157.            imagebuf, imbufsz )
  158.      struct imtoolRec *imhead;
  159.      struct connectRec *idev;
  160.      char *readbuf;
  161.      int iwdth, owdth;
  162.      int *y1, *y2;
  163.      short *imagebuf;
  164.      int imbufsz;
  165. {
  166.   int ndatabytes, ndatavals;
  167.   int packed;
  168.   int errcnt;
  169.   int x, y;
  170.   int nvals, nleft;
  171.   int got, try, gotten;
  172.   short *obuf;
  173.   int read_connection();
  174.   static void expand_byte_to_short();
  175.  
  176.   /* get data count and packing type */
  177.   ndatavals = -(int)imhead->thingct;
  178.   if( !(imhead->tid & PACKED) ) {
  179.     ndatabytes = ndatavals * 2;
  180.     packed = 0;
  181.   } else {
  182.     ndatabytes = ndatavals;
  183.     packed = 1;
  184.   }
  185.   /* get bytes and upper left coordinates */
  186.   x = imhead->x & 077777;
  187.   y = imhead->y & 077777;
  188.   /* check for running out of bounds */
  189.   *y1 = y;
  190.   *y2 = y + (ndatavals / iwdth);
  191.   if( ((y * owdth + ndatavals) * 2) > imbufsz ) {
  192.     (void)fprintf(stderr,"Error: Imtool read beyond buffer %d, %d\n", y, *y2);
  193.     return( -1 );
  194.   }
  195.   /* compute pointer to output buffer starting entry */
  196.   obuf = imagebuf + (y * owdth) + x;
  197.   /* read the data into the frame buffer. */
  198.   for( nleft = ndatabytes;  nleft > 0;  nleft -= nvals ) {
  199.     try = nvals = (nleft < SZ_FIFOBUF) ? nleft : SZ_FIFOBUF;
  200.     errcnt = 0;
  201.     gotten = 0;
  202.     while( (got = read_connection(idev, &(readbuf[gotten]), try)) < try ) {
  203.       (void)fprintf(stderr, "Pipe data under-read: %d %d\n", got, try);
  204.       (void)fflush(stderr);
  205.       try -= got;
  206.       gotten += got;
  207.       if( errcnt++ > 10 )
  208.     return( 1 );
  209.     }
  210.     /* move input data to image buffer */ 
  211.     if( packed ) {
  212.       /* 8 bit integers in */
  213.       expand_byte_to_short ((unsigned char *)readbuf, obuf, nvals);
  214.       obuf += nvals;
  215.     } else if( readbuf != (char *)obuf ) {
  216.       /* 16 bit integers in */
  217.       bcopy(readbuf, (char *)obuf, nvals);
  218.       obuf += (nvals / 2);
  219.     }
  220.   }
  221.   return( 0 );
  222. }
  223.  
  224. /*
  225.  * Subroutine:    expand_byte_to_short
  226.  * Purpose:    Unpack byte data for use as shorts
  227.  */
  228. static void expand_byte_to_short ( bts, ints, cnt )
  229.      register unsigned char *bts;
  230.      register short *ints;
  231.      register int cnt;
  232. {
  233.   while (cnt-- > 0) {
  234.     *ints = *bts;
  235.     ints++;
  236.     bts++;
  237.   }
  238. }
  239.  
  240. static int compact_short_to_byte ( ints, bts, cnt )
  241.      register short *ints;
  242.      register unsigned char *bts;
  243.      register int cnt;
  244. {
  245.   int buferr = 0;
  246.   while( cnt-- > 0 ) {
  247.     if( *ints > 255 ) {
  248.       *bts = 255;
  249.       buferr = 1;
  250.     } else if( *ints < 0 ) {
  251.       *bts = 0;
  252.       buferr = 1;
  253.     } else
  254.       *bts = *ints;
  255.     ++ints;
  256.     ++bts;
  257.   }
  258.   return( buferr );
  259. }
  260.  
  261.  
  262. #endif
  263.