r72: Initial C++ versions
[ctsim.git] / src / rs2if.cpp
diff --git a/src/rs2if.cpp b/src/rs2if.cpp
new file mode 100644 (file)
index 0000000..becedae
--- /dev/null
@@ -0,0 +1,162 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: rs2if.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: rs2if.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.3  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.2  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
+**  Initial CVS import for first public release
+**
+**
+**
+**  This program is free software; you can redistribute it and/or modify
+**  it under the terms of the GNU General Public License (version 2) as
+**  published by the Free Software Foundation.
+**
+**  This program is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+**  GNU General Public License for more details.
+**
+**  You should have received a copy of the GNU General Public License
+**  along with this program; if not, write to the Free Software
+**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+******************************************************************************/
+
+/* FILE
+ *   rs2if.c                   Convert Raysum to image
+ *
+ * DATE
+ *   Apr 1999
+ */
+
+#include "ct.h"
+
+
+enum { O_VERBOSE, O_HELP, O_VERSION };
+
+static struct option my_options[] =
+{
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+
+void 
+rs2if_usage (const char *program)
+{
+  fprintf(stdout, "usage: %s in-rs-file out-if-file [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "This program converts a raysum file to a IF file\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "   --verbose   Verbose mode\n");
+  fprintf(stdout, "   --version   Print version\n");
+  fprintf(stdout, "   --help      Print this help message\n");
+}
+
+         
+
+int 
+rs2if_main (const int argc, char *const argv[])
+{
+  ImageFile *im;
+  RAYSUM *rs;
+  DETARRAY *detarray;
+  char *rs_name, *im_name;
+  int ix, iy;
+  int opt_v = 0;
+  extern int optind;
+  
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_VERBOSE:
+         opt_v = 1;
+         break;
+        case O_VERSION:
+#ifdef VERSION
+         fprintf(stdout, "Version %s\n", VERSION);
+#else
+         fprintf(stderr, "Unknown version number");
+#endif
+         exit(0);
+       case O_HELP:
+       case '?':
+         rs2if_usage(argv[0]);
+         return (0);
+       default:
+         rs2if_usage(argv[0]);
+         return (1);
+       }
+    }
+  
+  if (argc - optind != 2) {
+    rs2if_usage(argv[0]);
+    return (1);
+  }
+
+  rs_name = argv[optind];
+  im_name = argv[optind + 1];
+
+  if (file_exists(rs_name) == FALSE) {
+    fprintf (stderr, "Raysum file %s does not exist\n", rs_name);
+    return (1);
+  }
+  
+  rs = raysum_open (rs_name);
+
+  if (opt_v)
+    {
+      printf ("Number of detectors: %d\n", rs->ndet);
+      printf ("    Number of views: %d\n", rs->nview);
+      printf ("             Remark: %s\n", rs->remark);
+    }
+  
+  im = new ImageFile (im_name, rs->ndet, rs->nview);
+  
+  detarray = detarray_alloc (rs->ndet);
+  ImageFileArray v = im->adf.getArray();
+
+  for (iy = 0; iy < rs->nview; iy++)
+    {
+      detarray_read (rs, detarray, iy);
+      for (ix = 0; ix < rs->ndet; ix++)
+       {
+         v[ix][iy] = detarray->detval[ix];
+       }
+    }
+  detarray_free (detarray);
+  raysum_close (rs);
+
+  im->adf.fileCreate ();
+  im->adf.arrayDataWrite ();
+  im->adf.labelAdd (Array2dFileLabel::L_HISTORY, rs->remark, rs->calctime);
+  im->adf.labelAdd (Array2dFileLabel::L_HISTORY, "Conversion from .rs to .idf");
+  im->adf.fileClose ();
+  
+  return(0);
+}
+
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (rs2if_main(argc, argv));
+}
+#endif