home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / cmdimage.c < prev    next >
C/C++ Source or Header  |  1991-06-21  |  12KB  |  340 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    cmdimage.c (Command Image File)
  6.  * Purpose:    Set options from command line
  7.  * Subroutine:    parse_filename()            returns: int
  8.  * Subroutine:    parse_filetype()            returns: int
  9.  * Subroutione:    parse_fileread()            returns: int
  10.  * Subroutione:    parse_connection()            returns: int
  11.  * Xlib calls:    none
  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} Michael VanHilst    initial version               5  Jan 1989
  19.  *              {1} MVH BSDonly strings.h compatability         19 Feb 1990
  20.  *        {2} John Add Socket connection            8  May 1990
  21.  *        {3} MVH removed isalpha test for file names    1  Jan 1991
  22.  *        {4} MVH added + type switches and support    21 Jun 1991
  23.  *        {n} <who> -- <does what> -- <when>
  24.  */
  25.  
  26. #include <stdio.h>        /* stderr, NULL, etc. */
  27. #include <ctype.h>
  28.  
  29. #ifndef VMS
  30. #ifdef SYSV
  31. #include <string.h>
  32. #else
  33. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  34. #endif
  35. #else
  36. #include <string.h>
  37. #endif
  38.  
  39. #include <math.h>        /* for atof (see Ultrx atof man page) */
  40. #include <X11/Xlib.h>        /* X window stuff */
  41. #include <X11/Xutil.h>        /* X window manager stuff */
  42. #include "hfiles/constant.h"    /* define codes */
  43. #include "hfiles/struct.h"    /* declare structure types */
  44. #include "hfiles/extern.h"    /* extern main parameter structures */
  45. #include "hfiles/cmdparse.h"    /* define parse status bits */
  46.  
  47. /*
  48.  * Subroutine:    parse_filename
  49.  * Purpose:    Parse commandline to find an image file name
  50.  */
  51. int parse_filename ( argc, argv, argi, got_name )
  52.      int argc;        /* total number of arg tokens */
  53.      char *argv[];    /* array of arg tokens */
  54.      int *argi;        /* first arg to check, (returned as last arg used) */
  55.      int *got_name;    /* status word (use bit for found a file name) */
  56. {
  57.   int i;
  58.   int usage();
  59.  
  60.   i = *argi;
  61.   /* --- check for the filename as an unidentified string --- */
  62.   if( (argv[i][0] != '-') ) {
  63.     if( *got_name & CMD_FNAME ) {
  64.       (void)fprintf(stderr, "More than 1 possible file name: %s %s.\n",
  65.             img.filename, argv[i]);
  66.       return( usage("filename", argc, argv, i, i) );
  67.     } else {
  68.       img.filename = argv[i];
  69.       *got_name |= CMD_FNAME;
  70.     }
  71.     return( 1 );
  72.   }
  73.   /* --- check for filename as an identified string --- */
  74.   if( (strcmp(argv[i], "-name") == 0) ||
  75.       (strcmp(argv[i], "-") == 0) ) {
  76.     if( ++i >= argc )
  77.       return( usage("filename", argc, argv, i-1, i) );
  78.     if( *got_name & CMD_FNAME ) {
  79.       (void)fprintf(stderr, "More than 1 possible file name: %s %s.\n",
  80.             img.filename, argv[i]);
  81.       return( usage("filename", argc, argv, i, i) );
  82.     } else {
  83.       img.filename = argv[i];
  84.       *got_name |= CMD_FNAME;
  85.     }
  86.     *argi = i;
  87.     return( 1 );
  88.   }
  89.   return( 0 );
  90. }
  91.  
  92. /*
  93.  * Subroutine:    parse_filetype
  94.  * Purpose:    Parse command line for image file type
  95.  */
  96. int parse_filetype ( argc, argv, argi, got_type )
  97.      int argc;        /* total number of arg tokens */
  98.      char *argv[];    /* array of arg tokens */
  99.      int *argi;        /* first arg to check, (returned as last arg used) */
  100.      int *got_type;    /* status word (bit for got an image type switch) */
  101. {
  102.   int i;
  103.  
  104.   i = *argi;
  105.   /* --- IMAGE FILE TYPE --- */
  106.   if( strcmp(argv[i], "-iraf") == 0 ) {
  107.     /* --- IRAF image --- */
  108.     img.file_type = SOP_IRAF;
  109.   } else if( strcmp(argv[i], "-oif") == 0 ) {
  110.     /* --- Iraf OIF .imh headed image file --- */
  111.     img.file_type = SOP_IRAF;
  112.   } else if( strcmp(argv[i], "-fits") == 0 ) {
  113.     /* --- FITS image --- */
  114.     img.file_type = SOP_FITS;
  115.   } else if( strcmp(argv[i], "-dfits") == 0 ) {
  116.     /* --- FITS image in non-standard byte swap order --- */
  117.     img.file_type = SOP_FITS;
  118.     img.byte_swap = !img.byte_swap;
  119.   } else if( strcmp(argv[i], "-saoccd") == 0 ) {
  120.     /* --- SAOCCD image --- */
  121.     img.file_type = SOP_SAOCCD;
  122.   } else if( strcmp(argv[i], "-ros") == 0 ) {
  123.     /* --- ROSAT HOPR HRI image --- */
  124.     img.file_type = SOP_ROSAT;
  125.   } else {
  126.     /* --- check array type specification --- */
  127.     if( (strcmp(argv[i], "-chararray") == 0) ||
  128.         (strcmp(argv[i], "-u1" ) == 0) ) {
  129.       img.file_type = SOP_Array;
  130.       img.storage_type = ARR_U1;
  131.       img.bytepix = 1;
  132.     } else if( (strcmp(argv[i], "-shortarray") == 0) ||
  133.            (strcmp(argv[i], "-i2" ) == 0) ) {
  134.       img.file_type = SOP_Array;
  135.       img.storage_type = ARR_I2;
  136.       img.bytepix = 2;
  137.     } else if( (strcmp(argv[i], "-ushortarray") == 0) ||
  138.            (strcmp(argv[i], "-u2" ) == 0) ) {
  139.       img.file_type = SOP_Array;
  140.       img.storage_type = ARR_U2;
  141.       img.bytepix = 2;
  142.     } else if( (strcmp(argv[i], "-longarray") == 0) ||
  143.            (strcmp(argv[i], "-i4" ) == 0) ) {
  144.       img.file_type = SOP_Array;
  145.       img.storage_type = ARR_I4;
  146.       img.bytepix = 4;
  147.     } else if( (strcmp(argv[i], "-floatarray") == 0) ||
  148.            (strcmp(argv[i], "-r4" ) == 0) ) {
  149.       img.file_type = SOP_Array;
  150.       img.storage_type = ARR_R4;
  151.       img.bytepix = 4;
  152.     } else if( (strcmp(argv[i], "-doublearray") == 0) ||
  153.            (strcmp(argv[i], "-r8" ) == 0) ) {
  154.       img.file_type = SOP_Array;
  155.       img.storage_type = ARR_R8;
  156.       img.bytepix = 8;
  157.     } else {
  158.       /* --- got here if no match of any type --- */
  159.       return( 0 );
  160.     }
  161.     /* --- got here by finding an array type identifier --- */
  162.     /* if there are more arguments and the first does not start with '-' */
  163.     if( (argc > (i+2)) && (argv[i+1][0] != '-') && isdigit(argv[i+1][0]) ) {
  164.       /* --- if giving args like this, 2 must be given --- */
  165.       if( ((i+2) >= argc) ||
  166.       ((img.filecols = atoi (argv[++i])) <= 0) ||
  167.       ((img.filerows = atoi (argv[++i])) <= 0) )
  168.     return( usage("array size", argc, argv, i-3, i) );
  169.     }
  170.   }
  171.   /* --- got here after finding a match --- */
  172.   *got_type |= CMD_FTYPE;
  173.   *argi = i;
  174.   return( 1 );
  175. }
  176.  
  177. /*
  178.  * Subroutine:    parse_fileread
  179.  * Purpose:    Parse command line for image file reading parameters
  180.  */
  181. int parse_fileread ( argc, argv, argi, got_read )
  182.      int argc;        /* total number of arg tokens */
  183.      char *argv[];    /* array of arg tokens */
  184.      int *argi;        /* first arg to check, (returned as last arg used) */
  185.      int *got_read;    /* status word (bit that there were read parameters) */
  186. {
  187.   int i;
  188.  
  189.   i = *argi;
  190.   if( (strcmp(argv[i], "-sk") == 0) ||
  191.       (strcmp(argv[i], "-skip") == 0) ||
  192.       (strcmp(argv[i], "-header") == 0) ) {
  193.     /* --- skip header of given size --- */
  194.     if( (++i >= argc) || ((img.headersize = atoi(argv[i])) < 0) )
  195.       return( usage("header size", argc, argv, i-1, i) );
  196.   } else if( (strcmp(argv[i], "-bswap") == 0) ||
  197.          (strcmp(argv[i], "-byteswap") == 0) ) {
  198.     /* --- selecting byteswap gets reverse byteswap of whatever it was --- */
  199.     img.byte_swap = !img.byte_swap;
  200.   } else if( (strcmp(argv[i], "-sb") == 0) ||
  201.          (strcmp(argv[i], "-scalebias") == 0) ) {
  202.     /* --- scale and bias for image file data --- */
  203.     double scale;
  204.     float bias;
  205.  
  206.     if( (i + 2) >= argc )
  207.       return( usage("args for scale and bias", argc, argv, i, i) );
  208.     if( (scale = atof(argv[++i])) <= 0.0 )
  209.       return( usage("value for scale", argc, argv, i-1, i) );
  210.     if( sscanf(argv[++i], "%f", &bias) != 1 )
  211.       return( usage("value for bias", argc, argv, i-2, i) );
  212.     /* store values and flag if they represent remapping of the values */
  213.     if( ((img.fscale = scale) != 1.0) ||
  214.         ((img.fbias = (double)bias) != 0.0) )
  215.       img.fscaled = 1;
  216.     else
  217.       img.fscaled = 0;
  218.   } else if( (strcmp(argv[i], "-dc") == 0) ||
  219.          (strcmp(argv[i], "-dispcen") == 0) ) {
  220.     /* --- initial display location --- */
  221.     if( ((i+2) >= argc) ||
  222.         (img.fdcenX = atof(argv[++i]) <= 0.0) ||
  223.         (img.fdcenY = atof(argv[++i]) <= 0.0) )
  224.       return( usage("display center", argc, argv, i-3, i) );
  225.     if( (argc >= (i+1)) && (argv[i+1][0] != '-') && isdigit(argv[i+1][0]) ) {
  226.       if( (img.fdblock = atoi(argv[++i]) == 0) )
  227.     return( usage("display blocking", argc, argv, i-1, i) );
  228.     }
  229.   } else if( (strcmp(argv[i], "-buf") == 0) ||
  230.          (strcmp(argv[i], "-bufmax") == 0) ) {
  231.     /* --- maximum buffer array size (1 side of square) --- */
  232.     if( (++i >= argc) || ((img.bufmax = atoi(argv[i])) <= 0) )
  233.       return( usage("buffer limit", argc, argv, i-1, i) );
  234.   } else if( strcmp(argv[i], "-fbconfig") == 0 ) {
  235.     /* --- iraf display configuration --- */
  236.     if( (++i >= argc) || ((img.fbconfig = atoi(argv[i])) == 0) )
  237.       return( usage("fbconfig", argc, argv, i-1, i) );
  238.   } else {
  239.     return( 0 );
  240.   }
  241.   *got_read |= CMD_FREAD;
  242.   *argi = i;
  243.   return( 1 );
  244. }
  245.  
  246. /*
  247.  * Subroutine:    parse_connection
  248.  * Purpose:    Parse command line for socket or pipe connection stuff
  249.  */
  250. int parse_connection ( argc, argv, argi, got_read, init )
  251.      int argc;        /* i: total number of arg tokens */
  252.      char *argv[];    /* i: array of arg tokens */
  253.      int *argi;        /* i/o: first arg to check, (return last arg used) */
  254.      int *got_read;    /* o: status word (bit for connection parameters) */
  255.      int init;        /* i: this is init time */
  256. {
  257.   int i;
  258.   void open_imtool_connection(), close_imtool_connection();
  259.   void rename_imtool_connection();
  260. #ifdef REALTIME
  261.   void open_rtsock_connection();
  262.   void open_rtfifo_connection();
  263. #endif
  264.  
  265.   i = *argi;
  266.   if( strcmp(argv[i], "-pros") == 0 ) {
  267.     /* --- Imtool pipe with pros extensions --- */
  268.     if( init ) {
  269.       control.IRAF_in.open = 1;
  270.       control.IRAF_in.func = open_imtool_connection;
  271.     } else if( control.IRAF_in.open == 0 )
  272.       open_imtool_connection();
  273.     control.IRAF_out.protocol = IOP_PROS;
  274.   } else if( strcmp(argv[i], "+imtool") == 0 ) {
  275.     /* --- Imtool pipe --- */
  276.     if( init ) {
  277.       control.IRAF_in.open = 1;
  278.       control.IRAF_in.func = open_imtool_connection;
  279.     } else if( control.IRAF_in.open == 0 )
  280.       open_imtool_connection();
  281.     control.IRAF_out.protocol = IOP_Imtool;
  282.   } else if( (strcmp(argv[i], "-imtool") == 0) ||
  283.          (strcmp(argv[i], "-imtool-") == 0) ) {
  284.     /* --- Close imtool pipe --- */
  285.     if( (init == 0) && (control.IRAF_in.open != 0) )
  286.       close_imtool_connection();
  287.     control.IRAF_in.open = 0;
  288.     control.IRAF_in.func = NULL;
  289.   } else if( strcmp(argv[i], "-idev") == 0 ) {
  290.     /* --- iraf input pipe device --- */
  291.     if( ++i >= argc )
  292.       return( usage ("idev", argc, argv, i-1, i) );
  293.     rename_imtool_connection(argv[i], 1);
  294.   } else if( strcmp(argv[i], "-odev") == 0 ) {
  295.     /* --- iraf output pipe device --- */
  296.     if( ++i >= argc )
  297.       return( usage("odev", argc, argv, i-1, i) );
  298.     rename_imtool_connection(argv[i], 0);
  299. #ifdef REALTIME
  300.   } else if( strcmp(argv[i], "-rtsock") == 0 ) {
  301.     /* --- RTIO Socket --- */
  302.     si_init(NULL);
  303.     if( init ) {
  304.       control.aux_in.open = 1;
  305.       control.aux_in.func =  open_rtsock_connection;
  306.       if( (argc >= i) && (argv[i][0] != '-') && isdigit(argv[i][0]) ) {
  307.         int j = 0;
  308.         i++;
  309.         control.aux_in.name = NULL;
  310.       } else 
  311.     return( usage("-rtsock", argc, argv, i-1, i ) );
  312.       control.aux_in.address = atoi(argv[i]);
  313.     } else
  314.       if( control.aux_in.open == 0 )
  315.         open_rtsock_connection(&control.aux_in);
  316.     control.aux_in.protocol = IOP_socket;
  317.   } else if( strcmp(argv[i], "-rtfifo") == 0 ) {
  318.     /* --- RTIO FIFO --- */
  319.     si_init(NULL);
  320.     if( init ) {
  321.       control.aux_in.open = 1;
  322.       control.aux_in.func =  open_rtfifo_connection;
  323.       if( ++i >= argc ) {
  324.         return( usage("odev", argc, argv, i-1, i) );
  325.       } else {
  326.     control.aux_in.name = argv[i];
  327.       };
  328.     } else 
  329.       if( control.aux_in.open == 0 )
  330.         open_rtfifo_connection(&control.aux_in);
  331. #endif
  332.   } else {
  333.     return( 0 );
  334.   }
  335.   *got_read |= CMD_CONNECT;
  336.   *argi = i;
  337.   return( 1 );
  338. }
  339.  
  340.