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

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    rgnwrite.c (Region Write)
  6.  * Purpose:    Write descriptions of saved regions to a file
  7.  * Subroutine:    write_regions()        returns: void
  8.  * Subroutine:    timestamp()        returns: void
  9.  * UNIX calls:    time(), asctime(), localtime(), fclose()
  10.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Modified:    {0} Michael VanHilst    initial version          9 July 1989
  17.  *        {1} MVH removed comma from list output           7 Dec 1989
  18.  *              {2} MVH BSDonly strings.h compatability           19 Feb 1990
  19.  *        {n} <who> -- <does what> -- <when>
  20.  */
  21.  
  22. #include <stdio.h>        /* stderr, NULL, FILE, etc. */
  23.  
  24. #ifndef VMS
  25. #ifdef SYSV
  26. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  27. #else
  28. #include <strings.h>        /* strlen, strcat, strcpy, rindex */
  29. #endif
  30. #else
  31. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  32. #endif
  33.  
  34. #include <time.h>        /* needed for asctime() */
  35. #include <X11/Xlib.h>        /* get X types and constants */
  36. #include <X11/Xutil.h>        /* X window manager, visual stuff */
  37. #include "hfiles/constant.h"    /* define codes */
  38. #include "hfiles/color.h"    /* colors used by cursor */
  39. #include "hfiles/cursor.h"    /* define cursor parameter structures */
  40. #include "hfiles/define.h"    /* define SZ_FNAME, MIN, MAX, etc. */
  41. #include "hfiles/image.h"    /* image description structure */
  42. #include "hfiles/edit.h"    /* define EditStruct */
  43.  
  44. #ifndef VMS
  45. #define    IRAFFILE    "frame.%d.%d"
  46. #else
  47. #define    IRAFFILE    "frame.%d_%d"
  48. #endif
  49.  
  50. char *ProsName = "saoimage.reg";
  51.  
  52. EditStruct *region_edit;
  53.  
  54. /*
  55.  * Subroutine:    write_regions
  56.  * Purpose:    Write region info to a file
  57.  */
  58. void write_regions ( cursor, image, output_type )
  59.      struct cursorRec *cursor;
  60.      struct imageRec *image;
  61.      int output_type;        /* i: SOP_Imtool or SOP_PROS */
  62. {
  63.   FILE *fd;
  64.   static int last_output_type = 0;
  65.   char fname[SZ_FNAME];
  66.   static void write_region_imtool();
  67.   int open_output_file();
  68.   EditStruct *init_edit_popup();
  69.   void set_path_iraf(), write_region_pros(), load_edit_struct(), timestamp();
  70.  
  71.   /* return if there is nothing to write */
  72.   if( cursor->next_region == NULL ) {
  73.     (void)fprintf(stderr, "WARNING: no saved cursors to write!\n");
  74.     return;
  75.   }
  76.   /* imtool type output is different */
  77.   if( output_type == SOP_Imtool ) {
  78.     if( last_output_type != SOP_Imtool ) {
  79.       /* get name of coordinate output file */
  80.       sprintf(fname, IRAFFILE, 1, 1);
  81.       set_path_iraf(fname);
  82.       if( region_edit == NULL )
  83.     region_edit = init_edit_popup(fname, SZ_FNAME);
  84.       else
  85.     load_edit_struct(region_edit, fname, strlen(fname));
  86.       last_output_type = SOP_Imtool;
  87.     }
  88.     /* open coordinate output file for writing */
  89.     if( open_output_file(&fd, region_edit, 0,
  90.              "Enter file name for IRAF list:") <= 0 )
  91.       return;
  92.     /* timestamp the first entry in the output file. */
  93.     timestamp(fd, image->filename);
  94.     /* put in a line to explain parameter list */
  95.     (void)fprintf(fd, "# (x y)\n");
  96.     write_region_imtool(fd, cursor->next_region, image);
  97.   } else {
  98.     if( last_output_type != SOP_PROS ) {
  99.       if( region_edit == NULL )
  100.     region_edit = init_edit_popup(ProsName, SZ_FNAME);
  101.       else
  102.     load_edit_struct(region_edit, ProsName, strlen(fname));
  103.       last_output_type = SOP_PROS;
  104.     }
  105.     /* open region output file for writing */
  106.     if( open_output_file(&fd, region_edit, 0,
  107.              "Enter file name for regions:") <= 0 )
  108.       return;
  109.     /* timestamp the first entry in the output file. */
  110.     timestamp(fd, image->filename);
  111.     /* put in a line to explain parameter list */
  112.     (void)fprintf(fd,"# shape x, y, [x dimension, y dimension], [angle]\n");
  113.     /* write the pros region specifications */
  114.     write_region_pros(fd, cursor->next_region);
  115.   }
  116.   (void)fclose(fd);
  117. }
  118.  
  119. /*
  120.  * Subroutine:    write_region_imtool
  121.  * Purpose:    Write region info in imtool's format (iraf "list")
  122.  * Note:    Output is a list of ascii pairs giving only center coords
  123.  */
  124. static void write_region_imtool ( fd, region, image )
  125.      FILE *fd;
  126.      struct cursorRec *region;
  127.      struct imageRec *image;
  128. {
  129.   if( region != NULL ) {
  130.     /* recurse first to reverse order of regions */
  131.     if( region->next_region != NULL )
  132.       write_region_imtool(fd, region->next_region, image);
  133.     if( image->index_base )
  134.       (void)fprintf(fd, "%d %d\n", RND(region->file.X), RND(region->file.Y));
  135.     else
  136.       (void)fprintf(fd, "%d %d\n", (int)region->file.X, (int)region->file.Y);
  137.   }
  138. }
  139.  
  140. /*
  141.  * Subroutine:    timestamp
  142.  * Purpose:    Write date and filename (if known) to region file
  143.  */
  144. void timestamp ( fd, imagename )
  145.      FILE *fd;
  146.      char *imagename;
  147. {
  148.   char  line[SZ_LINE];
  149.   int len;
  150.   static void set_time_string();
  151.  
  152.   /* timestamp the first entry in the output file. */
  153.   set_time_string (line);
  154.   /* get rid of any trailing new line */
  155.   len = MAX(0, strlen(line) - 1);
  156.   if (line[len] == '\n')
  157.     line[len] = '\0';
  158.   /* print file name if one is given */
  159.   if ((imagename != NULL) && (strlen(imagename)) != 0) {
  160.     (void)fprintf(fd, "# %s\n", imagename);
  161.   }
  162.   /* print date and time */
  163.   (void)fprintf(fd, "# %s\n", line);
  164. }
  165.  
  166. /*
  167.  * Subroutine:    set_time_string (time)
  168.  * Purpose:    Print the time in the passed string
  169.  * UNIX calls:    time(), asctime(), localtime()
  170.  */
  171. static void set_time_string ( time_string )
  172.      char *time_string;
  173. {
  174.   long timeofday;
  175. #ifndef VMS
  176.   /* I don't know where this should be defined, it's not in <time.h> */
  177.   /* it's a time_t which is defined as a long in <sys/types.h> */
  178.   long time();
  179. #endif
  180.  
  181.   timeofday = time((long *)0);
  182.   /* note asctime(localtime()) can be replaced by ctime() in newer OS's */
  183.   (void)strcpy(time_string, asctime(localtime(&timeofday)));
  184. }
  185.