r72: Initial C++ versions
authorKevin M. Rosenberg <kevin@rosenberg.net>
Wed, 7 Jun 2000 02:29:05 +0000 (02:29 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Wed, 7 Jun 2000 02:29:05 +0000 (02:29 +0000)
13 files changed:
src/ctrec.cpp [new file with mode: 0644]
src/ctsim.cpp [new file with mode: 0644]
src/if-1.cpp [new file with mode: 0644]
src/ifinfo.cpp [new file with mode: 0644]
src/phm2if.cpp [new file with mode: 0644]
src/phm2rs.cpp [new file with mode: 0644]
src/phm2sdf.cpp [new file with mode: 0644]
src/rs2if.cpp [new file with mode: 0644]
src/rs2sdf.cpp [new file with mode: 0644]
src/sdf-1.cpp [new file with mode: 0644]
src/sdf-2.cpp [new file with mode: 0644]
src/sdf2img.cpp [new file with mode: 0644]
src/sdfinfo.cpp [new file with mode: 0644]

diff --git a/src/ctrec.cpp b/src/ctrec.cpp
new file mode 100644 (file)
index 0000000..f48fc58
--- /dev/null
@@ -0,0 +1,482 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: ctrec.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: ctrec.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.13  2000/06/05 01:33:25  kevin
+**  BSpline changes
+**
+**  Revision 1.12  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.11  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.10  2000/05/11 01:06:30  kevin
+**  Changed sprintf to snprintf
+**
+**  Revision 1.9  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.8  2000/05/04 18:16:34  kevin
+**  renamed filter definitions
+**
+**  Revision 1.7  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  Revision 1.6  2000/05/02 15:31:47  kevin
+**  code cleaning
+**
+**  Revision 1.5  2000/04/30 11:41:06  kevin
+**  Cleaned up debugging code
+**
+**  Revision 1.4  2000/04/30 10:13:27  kevin
+**  Fixed MPI bugs
+**
+**  Revision 1.3  2000/04/30 04:06:13  kevin
+**  Update Raysum i/o routines
+**  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
+**
+**  Revision 1.2  2000/04/29 23:24:56  kevin
+**  *** empty log message ***
+**
+**  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
+ *   ctrec.c           Reconstruct an image from raysums
+ *
+ * DATE
+ *   Aug 84
+ *   Jul 99  -- Converted to ANSI C
+ *              Added MPI parallel processing
+ */
+
+#include "ct.h"
+
+enum {O_INTERP, O_FILTER, O_FILTER_PARAM, O_BACKPROJ, O_VERBOSE, O_TRACE, O_HELP, O_DEBUG, O_VERSION};
+
+static struct option my_options[] =
+{
+  {"interp", 1, 0, O_INTERP},
+  {"filter", 1, 0, O_FILTER},
+  {"filter-param", 1, 0, O_FILTER_PARAM},
+  {"backproj", 1, 0, O_BACKPROJ},
+  {"trace", 1, 0, O_TRACE},
+  {"debug", 0, 0, O_DEBUG},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+ctrec_usage (const char *program)
+{
+  fprintf(stdout,"usage: %s raysum-file image-file nx-image ny-image [OPTIONS]\n", kbasename(program));
+  fprintf(stdout,"Image reconstruction from raysum projections\n");
+  fprintf(stdout,"\n");
+  fprintf(stdout,"   raysum-file     Input raysum file\n");
+  fprintf(stdout,"   image-file      Output image file in SDF2D format\n");
+  fprintf(stdout,"   nx-image        Number of columns in output image\n");
+  fprintf(stdout,"   ny-image        Number of rows in output image\n");
+  fprintf(stdout,"   --interp        Interpolation method during backprojection\n");
+  fprintf(stdout,"       nearest     Nearest neighbor interpolation\n");
+  fprintf(stdout,"       linear      Linear interpolation\n");
+#if HAVE_BSPLINE_INTERP
+  fprintf(stdout,"       bspline     B-spline interpolation\n");
+#endif
+  fprintf(stdout,"    --filter       Filter name\n");
+  fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting (default)\n");
+  fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
+  fprintf(stdout,"       abs_cos       Abs * Cosine\n");
+  fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
+  fprintf(stdout,"       shepp         Shepp-Logan\n");
+  fprintf(stdout,"       bandlimit     Bandlimiting\n");
+  fprintf(stdout,"       sinc          Sinc\n");
+  fprintf(stdout,"       cos           Cosine\n");
+  fprintf(stdout,"       triangle      Triangle\n");
+  fprintf(stdout,"       hamming       Hamming\n");
+  fprintf(stdout,"    --backproj     Backprojection Method\n");
+  fprintf(stdout,"       trig        Trigometric functions at every point\n");
+  fprintf(stdout,"       table       Trigometric functions with precalculated table\n");
+  fprintf(stdout,"       diff        Difference method\n");
+  fprintf(stdout,"       diff2       Optimized difference method (default)\n");
+  fprintf(stdout,"       idiff2      Optimized difference method with integer math\n");
+  fprintf(stdout,"    --filter-param Alpha level for Hamming filter\n");
+  fprintf(stdout,"    --trace        Set tracing to level\n");
+  fprintf(stdout,"         none      No tracing (default)\n");
+  fprintf(stdout,"         text      Text level tracing\n");
+  fprintf(stdout,"         phm       Trace phantom\n");
+  fprintf(stdout,"         rays      Trace allrays\n");
+  fprintf(stdout,"         plot      Trace plotting\n");
+  fprintf(stdout,"         clipping  Trace clipping\n");
+  fprintf(stdout,"    --verbose      Turn on verbose mode\n");
+  fprintf(stdout,"    --debug        Turn on debug mode\n");
+  fprintf(stdout,"    --version      Print version\n");
+  fprintf(stdout,"    --help         Print this help message\n");
+}
+
+
+#ifdef MPI_CT
+static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int debug);
+#endif
+
+static void print_raysum_info(const RAYSUM *rs);
+
+int 
+ctrec_main (const int argc, char *const argv[])
+{
+  IMAGE *im_global = NULL;
+  RAYSUM *rs_global = NULL;
+  char *rs_name, *im_filename = NULL;
+  char remark[MAXREMARK];
+  char filt_name[80];
+  int nx, ny;
+  double time_start = 0, time_end = 0;
+  char *endptr;
+  int opt_verbose = 0;
+  int opt_debug = 0;
+  int opt_trace = TRACE_NONE;
+  double opt_filter_param = 1;
+  FilterType opt_filter = FILTER_ABS_BANDLIMIT;
+  InterpolationType opt_interp = I_LINEAR;
+  int opt_interp_param = 1;
+  BackprojType opt_backproj = O_BPROJ_DIFF2;
+#ifdef MPI_CT
+  IMAGE *im_local;
+  RAYSUM *rs_local;
+  int mpi_argc = argc;
+  char **mpi_argv = (char **) argv;
+  int mpi_nview, mpi_ndet;
+  double mpi_detinc, mpi_rotinc, mpi_phmlen;
+  double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
+  int ix;
+
+  MPI_Init(&mpi_argc, &mpi_argv);
+  MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
+  MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
+  MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
+
+  if (mpi_ct.nproc > MPI_MAX_PROCESS) {
+    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
+             mpi_ct.nproc, MPI_MAX_PROCESS);
+    exit(1);
+  }
+#endif
+
+#ifdef MPI_CT
+  time_start = MPI_Wtime();
+#else
+  time_start = td_current_sec();
+#endif
+
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) {
+#endif
+    while (1) {
+      int c = getopt_long(argc, argv, "", my_options, NULL);
+      char *endptr = NULL;
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_INTERP:
+         if ((opt_interp = opt_set_interpolation(optarg)) < 0) {
+           ctrec_usage(argv[0]);
+           return (1);
+         }
+         break;
+       case O_FILTER:
+         if ((opt_filter = opt_set_filter(optarg)) < 0) {
+           ctrec_usage(argv[0]);
+           return (1);
+         }
+         break;
+       case O_BACKPROJ:
+         if ((opt_backproj = opt_set_backproj(optarg)) < 0) {
+             ctrec_usage(argv[0]);
+             return (1);
+         }
+         break;
+       case O_FILTER_PARAM:
+         opt_filter_param = strtod(optarg, &endptr);
+         if (endptr != optarg + strlen(optarg)) {
+           ctrec_usage(argv[0]);
+         }
+         break;
+       case O_VERBOSE:
+         opt_verbose = 1;
+         break;
+       case O_DEBUG:
+         opt_debug = 1;
+         break;
+       case O_TRACE:
+         if ((opt_trace = opt_set_trace(optarg)) < 0) {
+           ctrec_usage(argv[0]);
+           return (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 '?':
+         ctrec_usage(argv[0]);
+         return (0);
+       default:
+         ctrec_usage(argv[0]);
+         return (1);
+       }
+    }
+  
+    if (optind + 4 != argc) {
+      ctrec_usage(argv[0]);
+      return (1);
+    }
+
+    rs_name = argv[optind];
+  
+    im_filename = argv[optind + 1];
+  
+    nx = strtol(argv[optind + 2], &endptr, 10);
+    ny = strtol(argv[optind + 3], &endptr, 10);
+  
+    if (opt_filter == FILTER_G_HAMMING || opt_filter == FILTER_ABS_G_HAMMING)
+      snprintf (filt_name, sizeof(filt_name), "%s: alpha = %.2f",
+              name_of_filter (opt_filter), opt_filter_param); 
+    else
+      snprintf (filt_name, sizeof(filt_name), "%s", name_of_filter (opt_filter));
+  
+    snprintf (remark, sizeof(remark), "Reconstruct: %dx%d, %s, %s, %s",
+            nx, ny, filt_name, name_of_interpolation (opt_interp), name_of_backproj(opt_backproj));
+  
+    if (opt_verbose)
+      fprintf (stdout, "%s\n", remark);
+#ifdef MPI_CT
+  }
+#endif
+
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) {
+    rs_global = raysum_open (rs_name);
+    raysum_read (rs_global);
+    if (opt_verbose)
+      print_raysum_info(rs_global);
+
+    mpi_ndet = rs_global->ndet;
+    mpi_nview = rs_global->nview;
+    mpi_detinc = rs_global->det_inc;
+    mpi_phmlen = rs_global->phmlen;
+    mpi_rotinc = rs_global->rot_inc;
+  }
+
+  mpi_t1 = MPI_Wtime();
+  MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_interp, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_interp_param, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_backproj, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&mpi_ndet, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&mpi_nview, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&mpi_detinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&mpi_phmlen, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&mpi_rotinc, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&nx, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&ny, 1, MPI_INT, 0, mpi_ct.comm);
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
+  }
+
+  mpi_ct_calc_work_units(mpi_nview);
+
+  rs_local = raysum_create (NULL, mpi_ct.local_work_units[mpi_ct.my_rank], mpi_ndet);
+
+  rs_local->ndet = mpi_ndet;
+  rs_local->nview = mpi_nview;
+  rs_local->det_inc = mpi_detinc;
+  rs_local->phmlen = mpi_phmlen;
+  rs_local->rot_inc = mpi_rotinc;
+
+  if (opt_verbose)
+    mpi_t1 = MPI_Wtime();
+  mpi_scatter_rs(rs_global, rs_local, opt_debug);
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to scatter rs = %f secs, Max time = %f sec\n", mpi_t, mpi_t_g);
+  }
+
+  if (mpi_ct.my_rank == 0) {
+    im_global = image_create (im_filename, nx, ny);
+    sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
+    sdf_add_empty_label (im_global->dfp_2d->dfp);
+  }
+  im_local = image_create (NULL, nx, ny);
+
+#else
+  rs_global = raysum_open (rs_name);
+  raysum_read (rs_global);
+  if (opt_verbose)
+    print_raysum_info(rs_global);
+
+  im_global = image_create (im_filename, nx, ny);
+  sdf_add_label (LT_HISTORY, rs_global->remark, rs_global->calctime, im_global->dfp_2d->dfp);
+  sdf_add_empty_label (im_global->dfp_2d->dfp);
+#endif
+
+#ifdef MPI_CT
+  mpi_t1 = MPI_Wtime();
+  image_reconst (im_local, rs_local, opt_filter, opt_filter_param, 
+                opt_interp, opt_interp_param, opt_backproj, opt_trace);
+  mpi_t2 = MPI_Wtime();
+  mpi_t = mpi_t2 - mpi_t1;
+  MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+  if (mpi_ct.my_rank == 0 && opt_verbose)
+    printf("Time to reconstruct = %f, Max time = %f\n", mpi_t, mpi_t_g);
+#else
+  image_reconst (im_global, rs_global, opt_filter, opt_filter_param, 
+                opt_interp, opt_interp_param, opt_backproj, opt_trace);
+#endif
+
+#ifdef MPI_CT
+  if (opt_verbose)
+    mpi_t1 = MPI_Wtime();
+
+  for (ix = 0; ix < im_local->nx; ix++) {
+    void *recvbuf = NULL;
+    if (mpi_ct.my_rank == 0)
+      recvbuf = im_global->v[ix];
+
+    MPI_Reduce(im_local->v[ix], recvbuf, im_local->ny, MPI_FLOAT, MPI_SUM, 0, mpi_ct.comm);
+  }
+
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to reduce image = %f secs, max time = %f\n", mpi_t, mpi_t_g);
+  }
+
+  if (mpi_ct.my_rank == 0)
+      time_end = MPI_Wtime();
+#else
+  time_end = td_current_sec();
+#endif
+
+
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0)
+#endif
+    {
+      raysum_close (rs_global);
+      strncpy (im_global->remark, remark, sizeof(im_global->remark));
+      im_global->calctime = time_end - time_start;
+      image_save (im_global);
+      if (opt_verbose)
+         fprintf (stdout, "Time active = %.2f\n", im_global->calctime);
+    }
+
+#ifdef MPI_CT
+       MPI_Finalize();
+#endif
+
+       return (0);
+}
+
+
+#ifdef MPI_CT
+static void mpi_scatter_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
+{
+  int iproc;
+  int end_work_unit;
+  int iw = 0;
+
+  if (mpi_ct.my_rank == 0) {
+    for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
+      end_work_unit = mpi_ct.start_work_unit[iproc] + mpi_ct.local_work_units[iproc]  - 1;
+
+      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
+       MPI_Send(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm);
+       MPI_Send(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm);
+       MPI_Send(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm);
+      }
+    }
+  }
+
+  if (opt_debug)
+    fprintf(stdout, "Receiving rs data in process %d\n", mpi_ct.my_rank);
+
+  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
+  for (iw = 0; iw <= end_work_unit; iw++) {
+    MPI_Status status;
+
+    MPI_Recv(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm, &status);
+    MPI_Recv(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm, &status);
+    MPI_Recv(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm, &status);
+  }
+  rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
+  if (opt_debug) {
+    MPI_Barrier(MPI_COMM_WORLD);
+    fprintf(stdout, "Done with mpi_scatter_rs in process %2d\n", mpi_ct.my_rank);
+  }
+}
+
+#endif
+
+static void print_raysum_info(const RAYSUM *rs)
+{
+  printf ("Number of detectors: %d\n", rs->ndet);
+  printf ("    Number of views: %d\n", rs->nview);
+  printf ("             Remark: %s\n", rs->remark);
+  printf ("             phmlen: %f\n", rs->phmlen);
+  printf ("          det_start: %f\n", rs->det_start);
+  printf ("            det_inc: %f\n", rs->det_inc);
+  printf ("          rot_start: %f\n", rs->rot_start);
+  printf ("            rot_inc: %f\n", rs->rot_inc);
+}
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (ctrec_main(argc, argv));
+}
+#endif
+
diff --git a/src/ctsim.cpp b/src/ctsim.cpp
new file mode 100644 (file)
index 0000000..bc3e0ba
--- /dev/null
@@ -0,0 +1,10 @@
+#include <ct.h>
+#include <iostream>
+
+int
+main (int argc, char **argv)
+{
+  cout << "Hello, CTSim!" << endl;
+
+  return (0);
+}
diff --git a/src/if-1.cpp b/src/if-1.cpp
new file mode 100644 (file)
index 0000000..16e3b82
--- /dev/null
@@ -0,0 +1,215 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: if-1.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: if-1.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.6  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.5  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.4  2000/05/09 14:52:27  kevin
+**  added sqr and sqrt functions
+**
+**  Revision 1.3  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.2  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  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
+ *   if-1.c             Filter a single IF file
+ */
+
+#include "ct.h"
+
+enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
+
+static struct option my_options[] =
+{
+  {"invert", 0, 0, O_INVERT},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"log", 0, 0, O_LOG},
+  {"exp", 0, 0, O_EXP},
+  {"sqr", 0, 0, O_SQR},
+  {"sqrt", 0, 0, O_SQRT},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+if1_usage (const char *program)
+{
+  fprintf(stdout, "if1_usage: %s infile outfile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "Generate a IF file from a IF file\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "     --invert   Invert image\n");
+  fprintf(stdout, "     --log      Natural logrithm of image\n");
+  fprintf(stdout, "     --exp      Natural exponential of image\n");
+  fprintf(stdout, "     --sqr      Square of image\n");
+  fprintf(stdout, "     --sqrt     Square root of image\n");
+  fprintf(stdout, "     --verbose  Verbose modem\n");
+  fprintf(stdout, "     --version  Print version\n");
+  fprintf(stdout, "     --help     Print this help message\n");
+}
+
+int 
+if1_main (int argc, char *const argv[])
+{
+  ImageFile *im_in;
+  ImageFile *im_out;
+  char *in_file;
+  char *out_file;
+  int opt_verbose = 0;
+  int opt_invert = 0;
+  int opt_log = 0;
+  int opt_exp = 0;
+  int opt_sqr = 0;
+  int opt_sqrt = 0;
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_INVERT:
+         opt_invert = 1;
+         break;
+       case O_LOG:
+         opt_log = 1;
+         break;
+       case O_SQR:
+         opt_sqr = 1;
+         break;
+       case O_SQRT:
+         opt_sqrt = 1;
+         break;
+       case O_EXP:
+         opt_exp = 1;
+         break;
+       case O_VERBOSE:
+         opt_verbose = 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 '?':
+         if1_usage(argv[0]);
+         return (0);
+       default:
+         if1_usage(argv[0]);
+         return (1);
+       }
+    }
+
+  if (optind + 2 != argc)
+    {
+      if1_usage(argv[0]);
+      return (1);
+    }
+  
+  in_file = argv[optind];
+  out_file = argv[optind + 1];
+
+
+  string histString;
+
+  if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
+    int ix, iy;
+
+    im_in = new ImageFile (in_file);
+    im_in->adf.fileRead ();
+    int nx = im_in->adf.nx;
+    int ny = im_in->adf.ny;
+    im_out = new ImageFile (out_file, nx, ny);
+    im_out->adf.fileCreate ();
+
+    ImageFileArray vIn = im_in->adf.getArray();
+    ImageFileArray vOut = im_out->adf.getArray();
+
+    if (opt_invert) {
+      for (ix = 0; ix < nx; ix++)
+        for (iy = 0; iy < ny; iy++)
+          vOut[ix][iy] = - vIn[ix][iy];
+      histString = "Invert transformation";
+    }
+    if (opt_log) {
+      for (ix = 0; ix < nx; ix++)
+        for (iy = 0; iy < ny; iy++)
+          vOut[ix][iy] = log (vIn[ix][iy]);
+      histString = "Log transformation";
+    }
+    if (opt_exp) {
+      for (ix = 0; ix < nx; ix++)
+        for (iy = 0; iy < ny; iy++)
+          vOut[ix][iy] = exp (vIn[ix][iy]);
+      histString = "Exp transformation";
+    }
+    if (opt_sqr) {
+      for (ix = 0; ix < nx; ix++)
+        for (iy = 0; iy < ny; iy++)
+          vOut[ix][iy] = vIn[ix][iy] * vIn[ix][iy];
+      histString = "Sqr transformation";
+    }
+    if (opt_sqrt) {
+      for (ix = 0; ix < nx; ix++)
+        for (iy = 0; iy < ny; iy++)
+          vOut[ix][iy] = sqrt (vIn[ix][iy]);
+      histString = "Sqrt transformation";
+    }
+
+    im_out->adf.arrayDataWrite ();
+    for (int i = 0; i < im_in->adf.getNumLabels(); i++) {
+      Array2dFileLabel l;
+      im_in->adf.labelRead (l, i);
+      im_out->adf.labelAdd (l);
+    }
+    im_out->adf.labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
+    im_out->adf.fileClose ();
+  }
+
+  return (0);
+}
+
+#ifndef NO_MAIN
+int 
+main (int argc, char *const argv[])
+{
+  return (if1_main(argc, argv));
+}
+#endif
+
diff --git a/src/ifinfo.cpp b/src/ifinfo.cpp
new file mode 100644 (file)
index 0000000..8ef592a
--- /dev/null
@@ -0,0 +1,246 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: ifinfo.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: ifinfo.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
+ *   ifinfo.c             Display info on sdf files
+ */
+
+#include "ct.h"
+
+enum { O_LABELS, O_STATS, O_NO_STATS, O_NO_LABELS, O_VERBOSE, O_HELP, O_VERSION, O_DEBUG };
+
+static struct option my_options[] =
+{
+  {"labels", 0, 0, O_LABELS},
+  {"no-labels", 0, 0, O_NO_LABELS},
+  {"stats", 0, 0, O_STATS},
+  {"no-stats", 0, 0, O_NO_STATS},
+  {"debug", 0, 0, O_DEBUG},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+ifinfo_usage (const char *program)
+{
+  fprintf(stdout, "usage: %s infile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "Imagefile information\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "     infile       Name of input SDF file\n");
+  fprintf(stdout, "     --display    Display image\n");
+  fprintf(stdout, "     --labels     Print image labels (default)\n");
+  fprintf(stdout, "     --no-labels  Do not print image labels\n");
+  fprintf(stdout, "     --stats      Print image statistics (default)\n");
+  fprintf(stdout, "     --no-stats   Do not print image statistics\n");
+  fprintf(stdout, "     --debug      Debug mode\n");
+  fprintf(stdout, "     --verbose    Verbose mode\n");
+  fprintf(stdout, "     --version    Print version\n");
+  fprintf(stdout, "     --help       Print this help message\n");
+}
+
+int 
+ifinfo_main (int argc, char *const argv[])
+{
+  ImageFile *im;
+  char *in_file;
+  int opt_verbose = 0;
+  int opt_stats = 1;
+  int opt_labels = 1;
+  int opt_debug = 0;
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_LABELS:
+         opt_labels = 1;
+         break;
+       case O_STATS:
+         opt_stats = 1;
+         break;
+       case O_NO_LABELS:
+         opt_labels = 0;
+         break;
+       case O_NO_STATS:
+         opt_stats = 0;
+         break;
+       case O_VERBOSE:
+         opt_verbose = 1;
+         break;
+       case O_DEBUG:
+         opt_debug = 0;
+         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 '?':
+         ifinfo_usage(argv[0]);
+         return (0);
+       default:
+         ifinfo_usage(argv[0]);
+         return (1);
+       }
+    }
+
+  if (optind + 1 != argc)
+    {
+      ifinfo_usage(argv[0]);
+      return (1);
+    }
+  
+  in_file = argv[optind];
+
+  im = new ImageFile (in_file);
+  im->adf.fileRead ();
+
+  if (opt_labels) 
+    {
+      int nlabels = im->adf.getNumLabels();
+      int i;
+
+      for (i = 0; i < nlabels; i++)
+       {
+           Array2dFileLabel label;
+           string str;
+
+           im->adf.labelRead (label, i);
+           label.getDateString (str);
+
+           if (label.getLabelType() == Array2dFileLabel::L_HISTORY) {
+             cout << "History: " << label.getLabelString() << endl;
+             cout << "  calc time = " << label.getCalcTime() << " secs" << endl;
+             cout << "  Timestamp = " << str << endl;
+           } else if (label.getLabelType() == Array2dFileLabel::L_USER) {
+             cout << "Note: " <<  label.getLabelString() << endl;
+             cout << "  Timestamp = %s" << str << endl;
+         }
+       }
+    }
+
+  if (opt_stats)
+    {
+      double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
+      double mode = 0, mean = 0, stddev = 0;
+      double spread;
+      int hist[256];
+      int ibin, nbin = 256;
+      int max_bin, max_binindex; 
+      double maxbin;
+      int ix, iy;
+
+      maxbin = nbin - 1;
+      ImageFileArray v = im->adf.getArray();
+      int nx = im->adf.nx;
+      int ny = im->adf.ny;
+
+      for (ix = 0; ix < nx; ix++)
+       {
+         for (iy = 0; iy < ny; iy++)
+           {
+             if (v[ix][iy] > maxfound)
+               maxfound = v[ix][iy];
+             if (v[ix][iy] < minfound)
+               minfound = v[ix][iy];
+             mean += v[ix][iy];
+           }
+       }
+      spread = maxfound - minfound;
+      if (spread == 0)
+       mode = minfound;
+      else
+       {
+         for (ibin = 0; ibin < nbin; ibin++)
+           hist[ibin] = 0;
+         for (ix = 0; ix < nx; ix++)
+           {
+             for (iy = 0; iy < ny; iy++)
+               {
+                 int b = (int) ((((v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
+                 hist[b]++;
+               }
+           }
+         max_binindex = 0;
+         max_bin = -1;
+         for (ibin = 0; ibin < nbin; ibin++)
+           {
+             if (hist[ibin] > max_bin)
+               {
+                 max_bin = hist[ibin];
+                 max_binindex = ibin;
+               }
+           }
+         mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
+       }
+
+      mean /= nx * ny;
+      for (ix = 0; ix < nx; ix++)
+       {
+         for (iy = 0; iy < ny; iy++)
+           {
+             double diff = (v[ix][iy] - mean);
+             stddev += diff * diff;
+           }
+       }
+      stddev = sqrt(stddev / (nx * ny));
+      fprintf(stdout,"nx=%d\n", nx);
+      fprintf(stdout,"ny=%d\n", ny);
+      fprintf(stdout,"min=%f\n", minfound);
+      fprintf(stdout,"max=%f\n", maxfound);
+      fprintf(stdout,"mean=%f\n", mean);
+      fprintf(stdout,"mode=%f\n", mode);
+      fprintf(stdout,"stddev=%f\n", stddev);
+    }
+  
+  return (0);
+}
+
+#ifndef NO_MAIN
+int 
+main (int argc, char *const argv[])
+{
+  return (ifinfo_main(argc, argv));
+}
+#endif
diff --git a/src/phm2if.cpp b/src/phm2if.cpp
new file mode 100644 (file)
index 0000000..9d32f1e
--- /dev/null
@@ -0,0 +1,503 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: phm2if.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: phm2if.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.10  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.9  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.8  2000/05/11 01:06:30  kevin
+**  Changed sprintf to snprintf
+**
+**  Revision 1.7  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.6  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  Revision 1.5  2000/04/30 19:17:54  kevin
+**  *** empty log message ***
+**
+**  Revision 1.4  2000/04/30 04:06:13  kevin
+**  Update Raysum i/o routines
+**  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
+**
+**  Revision 1.3  2000/04/28 18:19:01  kevin
+**  removed unused files
+**
+**  Revision 1.2  2000/04/28 13:50:45  kevin
+**  Removed Makefile Makefile.in that are automatically generated by autoconf
+**
+**  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
+ *   phm2sdf.c                 Generate a SDF image from a phantom
+ *
+ * HISTORY
+ *   1984 - Final DOS verion
+ *   1999 - First UNIX version
+ */
+
+#include "ct.h"
+
+enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP, 
+       O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
+
+static struct option my_options[] = 
+{
+  {"phantom", 1, 0, O_PHANTOM},
+  {"phmfile", 1, 0, O_PHMFILE},
+  {"desc", 1, 0, O_DESC},
+  {"nsample", 1, 0, O_NSAMPLE},
+  {"filter", 1, 0, O_FILTER},
+  {"filter-domain", 1, 0, O_FILTER_DOMAIN},
+  {"filter-bw", 1, 0, O_FILTER_BW},
+  {"filter-param", 1, 0, O_FILTER_PARAM},
+  {"trace", 1, 0, O_TRACE},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"debug", 0, 0, O_DEBUG},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+phm2sdf_usage (const char *program)
+{
+  fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program)); 
+  fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
+  fprintf(stdout,"\n");
+  fprintf(stdout,"     outfile         Name of output file for image\n");
+  fprintf(stdout,"     nx              Number of pixels X-axis\n");
+  fprintf(stdout,"     ny              Number of pixels Y-axis\n");
+  fprintf(stdout,"     --phantom       Phantom to use for projection\n");
+  fprintf(stdout,"        herman       Herman head phantom\n");
+  fprintf(stdout,"        rowland      Rowland head phantom\n");
+  fprintf(stdout,"        browland     Bordered Rowland head phantom\n");
+  fprintf(stdout,"        unitpulse    Unit pulse phantom\n");
+  fprintf(stdout,"     --phmfile       Generate Phantom from phantom file\n");
+  fprintf(stdout,"     --filter        Generate Phantom from a filter function\n");
+  fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting\n");
+  fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
+  fprintf(stdout,"       abs_cos       Abs * Cosine\n");
+  fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
+  fprintf(stdout,"       shepp         Shepp-Logan\n");
+  fprintf(stdout,"       bandlimit     Bandlimiting\n");
+  fprintf(stdout,"       sinc          Sinc\n");
+  fprintf(stdout,"       cos           Cosine\n");
+  fprintf(stdout,"       triangle      Triangle\n");
+  fprintf(stdout,"       hamming       Hamming\n");
+  fprintf(stdout,"     --filter-param  Alpha level for Hamming filter\n");
+  fprintf(stdout,"     --filter-domain Set domain of filter\n");
+  fprintf(stdout,"         spatial     Spatial domain (default)\n");
+  fprintf(stdout,"         freq        Frequency domain\n");
+  fprintf(stdout,"     --filter-bw     Filter bandwidth (default = 1)\n");
+  fprintf(stdout,"     --desc          Description of raysum\n");
+  fprintf(stdout,"     --nsample       Number of samples per axis per pixel (default = 1)\n");
+  fprintf(stdout,"     --trace         Trace level to use\n");
+  fprintf(stdout,"        none         No tracing (default)\n");
+  fprintf(stdout,"        text         Trace text level\n");
+  fprintf(stdout,"        phm          Trace phantom\n");
+  fprintf(stdout,"        rays         Trace rays\n");
+  fprintf(stdout,"        plot         Trace plot\n");
+  fprintf(stdout,"        clipping     Trace clipping\n");
+  fprintf(stdout,"     --debug         Debug mode\n");
+  fprintf(stdout,"     --verbose       Verbose mode\n");
+  fprintf(stdout,"     --version       Print version\n");
+  fprintf(stdout,"     --help          Print this help message\n");
+}
+
+#ifdef MPI_CT
+void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug);
+#endif
+
+int 
+phm2sdf_main (const int argc, char *const argv[])
+{
+  ImageFile *im_global = NULL;
+  PHANTOM *phm = NULL;
+  int opt_nx = 0, opt_ny = 0;
+  int opt_nsample = 1;
+  int opt_phmnum = -1;
+  FilterType opt_filter = static_cast<FilterType>(-1);
+  DomainType opt_filter_domain = D_SPATIAL;
+  char *opt_outfile = NULL;
+  int opt_debug = 0;
+  char str[256];
+  char opt_desc[256], opt_phmfilename[256];
+  char *endstr, *endptr;
+  double opt_filter_param = 1;
+  double opt_filter_bw = 1.;
+  int opt_trace = TRACE_NONE;
+  int opt_verbose = 0;
+  double time_start=0, time_end=0;
+#ifdef MPI_CT
+  ImageFile *im_local = NULL;
+  int mpi_argc = argc;
+  char **mpi_argv = (char **) argv;
+  double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
+
+  MPI_Init(&mpi_argc, &mpi_argv);
+  MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
+  MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
+  MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
+
+  if (mpi_ct.nproc > MPI_MAX_PROCESS) {
+    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
+             mpi_ct.nproc, MPI_MAX_PROCESS);
+  }
+#endif
+
+#ifdef MPI_CT
+  time_start = MPI_Wtime();
+#else
+  time_start = td_current_sec();
+#endif
+  
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) {
+#endif
+
+    strcpy(opt_desc, "");
+    strcpy(opt_phmfilename, "");
+    while (1) {
+      int c = getopt_long(argc, argv, "", my_options, NULL);
+      char *endptr = NULL;
+      char *endstr;
+      
+      if (c == -1)
+       break;
+      
+      switch (c) {
+      case O_PHANTOM:
+       if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_PHMFILE:
+       strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
+       phm = phm_create_from_file(opt_phmfilename);
+#ifdef MPI_CT
+       if (mpi_ct.my_rank == 0) 
+         fprintf(stderr, "Can't use phantom from file in MPI mode\n");
+       return (1);
+#endif
+       break;
+      case O_VERBOSE:
+       opt_verbose = 1;
+       break;
+      case O_DEBUG:
+       opt_debug = 1;
+       break;
+      case O_TRACE:
+       if ((opt_trace = opt_set_trace(optarg)) < 0) {
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_FILTER:
+       if ((opt_filter = opt_set_filter(optarg)) < 0) {
+         phm2sdf_usage (argv[0]);
+         return (1);
+       }
+       break;
+      case O_FILTER_DOMAIN:
+       if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
+         phm2sdf_usage (argv[0]);
+         return (1);
+       }
+       break;
+      case O_DESC:
+       strncpy(opt_desc, optarg, sizeof(opt_desc));
+       break;
+      case O_FILTER_BW:
+       opt_filter_bw = strtod(optarg, &endptr);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_FILTER_PARAM:
+       opt_filter_param = strtod(optarg, &endptr);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_NSAMPLE:
+       opt_nsample = strtol(optarg, &endptr, 10);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --nsample to %s\n", optarg);
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+        case O_VERSION:
+#ifdef VERSION
+         fprintf(stdout, "Version %s\n", VERSION);
+#else
+         fprintf(stderr, "Unknown version number");
+#endif
+      case O_HELP:
+      case '?':
+       phm2sdf_usage(argv[0]);
+       return (0);
+      default:
+       phm2sdf_usage(argv[0]);
+       return (1);
+      }
+    }
+    
+    if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
+      fprintf(stderr, "No phantom defined\n");
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+
+    if (optind + 3 != argc) {
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+    opt_outfile = argv[optind];
+    opt_nx = strtol(argv[optind+1], &endptr, 10);
+    endstr = argv[optind+1] + strlen(argv[optind+1]);
+    if (endptr != endstr) {
+      fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+    opt_ny = strtol(argv[optind+2], &endptr, 10);
+    endstr = argv[optind+2] + strlen(argv[optind+2]);
+    if (endptr != endstr) {
+      fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+    
+    snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
+    if (opt_phmfilename[0]) {
+      strncat(str, "phantom=", sizeof(str));
+      strncat(str, opt_phmfilename, sizeof(str));
+    }
+    else if (opt_phmnum != -1) {
+      strncat(str, "phantom=", sizeof(str));
+      strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
+    }
+    else if (opt_filter != -1) {
+      strncat(str, "filter=", sizeof(str));
+      strncat(str, name_of_filter(opt_filter), sizeof(str));
+      strncat(str, " - ", sizeof(str));
+      strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
+    }
+    if (opt_desc[0]) {
+      strncat(str, ": ", sizeof(str));
+      strncat(str, opt_desc, sizeof(str));
+    }
+    strncpy(opt_desc, str, sizeof(opt_desc));
+    
+    if (opt_verbose)
+      printf("Rasterize Phantom to Image\n\n");
+#ifdef MPI_CT
+  }
+#endif
+
+#ifdef MPI_CT
+  mpi_t1 = MPI_Wtime();
+  MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
+  }
+
+  mpi_ct_calc_work_units(opt_nx);
+
+  if (mpi_ct.my_rank == 0) {
+    im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
+    im_global->adf.fileCreate();
+  }
+
+  im_local = new ImageFile (opt_nx, opt_ny);
+#else
+  im_global = new ImageFile (opt_outfile, opt_nx, opt_ny);
+  im_global->adf.fileCreate ();
+#endif
+
+  if (opt_phmnum >= 0)
+      phm = phm_create (opt_phmnum);
+
+#ifdef MPI_CT
+  else {
+    if (mpi_ct.my_rank == 0)
+      fprintf(stderr, "phmnum < 0\n");
+    exit(1);
+  }
+#endif
+
+  ImageFileArray v = im_global->adf.getArray ();
+  double calctime = 0;
+
+#ifdef MPI_CT
+  if (phm->type == P_UNIT_PULSE) {
+    if (mpi_ct.my_rank == 0) {
+      v[opt_nx/2][opt_ny/2] = 1.;
+      calctime = 0;
+    }
+  } else if (opt_filter != -1) {
+    if (mpi_ct.my_rank == 0) {
+      image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
+      calctime = 0;
+    }
+  } else {
+    if (opt_verbose)
+      mpi_t1 = MPI_Wtime();
+    phm_to_image (phm, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
+                 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
+    if (opt_verbose) {
+      mpi_t2 = MPI_Wtime();
+      mpi_t = mpi_t2 - mpi_t1;
+      MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+      if (mpi_ct.my_rank == 0)
+       printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
+    }
+    mpi_gather_image(im_global, im_local, opt_debug);
+  }
+#else
+  if (phm->type == P_UNIT_PULSE) {
+    v[opt_nx/2][opt_ny/2] = 1.;
+    calctime = 0;
+  } else if (opt_filter != -1) {
+    // image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
+    calctime = 0;
+  } else {
+#if HAVE_SGP
+      if (opt_trace >= TRACE_PHM)
+       phm_show(phm);
+#endif
+      // phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace);
+  }
+#endif
+
+#ifdef MPI_CT
+  time_end = MPI_Wtime();
+#else
+  time_end = td_current_sec();
+#endif
+
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) 
+#endif
+  {
+    im_global->adf.arrayDataWrite ();
+    calctime = time_end - time_start;
+    im_global->adf.labelAdd (Array2dFileLabel::L_HISTORY, opt_desc, calctime);
+    im_global->adf.fileClose ();
+    if (opt_verbose)
+      fprintf (stdout, "Time to compile image = %.2f sec\n\n", calctime);
+  }
+
+  if (opt_trace >= TRACE_PHM)
+    {
+      double dmin, dmax;
+      int nscale;
+
+      printf ("Enter display size scale (nominal = 1): ");
+      scanf ("%d", &nscale);
+      printf ("Enter minimum and maximum densities (min, max): ");
+      scanf ("%lf %lf", &dmin, &dmax);
+      // image_display_scale (im_global, nscale, dmin, dmax);
+    }
+
+  phm_free (phm);
+
+  return (0);
+}
+
+
+
+#ifdef MPI_CT
+void mpi_gather_image (ImageFile *im_global, ImageFile *im_local, const int opt_debug)
+{
+  int iproc;
+  int end_work_unit;
+  int iw;
+
+  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
+  for (iw = 0; iw <= end_work_unit; iw++) {
+    MPI_Send(im_local->v[iw], im_local->ny, im_local->getMPIDataType(), 0, 0, mpi_ct.comm);
+  }
+
+  if (mpi_ct.my_rank == 0) {
+    for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
+      end_work_unit = mpi_ct.start_work_unit[iproc] 
+       + mpi_ct.local_work_units[iproc] 
+       - 1;
+
+      if (opt_debug) {
+       fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
+               mpi_ct.start_work_unit[iproc], end_work_unit);
+       fflush(stdout);
+      }
+
+
+      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
+       MPI_Status status;
+
+       MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
+      }
+    }
+  }
+
+}
+#endif
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (phm2sdf_main(argc, argv));
+}
+#endif
diff --git a/src/phm2rs.cpp b/src/phm2rs.cpp
new file mode 100644 (file)
index 0000000..1e15883
--- /dev/null
@@ -0,0 +1,433 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+** PURPOSE
+**   Generate raysum projections from phantom object
+**
+** HISTORY
+**   1984 -- Final DOS version  
+**   1999 -- First UNIX version
+**
+**  $Id: phm2rs.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: phm2rs.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.12  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.11  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.9  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.8  2000/05/04 18:16:34  kevin
+**  renamed filter definitions
+**
+**  Revision 1.7  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  Revision 1.6  2000/04/30 18:23:53  kevin
+**  Code cleaning
+**
+**  Revision 1.5  2000/04/30 10:13:27  kevin
+**  Fixed MPI bugs
+**
+**  Revision 1.4  2000/04/30 04:06:13  kevin
+**  Update Raysum i/o routines
+**  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
+**
+**  Revision 1.3  2000/04/29 23:24:56  kevin
+**  *** empty log message ***
+**
+**  Revision 1.2  2000/04/28 13:50:45  kevin
+**  Removed Makefile Makefile.in that are automatically generated by autoconf
+**
+**  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
+******************************************************************************/
+
+
+#include "ct.h"
+
+enum { O_PHANTOM, O_DESC, O_NRAY, O_ROTANGLE, O_PHMFILE,
+       O_TRACE, O_VERBOSE, O_HELP, O_DEBUG, O_VERSION };
+
+static struct option phm2rs_options[] = 
+{
+  {"phantom", 1, 0, O_PHANTOM},
+  {"phmfile", 1, 0, O_PHMFILE},
+  {"desc", 1, 0, O_DESC},
+  {"nray", 1, 0, O_NRAY},
+  {"rotangle", 1, 0, O_ROTANGLE},
+  {"trace", 1, 0, O_TRACE},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"debug", 0, 0, O_DEBUG},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+
+void 
+phm2rs_usage (const char *program)
+{
+#ifdef MPI_CT
+if (mpi_ct.my_rank == 0)
+  {
+#endif  
+  fprintf(stdout,"usage: %s outfile ndet nview [--phantom phantom-name] [--phmfile filename] [OPTIONS]\n", kbasename(program));
+  fprintf(stdout,"Calculate raysums (projections) through phantom object, either\n");
+  fprintf(stdout,"a predefined --phantom or a --phmfile\n");
+  fprintf(stdout,"\n");
+  fprintf(stdout,"     outfile      Name of output file for raysums\n");
+  fprintf(stdout,"     ndet         Number of detectors\n");
+  fprintf(stdout,"     nview        Number of rotated views\n");
+  fprintf(stdout,"     --phantom    Phantom to use for projection\n");
+  fprintf(stdout,"        herman    Herman head phantom\n");
+  fprintf(stdout,"        rowland   Rowland head phantom\n");
+  fprintf(stdout,"        browland  Bordered Rowland head phantom\n");
+  fprintf(stdout,"        unitpulse Unit pulse phantom\n");
+  fprintf(stdout,"     --phmfile    Get Phantom from phantom file\n");
+  fprintf(stdout,"     --desc       Description of raysum\n");
+  fprintf(stdout,"     --nray       Number of rays per detector (default = 1)\n");
+  fprintf(stdout,"     --rotangle   Degrees to rotate view through, multiple of PI (default = 1)\n");
+  fprintf(stdout,"     --trace      Trace level to use\n");
+  fprintf(stdout,"        none      No tracing (default)\n");
+  fprintf(stdout,"        text      Trace text level\n");
+  fprintf(stdout,"        phm       Trace phantom image\n");
+  fprintf(stdout,"        rays      Trace rays\n");
+  fprintf(stdout,"        plot      Trace plot\n");
+  fprintf(stdout,"        clipping  Trace clipping\n");
+  fprintf(stdout,"     --verbose    Verbose mode\n");
+  fprintf(stdout,"     --debug      Debug mode\n");
+  fprintf(stdout,"     --version    Print version\n");
+  fprintf(stdout,"     --help       Print this help message\n");
+#ifdef MPI_CT
+  }
+  MPI_Abort(mpi_ct.comm, 1);
+#endif
+}
+
+#ifdef MPI_CT
+void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug);
+#endif
+
+int 
+phm2rs_main (const int argc, char *const argv[])
+{
+  DETECTOR *det;
+  PHANTOM *phm = NULL;
+  RAYSUM *rs_global = NULL;
+  char str[256], *opt_outfile = NULL, opt_desc[MAXREMARK+1];
+  char opt_phmfilename[256];
+  char *endptr, *endstr;
+  int opt_ndet, opt_nview;
+  int opt_nray = 1;
+  int opt_trace = 0, opt_phmnum = -1;
+  int opt_verbose = 0;
+  int opt_debug = 0;
+  double opt_rotangle = 1;
+  double time_start = 0, time_end = 0;
+
+#ifndef MPI_CT
+  time_start = td_current_sec();
+#else
+  RAYSUM *rs_local;
+  int mpi_argc = argc;
+  char **mpi_argv = (char **) argv;
+  double mpi_t1 = 0, mpi_t2 = 0, mpi_t, mpi_t_g;
+
+  MPI_Init(&mpi_argc, &mpi_argv);
+  MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
+  MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
+  MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
+
+  if (mpi_ct.nproc > MPI_MAX_PROCESS) {
+    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)", mpi_ct.nproc, MPI_MAX_PROCESS);
+    exit(1);
+  }
+  time_start = MPI_Wtime();
+#endif
+  
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) {
+#endif
+    strcpy(opt_desc, "");
+    strcpy(opt_phmfilename, "");
+    while (1) {
+      int c = getopt_long(argc, argv, "", phm2rs_options, NULL);
+      char *endptr = NULL;
+      char *endstr;
+      
+      if (c == -1)
+       break;
+      
+      switch (c) {
+      case O_PHANTOM:
+       if ((opt_phmnum = opt_set_phantom (optarg)) < 0) {
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       phm = phm_create (opt_phmnum);
+       break;
+      case O_PHMFILE:
+#ifdef MPI_CT
+       if (mpi_ct.my_rank == 0)
+         fprintf(stderr, "Can not read phantom from file in MPI mode\n");
+       return (1);
+#endif
+       strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
+       phm = phm_create_from_file(opt_phmfilename);
+       break;
+      case O_VERBOSE:
+       opt_verbose = 1;
+       break;
+      case O_DEBUG:
+       opt_debug = 1;
+       break;
+       break;
+      case O_TRACE:
+       if ((opt_trace = opt_set_trace(optarg)) < 0) {
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_DESC:
+       strncpy(opt_desc, optarg, sizeof(opt_desc));
+       break;
+      case O_ROTANGLE:
+       opt_rotangle = strtod(optarg, &endptr);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --rotangle to %s\n", optarg);
+         phm2rs_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_NRAY:
+       opt_nray = strtol(optarg, &endptr, 10);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --nray to %s\n", optarg);
+         phm2rs_usage(argv[0]);
+         return (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 '?':
+       phm2rs_usage(argv[0]);
+       return (0);
+      default:
+       phm2rs_usage(argv[0]);
+       return (1);
+      }
+    }
+  
+    if (phm == NULL) {
+      fprintf(stderr, "No phantom defined\n");
+      phm2rs_usage(argv[0]);
+      return (1);
+    }
+    if (optind + 3 != argc) {
+      phm2rs_usage(argv[0]);
+      return (1);
+    }
+
+    opt_outfile = argv[optind];
+    opt_ndet = strtol(argv[optind+1], &endptr, 10);
+    endstr = argv[optind+1] + strlen(argv[optind+1]);
+    if (endptr != endstr) {
+      fprintf(stderr,"Error setting --ndet to %s\n", argv[optind+1]);
+      phm2rs_usage(argv[0]);
+      return (1);
+    }
+    opt_nview = strtol(argv[optind+2], &endptr, 10);
+    endstr = argv[optind+2] + strlen(argv[optind+2]);
+    if (endptr != endstr) {
+      fprintf(stderr,"Error setting --nview to %s\n", argv[optind+2]);
+      phm2rs_usage(argv[0]);
+      return (1);
+    }
+
+    snprintf(str, sizeof(str), 
+            "Raysum_Collect: NDet=%d, Nview=%d, NRay=%d, RotAngle=%.2f, ", 
+            opt_ndet, opt_nview, opt_nray, opt_rotangle);
+    if (opt_phmfilename[0]) {
+      strncat(str, "Phantom=", sizeof(str));
+      strncat(str, opt_phmfilename, sizeof(str));
+    } else if (opt_phmnum != -1) {
+      strncat(str, "Phantom=", sizeof(str));
+      strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
+    }
+    if (opt_desc[0]) {
+      strncat(str, ": ", sizeof(str));
+      strncat(str, opt_desc, sizeof(str));
+    }
+    strncpy(opt_desc, str, sizeof(opt_desc));
+#ifdef MPI_CT
+  }
+#endif
+
+#ifdef MPI_CT
+  MPI_Barrier(mpi_ct.comm);
+  MPI_Bcast(&opt_rotangle, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_nview, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_ndet, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_nray, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
+
+  if (mpi_ct.my_rank > 0 && opt_phmnum >= 0)
+    phm = phm_create (opt_phmnum);
+#endif
+
+  opt_rotangle *= PI;
+  det = detector_create (phm, DETECTOR_PARALLEL, opt_ndet, opt_nview, opt_nray, opt_rotangle);
+
+#ifdef MPI_CT
+  mpi_ct_calc_work_units(opt_nview);
+  if (mpi_ct.my_rank == 0) {
+    rs_global = raysum_create_from_det (opt_outfile, det);
+    raysum_alloc_views(rs_global);
+  }
+  
+  rs_local = raysum_create_from_det (NULL, det);
+  rs_local->nview = mpi_ct.local_work_units[mpi_ct.my_rank];
+  if (opt_debug)
+    printf("rs_local->nview = %d (process %d)\n", rs_local->nview, mpi_ct.my_rank);
+
+  if (opt_verbose)
+    mpi_t1 = MPI_Wtime();
+  raysum_collect (rs_local, det, phm, mpi_ct.start_work_unit[mpi_ct.my_rank], opt_trace, FALSE);
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to collect rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
+  }
+
+  if (opt_verbose)
+    mpi_t1 = MPI_Wtime();
+  mpi_gather_rs(rs_global, rs_local, opt_debug);
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to gather rs = %f secs, Max = %f secs\n", mpi_t, mpi_t_g);
+  }
+#else
+  rs_global = raysum_create_from_det (opt_outfile, det);
+  raysum_collect (rs_global, det, phm, 0, opt_trace, FALSE);
+#endif
+  
+#ifndef MPI_CT
+  time_end = td_current_sec();
+#else
+  time_end = MPI_Wtime();
+  if (mpi_ct.my_rank == 0) 
+#endif
+    {
+      rs_global->calctime = time_end - time_start;
+      strncpy (rs_global->remark, opt_desc, sizeof(rs_global->remark));
+      if (opt_verbose) {
+       fprintf(stdout, "Remark: %s\n", rs_global->remark);
+       fprintf(stdout, "Time active = %.2f secs\n", rs_global->calctime);
+      }
+    }
+
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) 
+#endif
+    {
+      raysum_write (rs_global);
+      raysum_close (rs_global);
+    }
+
+  phm_free (phm);
+  detector_free (det);
+
+  return (0);
+}
+
+
+/* FUNCTION
+ *    mpi_gather_rs
+ *
+ * SYNOPSIS
+ *    Gather's raysums from all processes in rs_local in rs_global in process 0
+ */
+
+#ifdef MPI_CT
+void mpi_gather_rs (RAYSUM *rs_global, RAYSUM *rs_local, const int opt_debug)
+{
+  int iproc;
+  int end_work_unit;
+  int iw;
+
+  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
+  for (iw = 0; iw <= end_work_unit; iw++) {
+    MPI_Send(&rs_local->view[iw]->view_angle, 1, MPI_DOUBLE, 0, 0, mpi_ct.comm);
+    MPI_Send(&rs_local->view[iw]->ndet, 1, MPI_INT, 0, 0, mpi_ct.comm);
+    MPI_Send(rs_local->view[iw]->detval, rs_local->ndet, MPI_FLOAT, 0, 0, mpi_ct.comm);
+  }
+
+  if (mpi_ct.my_rank == 0) {
+    for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
+      end_work_unit = mpi_ct.start_work_unit[iproc] 
+       + mpi_ct.local_work_units[iproc] 
+       - 1;
+
+      if (opt_debug)
+       fprintf(stdout, "Recv rs data from process %d\n", iproc);
+
+      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
+       MPI_Status status;
+
+       MPI_Recv(&rs_global->view[iw]->view_angle, 1, MPI_DOUBLE, iproc, 0, mpi_ct.comm, &status);
+       MPI_Recv(&rs_global->view[iw]->ndet, 1, MPI_INT, iproc, 0, mpi_ct.comm, &status);
+       MPI_Recv(rs_global->view[iw]->detval, rs_global->ndet, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
+      }
+    }
+  }
+
+}
+#endif
+
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (phm2rs_main(argc, argv));
+}
+#endif
+
diff --git a/src/phm2sdf.cpp b/src/phm2sdf.cpp
new file mode 100644 (file)
index 0000000..4211ae7
--- /dev/null
@@ -0,0 +1,496 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: phm2sdf.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: phm2sdf.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.10  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.9  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.8  2000/05/11 01:06:30  kevin
+**  Changed sprintf to snprintf
+**
+**  Revision 1.7  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.6  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  Revision 1.5  2000/04/30 19:17:54  kevin
+**  *** empty log message ***
+**
+**  Revision 1.4  2000/04/30 04:06:13  kevin
+**  Update Raysum i/o routines
+**  Fix MPI bug in ctrec (scatter_raysum) that referenced rs_global
+**
+**  Revision 1.3  2000/04/28 18:19:01  kevin
+**  removed unused files
+**
+**  Revision 1.2  2000/04/28 13:50:45  kevin
+**  Removed Makefile Makefile.in that are automatically generated by autoconf
+**
+**  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
+ *   phm2sdf.c                 Generate a SDF image from a phantom
+ *
+ * HISTORY
+ *   1984 - Final DOS verion
+ *   1999 - First UNIX version
+ */
+
+#include "ct.h"
+
+enum { O_PHANTOM, O_DESC, O_NSAMPLE, O_FILTER, O_TRACE, O_VERBOSE, O_HELP, 
+       O_PHMFILE, O_FILTER_DOMAIN, O_FILTER_BW, O_FILTER_PARAM, O_DEBUG, O_VERSION };
+
+static struct option my_options[] = 
+{
+  {"phantom", 1, 0, O_PHANTOM},
+  {"phmfile", 1, 0, O_PHMFILE},
+  {"desc", 1, 0, O_DESC},
+  {"nsample", 1, 0, O_NSAMPLE},
+  {"filter", 1, 0, O_FILTER},
+  {"filter-domain", 1, 0, O_FILTER_DOMAIN},
+  {"filter-bw", 1, 0, O_FILTER_BW},
+  {"filter-param", 1, 0, O_FILTER_PARAM},
+  {"trace", 1, 0, O_TRACE},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"debug", 0, 0, O_DEBUG},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+phm2sdf_usage (const char *program)
+{
+  fprintf(stdout,"phm2sdf_usage: %s outfile nx ny [--phantom phantom-name] [--phmfile filename] [--filter filter-name] [OPTIONS]\n",kbasename(program)); 
+  fprintf(stdout,"Generate phantom image from a predefined --phantom or a --phmfile\n");
+  fprintf(stdout,"\n");
+  fprintf(stdout,"     outfile         Name of output file for image\n");
+  fprintf(stdout,"     nx              Number of pixels X-axis\n");
+  fprintf(stdout,"     ny              Number of pixels Y-axis\n");
+  fprintf(stdout,"     --phantom       Phantom to use for projection\n");
+  fprintf(stdout,"        herman       Herman head phantom\n");
+  fprintf(stdout,"        rowland      Rowland head phantom\n");
+  fprintf(stdout,"        browland     Bordered Rowland head phantom\n");
+  fprintf(stdout,"        unitpulse    Unit pulse phantom\n");
+  fprintf(stdout,"     --phmfile       Generate Phantom from phantom file\n");
+  fprintf(stdout,"     --filter        Generate Phantom from a filter function\n");
+  fprintf(stdout,"       abs_bandlimit Abs * Bandlimiting\n");
+  fprintf(stdout,"       abs_sinc      Abs * Sinc\n");
+  fprintf(stdout,"       abs_cos       Abs * Cosine\n");
+  fprintf(stdout,"       abs_hamming   Abs * Hamming\n");
+  fprintf(stdout,"       shepp         Shepp-Logan\n");
+  fprintf(stdout,"       bandlimit     Bandlimiting\n");
+  fprintf(stdout,"       sinc          Sinc\n");
+  fprintf(stdout,"       cos           Cosine\n");
+  fprintf(stdout,"       triangle      Triangle\n");
+  fprintf(stdout,"       hamming       Hamming\n");
+  fprintf(stdout,"     --filter-param  Alpha level for Hamming filter\n");
+  fprintf(stdout,"     --filter-domain Set domain of filter\n");
+  fprintf(stdout,"         spatial     Spatial domain (default)\n");
+  fprintf(stdout,"         freq        Frequency domain\n");
+  fprintf(stdout,"     --filter-bw     Filter bandwidth (default = 1)\n");
+  fprintf(stdout,"     --desc          Description of raysum\n");
+  fprintf(stdout,"     --nsample       Number of samples per axis per pixel (default = 1)\n");
+  fprintf(stdout,"     --trace         Trace level to use\n");
+  fprintf(stdout,"        none         No tracing (default)\n");
+  fprintf(stdout,"        text         Trace text level\n");
+  fprintf(stdout,"        phm          Trace phantom\n");
+  fprintf(stdout,"        rays         Trace rays\n");
+  fprintf(stdout,"        plot         Trace plot\n");
+  fprintf(stdout,"        clipping     Trace clipping\n");
+  fprintf(stdout,"     --debug         Debug mode\n");
+  fprintf(stdout,"     --verbose       Verbose mode\n");
+  fprintf(stdout,"     --version       Print version\n");
+  fprintf(stdout,"     --help          Print this help message\n");
+}
+
+#ifdef MPI_CT
+void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug);
+#endif
+
+int 
+phm2sdf_main (const int argc, char *const argv[])
+{
+  IMAGE *im_global = NULL;
+  PHANTOM *phm = NULL;
+  int opt_nx = 0, opt_ny = 0;
+  int opt_nsample = 1;
+  int opt_phmnum = -1;
+  FilterType opt_filter = static_cast<FilterType>(-1);
+  DomainType opt_filter_domain = D_SPATIAL;
+  char *opt_outfile = NULL;
+  int opt_debug = 0;
+  char str[256];
+  char opt_desc[256], opt_phmfilename[256];
+  char *endstr, *endptr;
+  double opt_filter_param = 1;
+  double opt_filter_bw = 1.;
+  int opt_trace = TRACE_NONE;
+  int opt_verbose = 0;
+  double time_start=0, time_end=0;
+#ifdef MPI_CT
+  IMAGE *im_local = NULL;
+  int mpi_argc = argc;
+  char **mpi_argv = (char **) argv;
+  double mpi_t1, mpi_t2, mpi_t, mpi_t_g;
+
+  MPI_Init(&mpi_argc, &mpi_argv);
+  MPI_Comm_dup (MPI_COMM_WORLD, &mpi_ct.comm);
+  MPI_Comm_size(mpi_ct.comm, &mpi_ct.nproc);
+  MPI_Comm_rank(mpi_ct.comm, &mpi_ct.my_rank);
+
+  if (mpi_ct.nproc > MPI_MAX_PROCESS) {
+    sys_error(ERR_FATAL, "Number of mpi processes (%d) exceeds max processes (%d)",
+             mpi_ct.nproc, MPI_MAX_PROCESS);
+  }
+#endif
+
+#ifdef MPI_CT
+  time_start = MPI_Wtime();
+#else
+  time_start = td_current_sec();
+#endif
+  
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) {
+#endif
+
+    strcpy(opt_desc, "");
+    strcpy(opt_phmfilename, "");
+    while (1) {
+      int c = getopt_long(argc, argv, "", my_options, NULL);
+      char *endptr = NULL;
+      char *endstr;
+      
+      if (c == -1)
+       break;
+      
+      switch (c) {
+      case O_PHANTOM:
+       if ((opt_phmnum = opt_set_phantom(optarg)) < 0) {
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_PHMFILE:
+       strncpy(opt_phmfilename, optarg, sizeof(opt_phmfilename));
+       phm = phm_create_from_file(opt_phmfilename);
+#ifdef MPI_CT
+       if (mpi_ct.my_rank == 0) 
+         fprintf(stderr, "Can't use phantom from file in MPI mode\n");
+       return (1);
+#endif
+       break;
+      case O_VERBOSE:
+       opt_verbose = 1;
+       break;
+      case O_DEBUG:
+       opt_debug = 1;
+       break;
+      case O_TRACE:
+       if ((opt_trace = opt_set_trace(optarg)) < 0) {
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_FILTER:
+       if ((opt_filter = opt_set_filter(optarg)) < 0) {
+         phm2sdf_usage (argv[0]);
+         return (1);
+       }
+       break;
+      case O_FILTER_DOMAIN:
+       if ((opt_filter_domain = opt_set_filter_domain(optarg)) < 0) {
+         phm2sdf_usage (argv[0]);
+         return (1);
+       }
+       break;
+      case O_DESC:
+       strncpy(opt_desc, optarg, sizeof(opt_desc));
+       break;
+      case O_FILTER_BW:
+       opt_filter_bw = strtod(optarg, &endptr);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --filter-bw to %s\n", optarg);
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_FILTER_PARAM:
+       opt_filter_param = strtod(optarg, &endptr);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --filter-param to %s\n", optarg);
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+      case O_NSAMPLE:
+       opt_nsample = strtol(optarg, &endptr, 10);
+       endstr = optarg + strlen(optarg);
+       if (endptr != endstr) {
+         fprintf(stderr,"Error setting --nsample to %s\n", optarg);
+         phm2sdf_usage(argv[0]);
+         return (1);
+       }
+       break;
+        case O_VERSION:
+#ifdef VERSION
+         fprintf(stdout, "Version %s\n", VERSION);
+#else
+         fprintf(stderr, "Unknown version number");
+#endif
+      case O_HELP:
+      case '?':
+       phm2sdf_usage(argv[0]);
+       return (0);
+      default:
+       phm2sdf_usage(argv[0]);
+       return (1);
+      }
+    }
+    
+    if (phm == NULL && opt_phmnum == -1 && opt_filter == -1) {
+      fprintf(stderr, "No phantom defined\n");
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+
+    if (optind + 3 != argc) {
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+    opt_outfile = argv[optind];
+    opt_nx = strtol(argv[optind+1], &endptr, 10);
+    endstr = argv[optind+1] + strlen(argv[optind+1]);
+    if (endptr != endstr) {
+      fprintf(stderr,"Error setting nx to %s\n", argv[optind+1]);
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+    opt_ny = strtol(argv[optind+2], &endptr, 10);
+    endstr = argv[optind+2] + strlen(argv[optind+2]);
+    if (endptr != endstr) {
+      fprintf(stderr,"Error setting ny to %s\n", argv[optind+2]);
+      phm2sdf_usage(argv[0]);
+      return (1);
+    }
+    
+    snprintf(str, sizeof(str), "nx=%d, ny=%d, nsample=%d, ", opt_nx, opt_ny, opt_nsample);
+    if (opt_phmfilename[0]) {
+      strncat(str, "phantom=", sizeof(str));
+      strncat(str, opt_phmfilename, sizeof(str));
+    }
+    else if (opt_phmnum != -1) {
+      strncat(str, "phantom=", sizeof(str));
+      strncat(str, name_of_phantom(opt_phmnum), sizeof(str));
+    }
+    else if (opt_filter != -1) {
+      strncat(str, "filter=", sizeof(str));
+      strncat(str, name_of_filter(opt_filter), sizeof(str));
+      strncat(str, " - ", sizeof(str));
+      strncat(str, name_of_filter_domain(opt_filter_domain), sizeof(str));
+    }
+    if (opt_desc[0]) {
+      strncat(str, ": ", sizeof(str));
+      strncat(str, opt_desc, sizeof(str));
+    }
+    strncpy(opt_desc, str, sizeof(opt_desc));
+    
+    if (opt_verbose)
+      printf("Compile Phantom to Image\n\n");
+#ifdef MPI_CT
+  }
+#endif
+
+#ifdef MPI_CT
+  mpi_t1 = MPI_Wtime();
+  MPI_Bcast(&opt_verbose, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_debug, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_trace, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_nx, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_ny, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_nsample, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_phmnum, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_domain, 1, MPI_INT, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_param, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+  MPI_Bcast(&opt_filter_bw, 1, MPI_DOUBLE, 0, mpi_ct.comm);
+
+  if (opt_verbose) {
+    mpi_t2 = MPI_Wtime();
+    mpi_t = mpi_t2 - mpi_t1;
+    MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+    if (mpi_ct.my_rank == 0)
+      printf("Time to Bcast vars = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
+  }
+
+  mpi_ct_calc_work_units(opt_nx);
+
+  if (mpi_ct.my_rank == 0)
+    im_global = image_create (opt_outfile, opt_nx, opt_ny);
+
+  im_local = image_create(NULL, opt_nx, opt_ny);
+#else
+  im_global = image_create (opt_outfile, opt_nx, opt_ny);
+#endif
+
+  if (opt_phmnum >= 0)
+      phm = phm_create (opt_phmnum);
+
+#ifdef MPI_CT
+  else {
+    if (mpi_ct.my_rank == 0)
+      fprintf(stderr, "phmnum < 0\n");
+    exit(1);
+  }
+#endif
+
+#ifdef MPI_CT
+  if (phm->type == P_UNIT_PULSE) {
+    if (mpi_ct.my_rank == 0) {
+      im_global->v[opt_nx/2][opt_ny/2] = 1.;
+      im_global->calctime = 0;
+    }
+  } else if (opt_filter != -1) {
+    if (mpi_ct.my_rank == 0) {
+      image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
+      im_global->calctime = 0;
+    }
+  } else {
+    if (opt_verbose)
+      mpi_t1 = MPI_Wtime();
+    phm_to_image (phm, im_local, mpi_ct.start_work_unit[mpi_ct.my_rank],
+                 mpi_ct.local_work_units[mpi_ct.my_rank], opt_nsample, opt_trace);
+    if (opt_verbose) {
+      mpi_t2 = MPI_Wtime();
+      mpi_t = mpi_t2 - mpi_t1;
+      MPI_Reduce(&mpi_t, &mpi_t_g, 1, MPI_DOUBLE, MPI_MAX, 0, mpi_ct.comm);
+      if (mpi_ct.my_rank == 0)
+       printf("Time to compile phm = %f secs, Max time = %f\n", mpi_t, mpi_t_g);
+    }
+    mpi_gather_image(im_global, im_local, opt_debug);
+  }
+#else
+  if (phm->type == P_UNIT_PULSE) {
+    im_global->v[opt_nx/2][opt_ny/2] = 1.;
+    im_global->calctime = 0;
+  } else if (opt_filter != -1) {
+    image_filter_response (im_global, opt_filter_domain, opt_filter_bw, opt_filter, opt_filter_param, opt_trace);
+    im_global->calctime = 0;
+  } else {
+#if HAVE_SGP
+      if (opt_trace >= TRACE_PHM)
+       phm_show(phm);
+#endif
+      phm_to_image (phm, im_global, 0, opt_nx, opt_nsample, opt_trace);
+  }
+#endif
+
+#ifdef MPI_CT
+  time_end = MPI_Wtime();
+#else
+  time_end = td_current_sec();
+#endif
+
+#ifdef MPI_CT
+  if (mpi_ct.my_rank == 0) 
+#endif
+  {
+    im_global->calctime = time_end - time_start;
+    strncpy (im_global->remark, opt_desc, sizeof(im_global->remark));
+    image_save (im_global);
+    if (opt_verbose)
+      fprintf (stdout, "Time to compile image = %.2f sec\n\n", im_global->calctime);
+  }
+
+  if (opt_trace >= TRACE_PHM)
+    {
+      double dmin, dmax;
+      int nscale;
+
+      printf ("Enter display size scale (nominal = 1): ");
+      scanf ("%d", &nscale);
+      printf ("Enter minimum and maximum densities (min, max): ");
+      scanf ("%lf %lf", &dmin, &dmax);
+      image_display_scale (im_global, nscale, dmin, dmax);
+    }
+
+  phm_free (phm);
+
+  return (0);
+}
+
+
+
+#ifdef MPI_CT
+void mpi_gather_image (IMAGE *im_global, IMAGE *im_local, const int opt_debug)
+{
+  int iproc;
+  int end_work_unit;
+  int iw;
+
+  end_work_unit = mpi_ct.local_work_units[mpi_ct.my_rank] - 1;
+  for (iw = 0; iw <= end_work_unit; iw++) {
+    MPI_Send(im_local->v[iw], im_local->ny, MPI_FLOAT, 0, 0, mpi_ct.comm);
+  }
+
+  if (mpi_ct.my_rank == 0) {
+    for (iproc = 0; iproc < mpi_ct.nproc; iproc++) {
+      end_work_unit = mpi_ct.start_work_unit[iproc] 
+       + mpi_ct.local_work_units[iproc] 
+       - 1;
+
+      if (opt_debug) {
+       fprintf(stdout, "Recv rs data from process %d (%d-%d)\n", iproc,
+               mpi_ct.start_work_unit[iproc], end_work_unit);
+       fflush(stdout);
+      }
+
+
+      for (iw = mpi_ct.start_work_unit[iproc]; iw <= end_work_unit; iw++) {
+       MPI_Status status;
+
+       MPI_Recv(im_global->v[iw], im_global->ny, MPI_FLOAT, iproc, 0, mpi_ct.comm, &status);
+      }
+    }
+  }
+
+}
+#endif
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (phm2sdf_main(argc, argv));
+}
+#endif
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
diff --git a/src/rs2sdf.cpp b/src/rs2sdf.cpp
new file mode 100644 (file)
index 0000000..4ce0622
--- /dev/null
@@ -0,0 +1,157 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: rs2sdf.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: rs2sdf.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
+ *   rs2sdf.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 
+rs2sdf_usage (const char *program)
+{
+  fprintf(stdout, "rs2sdf_usage: %s in-rs-file out-sdf-file [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "This program converts a raysum file to a SDF2D 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 
+rs2sdf_main (const int argc, char *const argv[])
+{
+  IMAGE *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 '?':
+         rs2sdf_usage(argv[0]);
+         return (0);
+       default:
+         rs2sdf_usage(argv[0]);
+         return (1);
+       }
+    }
+  
+  if (argc - optind != 2) {
+    rs2sdf_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 = image_create (im_name, rs->ndet, rs->nview);
+  
+  sdf_add_label (LT_HISTORY, rs->remark, rs->calctime, im->dfp_2d->dfp);
+  sdf_add_empty_label (im->dfp_2d->dfp);
+
+  detarray = detarray_alloc (rs->ndet);
+  for (iy = 0; iy < rs->nview; iy++)
+    {
+      detarray_read (rs, detarray, iy);
+      for (ix = 0; ix < rs->ndet; ix++)
+       {
+         im->v[ix][iy] = detarray->detval[ix];
+       }
+    }
+  detarray_free (detarray);
+  
+  raysum_close (rs);
+  image_save (im);
+  
+  return(0);
+}
+
+#ifndef NO_MAIN
+int 
+main (const int argc, char *const argv[])
+{
+  return (rs2sdf_main(argc, argv));
+}
+#endif
diff --git a/src/sdf-1.cpp b/src/sdf-1.cpp
new file mode 100644 (file)
index 0000000..545bb72
--- /dev/null
@@ -0,0 +1,194 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: sdf-1.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: sdf-1.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.6  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.5  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.4  2000/05/09 14:52:27  kevin
+**  added sqr and sqrt functions
+**
+**  Revision 1.3  2000/05/08 20:02:32  kevin
+**  ANSI C changes
+**
+**  Revision 1.2  2000/05/03 08:49:50  kevin
+**  Code cleanup
+**
+**  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
+ *   sdf-1.c             Filter a single SDF file
+ */
+
+#include "ct.h"
+
+enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
+
+static struct option my_options[] =
+{
+  {"invert", 0, 0, O_INVERT},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"log", 0, 0, O_LOG},
+  {"exp", 0, 0, O_EXP},
+  {"sqr", 0, 0, O_SQR},
+  {"sqrt", 0, 0, O_SQRT},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+sdf1_usage (const char *program)
+{
+  fprintf(stdout, "sdf1_usage: %s infile outfile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "Generate a SDF2D file from a SDF2D file\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "     --invert   Invert image\n");
+  fprintf(stdout, "     --log      Natural logrithm of image\n");
+  fprintf(stdout, "     --exp      Natural exponential of image\n");
+  fprintf(stdout, "     --sqr      Square of image\n");
+  fprintf(stdout, "     --sqrt     Square root of image\n");
+  fprintf(stdout, "     --verbose  Verbose modem\n");
+  fprintf(stdout, "     --version  Print version\n");
+  fprintf(stdout, "     --help     Print this help message\n");
+}
+
+int 
+sdf1_main (int argc, char *const argv[])
+{
+  IMAGE *im_in;
+  IMAGE *im_out;
+  char *in_file;
+  char *out_file;
+  int opt_verbose = 0;
+  int opt_invert = 0;
+  int opt_log = 0;
+  int opt_exp = 0;
+  int opt_sqr = 0;
+  int opt_sqrt = 0;
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_INVERT:
+         opt_invert = 1;
+         break;
+       case O_LOG:
+         opt_log = 1;
+         break;
+       case O_SQR:
+         opt_sqr = 1;
+         break;
+       case O_SQRT:
+         opt_sqrt = 1;
+         break;
+       case O_EXP:
+         opt_exp = 1;
+         break;
+       case O_VERBOSE:
+         opt_verbose = 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 '?':
+         sdf1_usage(argv[0]);
+         return (0);
+       default:
+         sdf1_usage(argv[0]);
+         return (1);
+       }
+    }
+
+  if (optind + 2 != argc)
+    {
+      sdf1_usage(argv[0]);
+      return (1);
+    }
+  
+  in_file = argv[optind];
+  out_file = argv[optind + 1];
+
+
+  if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
+    int ix, iy;
+
+    im_in = image_load (in_file);
+    im_out = image_create (out_file, im_in->nx, im_in->ny);
+
+    if (opt_invert) {
+      for (ix = 0; ix < im_in->nx; ix++)
+        for (iy = 0; iy < im_in->ny; iy++)
+          im_out->v[ix][iy] = - im_in->v[ix][iy];
+    }
+    if (opt_log) {
+      for (ix = 0; ix < im_in->nx; ix++)
+        for (iy = 0; iy < im_in->ny; iy++)
+          im_out->v[ix][iy] = log (im_in->v[ix][iy]);
+    }
+    if (opt_exp) {
+      for (ix = 0; ix < im_in->nx; ix++)
+        for (iy = 0; iy < im_in->ny; iy++)
+          im_out->v[ix][iy] = exp (im_in->v[ix][iy]);
+    }
+    if (opt_sqr) {
+      for (ix = 0; ix < im_in->nx; ix++)
+        for (iy = 0; iy < im_in->ny; iy++)
+          im_out->v[ix][iy] = im_in->v[ix][iy] * im_in->v[ix][iy];
+    }
+    if (opt_sqrt) {
+      for (ix = 0; ix < im_in->nx; ix++)
+        for (iy = 0; iy < im_in->ny; iy++)
+          im_out->v[ix][iy] = sqrt (im_in->v[ix][iy]);
+    }
+
+    image_save(im_out);
+  }
+
+  return (0);
+}
+
+#ifndef NO_MAIN
+int 
+main (int argc, char *const argv[])
+{
+  return (sdf1_main(argc, argv));
+}
+#endif
+
diff --git a/src/sdf-2.cpp b/src/sdf-2.cpp
new file mode 100644 (file)
index 0000000..cf1dd74
--- /dev/null
@@ -0,0 +1,225 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: sdf-2.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: sdf-2.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.5  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  Revision 1.4  2000/05/16 04:33:59  kevin
+**  Improved option processing
+**
+**  Revision 1.3  2000/05/11 01:06:30  kevin
+**  Changed sprintf to snprintf
+**
+**  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
+ *   sdf-2.c         Generate a SDF file from two input SDF files
+ */
+
+#include "ct.h"
+
+enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
+
+static struct option my_options[] =
+{
+  {"add", 0, 0, O_ADD},
+  {"sub", 0, 0, O_SUB},
+  {"mul", 0, 0, O_MUL},
+  {"comp", 0, 0, O_COMP},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+sdf2_usage (const char *program)
+{
+  fprintf(stdout, "sdf2_usage: %s infile1 infile2 outfile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "Generate an SDF2D file from two input SDF2D files\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "     infile1    Name of first input SDF file\n");
+  fprintf(stdout, "     infile2    Name of second input SDF file\n");
+  fprintf(stdout, "     outfile    Name of output SDF file\n");
+  fprintf(stdout, "     --add      Add images\n");
+  fprintf(stdout, "     --sub      Subtract image 2 from image 1\n");
+  fprintf(stdout, "     --mul      Multiply images\n");
+  fprintf(stdout, "     --comp     Compare images\n");
+  fprintf(stdout, "     --verbose  Verbose modem\n");
+  fprintf(stdout, "     --version  Print version\n");
+  fprintf(stdout, "     --help     Print this help message\n");
+}
+
+int 
+sdf2_main (int argc, char *const argv[])
+{
+  IMAGE *im_in1;
+  IMAGE *im_in2;
+  IMAGE *im_out;
+  char *in_file1;
+  char *in_file2;
+  char *out_file;
+  int opt_verbose = 0;
+  int opt_add = 0;
+  int opt_sub = 0;
+  int opt_mul = 0;
+  int opt_comp = 0;
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_ADD:
+         opt_add = 1;
+         break;
+       case O_SUB :
+         opt_sub = 1;
+         break;
+       case O_MUL:
+         opt_mul = 1;
+         break;
+       case O_COMP:
+         opt_comp = 1;
+         break;
+       case O_VERBOSE:
+         opt_verbose = 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 '?':
+         sdf2_usage(argv[0]);
+         return (0);
+       default:
+         sdf2_usage(argv[0]);
+         return (1);
+       }
+    }
+
+  if (optind + 3 != argc)
+    {
+      sdf2_usage(argv[0]);
+      return (1);
+    }
+  
+  in_file1 = argv[optind];
+  in_file2 = argv[optind + 1];
+  out_file = argv[optind + 2];
+
+  im_in1 = image_load (in_file1);
+  im_in2 = image_load (in_file2);
+  if (im_in1 == NULL || im_in2 == NULL) {
+      fprintf(stderr, "Error reading an image");
+      return (1);
+  }
+
+  if (im_in1->nx != im_in2->nx || im_in1->ny != im_in2->ny) {
+    fprintf(stderr, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match\n",
+           im_in1->nx, im_in1->ny, im_in2->nx, im_in2->ny);
+    return(1);
+  }
+  if (im_in1->nx < 0 || im_in1->ny < 0) {
+      fprintf(stderr, "Error: Size of image < 0");
+      return(1);
+  }
+
+  im_out = image_create (out_file, im_in1->nx, im_in1->ny);
+
+  if (opt_add) {
+    int ix, iy;
+    for (ix = 0; ix < im_in1->nx; ix++)
+      {
+       IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
+       IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
+       IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
+       for (iy = 0; iy < im_in1->ny; iy++)
+         *out++ = *in1++ + *in2++;
+      }
+  } else if (opt_sub) {
+    int ix, iy;
+    for (ix = 0; ix < im_in1->nx; ix++)
+      {
+       IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
+       IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
+       IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
+       for (iy = 0; iy < im_in1->ny; iy++)
+         *out++ = *in1++ - *in2++;
+      }
+  } else if (opt_mul) {
+    int ix, iy;
+    for (ix = 0; ix < im_in1->nx; ix++)
+      {
+       IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
+       IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
+       IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
+       for (iy = 0; iy < im_in1->ny; iy++)
+         *out++ = *in1++ * *in2++;
+      }
+  } else if (opt_comp) {
+    int ix, iy;
+    double abs_error = 0.;
+    for (ix = 0; ix < im_in1->nx; ix++)
+      {
+       IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
+       IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
+       IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
+       for (iy = 0; iy < im_in1->ny; iy++)
+         {
+           double diff = *in1++ - *in2++;
+           *out++ = diff;
+           abs_error += fabs(diff);
+         }
+      }
+    abs_error /= (im_in1->nx * im_in1->ny);
+    fprintf(stdout, "Average Error = %f\n", abs_error);
+  }
+
+  image_save(im_out);
+
+  return (0);
+}
+
+#ifndef NO_MAIN
+int 
+main (int argc, char *const argv[])
+{
+  return (sdf2_main(argc, argv));
+}
+#endif
+
diff --git a/src/sdf2img.cpp b/src/sdf2img.cpp
new file mode 100644 (file)
index 0000000..c646e54
--- /dev/null
@@ -0,0 +1,736 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: sdf2img.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: sdf2img.cpp,v $
+**  Revision 1.1  2000/06/07 02:29:05  kevin
+**  Initial C++ versions
+**
+**  Revision 1.6  2000/06/03 07:57:51  kevin
+**  Fixed PNG 16-bit format
+**
+**  Revision 1.5  2000/06/03 06:29:57  kevin
+**  *** empty log message ***
+**
+**  Revision 1.4  2000/05/24 22:50:04  kevin
+**  Added support for new SGP library
+**
+**  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
+ *   sdf2img.c         Convert an SDF file to a viewable format image
+ */
+
+#include "ct.h"
+
+#if HAVE_PNG
+void sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax);
+#endif
+#if HAVE_GIF
+void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
+#endif
+void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
+void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
+
+enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS, 
+       O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
+
+static struct option my_options[] =
+{
+  {"scale", 1, 0, O_SCALE},
+  {"min", 1, 0, O_MIN},
+  {"max", 1, 0, O_MAX},
+  {"auto", 1, 0, O_AUTO},
+  {"center", 1, 0, O_CENTER},
+  {"format", 1, 0, O_FORMAT},
+  {"labels", 0, 0, O_LABELS},
+  {"stats", 0, 0, O_STATS},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"debug", 0, 0, O_DEBUG},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
+#define O_AUTO_FULL_STR  "full"
+#define O_AUTO_STD0_1_STR  "std0.1"
+#define O_AUTO_STD0_5_STR  "std0.5"
+#define O_AUTO_STD1_STR  "std1"
+#define O_AUTO_STD2_STR  "std2"
+#define O_AUTO_STD3_STR  "std3"
+
+enum { O_CENTER_MEAN, O_CENTER_MODE };
+#define O_CENTER_MEAN_STR  "mean"
+#define O_CENTER_MODE_STR  "mode"
+
+enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
+#define O_FORMAT_GIF_STR   "gif"
+#define O_FORMAT_PNG_STR   "png" 
+#define O_FORMAT_PNG16_STR   "png16" 
+#define O_FORMAT_PGM_STR   "pgm"
+#define O_FORMAT_PGMASC_STR "pgmasc"
+#define O_FORMAT_DISP_STR    "disp"
+
+void 
+sdf2img_usage (const char *program)
+{
+  fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "Convert SDF2D file to an image file\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "     sdfname    Name of input SDF file\n");
+  fprintf(stdout, "     outfile    Name of output file\n");
+  fprintf(stdout, "     --format   Output format\n");
+  fprintf(stdout, "         pgm    PGM (portable graymap) format (default)\n");
+  fprintf(stdout, "         pgmasc PGM (portable graymap) ASCII format\n");
+#ifdef HAVE_PNG
+  fprintf(stdout, "         png    PNG (8-bit) format\n");
+  fprintf(stdout, "         png16  PNG (16-bit) format\n");
+#endif
+#ifdef HAVE_GIF
+  fprintf(stdout, "         gif    GIF format\n");
+#endif
+  fprintf(stdout, "         disp   Display on screen\n");
+  fprintf(stdout, "     --center   Center of window\n");
+  fprintf(stdout, "         mode   Mode is center of window (default)\n");
+  fprintf(stdout, "         mean   Mean is center of window\n");
+  fprintf(stdout, "     --auto     Set auto window\n");
+  fprintf(stdout, "         full   Use full window (default)\n");
+  fprintf(stdout, "         std0.1 Use 0.1 standard deviation about center\n");
+  fprintf(stdout, "         std0.5 Use 0.5 standard deviation about center\n");
+  fprintf(stdout, "         std1   Use one standard deviation about center\n");
+  fprintf(stdout, "         std2   Use two standard deviations about center\n");
+  fprintf(stdout, "         std3   Use three standard deviations about center\n");
+  fprintf(stdout, "     --scale    Scaling factor for output size\n");
+  fprintf(stdout, "     --min      Set minimum intensity\n");
+  fprintf(stdout, "     --max      Set maximum intensity\n");
+  fprintf(stdout, "     --stats    Print image statistics\n");
+  fprintf(stdout, "     --labels   Print image labels\n");
+  fprintf(stdout, "     --debug    Set debug mode\n");
+  fprintf(stdout, "     --verbose  Set verbose mode\n");
+  fprintf(stdout, "     --version  Print version\n");
+  fprintf(stdout, "     --help     Print this help message\n");
+}
+
+
+int 
+sdf2img_main (int argc, char *const argv[])
+{
+  IMAGE *im;
+  double densmin = HUGE_VAL, densmax = -HUGE_VAL;
+  char *in_file, *out_file;
+  int opt_verbose = 0;
+  int opt_scale = 1;
+  int opt_auto = O_AUTO_FULL;
+  int opt_set_max = 0;
+  int opt_set_min = 0;
+  int opt_stats = 0;
+  int opt_debug = 0;
+  int opt_center = O_CENTER_MODE;
+  int opt_format = O_FORMAT_PGM;
+  int opt_labels = 0;
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      char *endptr = NULL;
+      char *endstr;
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_MIN:
+         opt_set_min = 1;
+         densmin = strtod(optarg, &endptr);
+         endstr = optarg + strlen(optarg);
+         if (endptr != endstr)
+           {
+             fprintf(stderr, "Error setting --min to %s\n", optarg);
+             sdf2img_usage(argv[0]);
+             return (1);
+           }
+         break;
+       case O_MAX:
+         opt_set_max = 1;
+         densmax = strtod(optarg, &endptr);
+         endstr = optarg + strlen(optarg);
+         if (endptr != endstr)
+           {
+             fprintf(stderr, "Error setting --max to %s\n", optarg);
+             sdf2img_usage(argv[0]);
+             return (1);
+           }
+         break;
+       case O_SCALE:
+         opt_scale = strtol(optarg, &endptr, 10);
+         endstr = optarg + strlen(optarg);
+         if (endptr != endstr)
+           {
+             fprintf(stderr,"Error setting --scale to %s\n", optarg);
+             sdf2img_usage(argv[0]);
+             return (1);
+           }
+         break;
+       case O_AUTO:
+         if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
+           opt_auto = O_AUTO_FULL;
+         else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
+           opt_auto = O_AUTO_STD1;
+         else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
+           opt_auto = O_AUTO_STD0_5;
+         else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
+           opt_auto = O_AUTO_STD0_1;
+         else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
+           opt_auto = O_AUTO_STD2;
+         else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
+           opt_auto = O_AUTO_STD3;
+         else
+           {
+             fprintf(stderr, "Invalid auto mode %s\n", optarg);
+             sdf2img_usage(argv[0]);
+             return (1);
+           }
+               break;
+       case O_CENTER:
+         if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
+           opt_center = O_CENTER_MEAN;
+         else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
+           opt_center = O_CENTER_MODE;
+         else
+           {
+             fprintf(stderr, "Invalid center mode %s\n", optarg);
+             sdf2img_usage(argv[0]);
+             return (1);
+           }
+               break;
+       case O_FORMAT:
+         if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
+           opt_format = O_FORMAT_PGM;
+         else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
+           opt_format = O_FORMAT_PGMASC;
+#if HAVE_PNG
+         else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
+           opt_format = O_FORMAT_PNG;
+         else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
+           opt_format = O_FORMAT_PNG16;
+#endif
+#if HAVE_GIF
+         else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
+           opt_format = O_FORMAT_GIF;
+#endif
+         else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
+           opt_format = O_FORMAT_DISP;
+         else {
+             fprintf(stderr, "Invalid format mode %s\n", optarg);
+             sdf2img_usage(argv[0]);
+             return (1);
+           }
+         break;
+       case O_LABELS:
+         opt_labels = 1;
+         break;
+       case O_VERBOSE:
+         opt_verbose = 1;
+         break;
+       case O_DEBUG:
+         opt_debug = 1;
+         break;
+       case O_STATS:
+         opt_stats = 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 '?':
+         sdf2img_usage(argv[0]);
+         return (0);
+       default:
+         sdf2img_usage(argv[0]);
+         return (1);
+       }
+    }
+
+  if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) 
+      || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
+    {
+       sdf2img_usage(argv[0]);
+       return (1);
+    }
+  
+  in_file = argv[optind];
+
+  if (opt_format != O_FORMAT_DISP)
+      out_file = argv[optind+1];
+  else out_file = NULL;
+
+  if ((im = image_load (in_file)) == NULL)
+    {
+      fprintf(stderr, "File %s does not exist\n", in_file);
+      return (1);
+    }
+
+  if (opt_labels) 
+    {
+      int nlabels = im->dfp_2d->dfp->num_label;
+      int i;
+
+      for (i = 0; i < nlabels; i++)
+       {
+         SDF_BLK blk;
+         if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
+           break;
+         if (blk.lab.label_type == LT_HISTORY) {
+           fprintf(stdout, "History: %s\n", blk.lab.label_str);
+           fprintf(stdout, "  calc time = %.2f secs\n", blk.lab.calc_time);
+           fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
+         } else if (blk.lab.label_type == LT_TITLE) {
+           fprintf(stdout, "Title: %s\n", blk.lab.label_str);
+           fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
+         } else if (blk.lab.label_type == LT_NOTE) {
+           fprintf(stdout, "Note: %s\n", blk.lab.label_str);
+           fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
+         }
+       }
+    }
+
+  if (opt_stats || (! (opt_set_max && opt_set_min)))
+    {
+      double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
+      double mode = 0, mean = 0, stddev = 0, window = 0;
+      double spread;
+      int hist[256];
+      int ibin, nbin = 256;
+      int max_bin, max_binindex; 
+      double maxbin;
+      int ix, iy;
+
+      maxbin = nbin - 1;
+      for (ix = 0; ix < im->nx; ix++)
+       {
+         for (iy = 0; iy < im->ny; iy++)
+           {
+             if (im->v[ix][iy] > maxfound)
+               maxfound = im->v[ix][iy];
+             if (im->v[ix][iy] < minfound)
+               minfound = im->v[ix][iy];
+             mean += im->v[ix][iy];
+           }
+       }
+      spread = maxfound - minfound;
+      if (spread == 0)
+       mode = minfound;
+      else
+       {
+         for (ibin = 0; ibin < nbin; ibin++)
+           hist[ibin] = 0;
+         for (ix = 0; ix < im->nx; ix++)
+           {
+             for (iy = 0; iy < im->ny; iy++)
+               {
+                 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
+                 hist[b]++;
+               }
+           }
+         max_binindex = 0;
+         max_bin = -1;
+         for (ibin = 0; ibin < nbin; ibin++)
+           {
+             if (hist[ibin] > max_bin)
+               {
+                 max_bin = hist[ibin];
+                 max_binindex = ibin;
+               }
+           }
+         mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
+       }
+
+      if (opt_auto == O_AUTO_FULL)
+       {
+         if (! opt_set_max)
+           densmax = maxfound;
+         if (! opt_set_min)
+           densmin = minfound;
+       }
+      if (opt_stats || opt_auto != O_AUTO_FULL)
+       {
+         mean /= im->nx * im->ny;
+         for (ix = 0; ix < im->nx; ix++)
+           {
+             for (iy = 0; iy < im->ny; iy++)
+               {
+                 double diff = (im->v[ix][iy] - mean);
+                 stddev += diff * diff;
+               }
+           }
+         stddev = sqrt(stddev / (im->nx * im->ny));
+         if (opt_auto == O_AUTO_FULL)
+           ;
+         else if (opt_auto == O_AUTO_STD1)
+           window = stddev;
+         else if (opt_auto == O_AUTO_STD0_1)
+           window = stddev * 0.1;
+         else if (opt_auto == O_AUTO_STD0_5)
+           window = stddev * 0.5;
+         else if (opt_auto == O_AUTO_STD2)
+           window = stddev * 2;
+         else if (opt_auto == O_AUTO_STD3)
+           window = stddev * 3;
+         else
+           {
+             fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
+             return (1);
+           }
+       }
+      if (opt_stats)
+       {
+         fprintf(stdout,"nx=%d\n", im->nx);
+         fprintf(stdout,"ny=%d\n", im->ny);
+         fprintf(stdout,"min=%f\n", minfound);
+         fprintf(stdout,"max=%f\n", maxfound);
+         fprintf(stdout,"mean=%f\n", mean);
+         fprintf(stdout,"mode=%f\n", mode);
+         fprintf(stdout,"stddev=%f\n", stddev);
+       }
+      if (opt_auto != O_AUTO_FULL)
+       {
+         double center;
+
+         if (opt_center == O_CENTER_MODE)
+           center = mode;
+         else if (opt_center == O_CENTER_MEAN)
+           center = mean;
+         else
+           {
+             fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
+             return (1);
+           }
+         if (! opt_set_max)
+           densmax = center + window;
+         if (! opt_set_min)
+           densmin = center - window;
+       }
+    }
+
+  if (opt_stats)
+    {
+      fprintf(stdout,"min_disp=%f\n", densmin);
+      fprintf(stdout,"max_disp=%f\n", densmax);
+    }
+  
+  if (opt_format == O_FORMAT_PGM)
+    sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
+  else if (opt_format == O_FORMAT_PGMASC)
+    sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
+#if HAVE_PNG
+  else if (opt_format == O_FORMAT_PNG)
+    sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax);
+  else if (opt_format == O_FORMAT_PNG16)
+    sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax);
+#endif
+#if HAVE_GIF
+  else if (opt_format == O_FORMAT_GIF) 
+    sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
+#endif
+  else if (opt_format == O_FORMAT_DISP) {
+#if HAVE_SGP
+      image_display_scale (im, opt_scale, densmin, densmax);
+      cio_kb_getc();
+      sgp2_close(sgp2_get_active_win());
+#endif
+  }
+  else
+    {
+      fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
+      return (1);
+    }
+  return (0);
+}
+
+
+void 
+sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+  FILE *fp;
+  int irow;
+  unsigned char *rowp;
+
+  rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
+  if (rowp == NULL)
+    return;
+
+  if ((fp = fopen (outfile, "wb")) == NULL)
+     return;
+
+  fprintf(fp, "P5\n");
+  fprintf(fp, "%d %d\n", im->nx, im->ny);
+  fprintf(fp, "255\n");
+
+  for (irow = im->ny - 1; irow >= 0; irow--)
+    {
+      int icol, ir;
+
+      for (icol = 0; icol < im->nx; icol++)
+       {
+         double dens;
+         int p;
+         int pos = icol * nxcell;
+         dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
+         if (dens < 0)
+           dens = 0;
+         else if (dens > 1)
+           dens = 1;
+         for (p = pos; p < pos + nxcell; p++)
+           {
+             rowp[p] = (unsigned int) (dens * 255.);
+           }
+       }
+      for (ir = 0; ir < nycell; ir++)
+       {
+         int ic;
+         for (ic = 0; ic < im->nx * nxcell; ic++) 
+           fprintf(fp, "%c ", rowp[ic]);
+       }
+    }
+  sys_free(rowp, "sdf2img");
+
+  fclose(fp);
+}
+
+void 
+sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+  FILE *fp;
+  int irow;
+  unsigned char *rowp;
+
+  rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
+  if (rowp == NULL)
+    return;
+
+  if ((fp = fopen (outfile, "wb")) == NULL)
+     return;
+
+  fprintf(fp, "P2\n");
+  fprintf(fp, "%d %d\n", im->nx, im->ny);
+  fprintf(fp, "255\n");
+
+  for (irow = im->ny - 1; irow >= 0; irow--)
+    {
+      int icol, ir;
+
+      for (icol = 0; icol < im->nx; icol++)
+       {
+         double dens;
+         int p;
+         int pos = icol * nxcell;
+         dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
+         if (dens < 0)
+           dens = 0;
+         else if (dens > 1)
+           dens = 1;
+         for (p = pos; p < pos + nxcell; p++)
+           {
+             rowp[p] = (unsigned int) (dens * 255.);
+           }
+       }
+      for (ir = 0; ir < nycell; ir++)
+       {
+         int ic;
+         for (ic = 0; ic < im->nx * nxcell; ic++) 
+           fprintf(fp, "%d ", rowp[ic]);
+         fprintf(fp, "\n");
+       }
+    }
+  sys_free(rowp, "sdf2img");
+
+  fclose(fp);
+}
+
+
+#ifdef HAVE_PNG
+void 
+sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
+{
+  FILE *fp;
+  png_structp png_ptr;
+  png_infop info_ptr;
+  int irow;
+  unsigned char *rowp;
+  double max_out_level = (1 << bitdepth) - 1;
+
+  rowp = (unsigned char *) sys_alloc(im->nx * nxcell * (bitdepth / 8),"sdf2d_to_img");
+  if (rowp == NULL)
+    return;
+
+  if ((fp = fopen (outfile, "wb")) == NULL)
+     return;
+
+  png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+  if (! png_ptr)
+    return;
+
+  info_ptr = png_create_info_struct(png_ptr);
+  if (! info_ptr)
+    {
+      png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
+      fclose(fp);
+      return;
+    }
+
+  if (setjmp(png_ptr->jmpbuf))
+    {
+      png_destroy_write_struct(&png_ptr, &info_ptr);
+      fclose(fp);
+      return;
+    }
+
+  png_init_io(png_ptr, fp);
+
+  png_set_IHDR(png_ptr, info_ptr, im->nx * nxcell, im->ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
+
+  png_write_info(png_ptr, info_ptr);
+  for (irow = im->ny - 1; irow >= 0; irow--)
+    {
+      png_bytep row_pointer = rowp;
+      int icol, ir;
+
+      for (icol = 0; icol < im->nx; icol++)
+       {
+         double dens;
+         int p;
+          unsigned int outval;
+         int pos = icol * nxcell;
+         dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
+         if (dens < 0)
+           dens = 0;
+         else if (dens > 1)
+           dens = 1;
+          outval = (unsigned int) (dens * max_out_level);
+         for (p = pos; p < pos + nxcell; p++)
+                {
+               if (bitdepth == 8)
+                    rowp[p] = outval;
+               else {
+                    int rowpos = p * 2;
+                   rowp[rowpos] = (outval >> 8) & 0xFF;
+                   rowp[rowpos+1] = (outval & 0xFF);
+               }
+           }
+       }
+      for (ir = 0; ir < nycell; ir++)
+       {
+         png_write_rows (png_ptr, &row_pointer, 1);
+       }
+    }
+  sys_free(rowp, "sdf2img");
+
+  png_write_end(png_ptr, info_ptr);
+  png_destroy_write_struct(&png_ptr, &info_ptr);
+
+  fclose(fp);
+}
+#endif
+
+#ifdef HAVE_GD
+#include "gd.h"
+#define N_GRAYSCALE 256
+#endif
+
+void
+sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
+{
+#ifdef HAVE_GD
+  gdImagePtr gif;
+  FILE *out;
+  int gs_indices[N_GRAYSCALE];
+  int i;
+  int irow;
+  int lastrow;
+  unsigned char *rowp;
+
+  rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
+  if (rowp == NULL)
+    return;
+
+  gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
+  for (i = 0; i < N_GRAYSCALE; i++)
+    gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
+
+  lastrow = im->ny * nycell - 1;
+  for (irow = 0; irow < im->ny; irow++)
+    {
+      int icol, ir;
+      int rpos = irow * nycell;
+      for (ir = rpos; ir < rpos + nycell; ir++)
+       {
+         for (icol = 0; icol < im->nx; icol++)
+           {
+             double dens;
+             int ic;
+             int cpos = icol * nxcell;
+             dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
+             if (dens < 0)
+               dens = 0;
+             else if (dens > 1)
+               dens = 1;
+             for (ic = cpos; ic < cpos + nxcell; ic++)
+               {
+                 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
+                 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
+               }
+           }
+       }
+    }
+  sys_free(rowp, "sdf2img");
+
+  if ((out = fopen(outfile,"w")) == NULL)
+    {
+      fprintf(stderr,"Error opening output file %s for writing\n", outfile);
+      exit(1);
+    }
+  gdImageGif(gif,out);
+  fclose(out);
+  gdImageDestroy(gif);
+#else
+  fprintf(stderr, "This version does not support GIF");
+#endif
+}
+
+#ifndef NO_MAIN
+int 
+main (int argc, char *const argv[])
+{
+  return (sdf2img_main(argc, argv));
+}
+#endif
diff --git a/src/sdfinfo.cpp b/src/sdfinfo.cpp
new file mode 100644 (file)
index 0000000..b6d71f8
--- /dev/null
@@ -0,0 +1,241 @@
+/*****************************************************************************
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
+**
+**  $Id: sdfinfo.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
+**  $Log: sdfinfo.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
+ *   sdfinfo.c             Display info on sdf files
+ */
+
+#include "ct.h"
+
+enum { O_LABELS, O_STATS, O_NO_STATS, O_NO_LABELS, O_VERBOSE, O_HELP, O_VERSION, O_DEBUG };
+
+static struct option my_options[] =
+{
+  {"labels", 0, 0, O_LABELS},
+  {"no-labels", 0, 0, O_NO_LABELS},
+  {"stats", 0, 0, O_STATS},
+  {"no-stats", 0, 0, O_NO_STATS},
+  {"debug", 0, 0, O_DEBUG},
+  {"verbose", 0, 0, O_VERBOSE},
+  {"help", 0, 0, O_HELP},
+  {"version", 0, 0, O_VERSION},
+  {0, 0, 0, 0}
+};
+
+void 
+sdfinfo_usage (const char *program)
+{
+  fprintf(stdout, "sdfinfo_usage: %s infile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "Analyze an SDF2D file\n");
+  fprintf(stdout, "\n");
+  fprintf(stdout, "     infile       Name of input SDF file\n");
+  fprintf(stdout, "     --display    Display image\n");
+  fprintf(stdout, "     --labels     Print image labels (default)\n");
+  fprintf(stdout, "     --no-labels  Do not print image labels\n");
+  fprintf(stdout, "     --stats      Print image statistics (default)\n");
+  fprintf(stdout, "     --no-stats   Do not print image statistics\n");
+  fprintf(stdout, "     --debug      Debug mode\n");
+  fprintf(stdout, "     --verbose    Verbose mode\n");
+  fprintf(stdout, "     --version    Print version\n");
+  fprintf(stdout, "     --help       Print this help message\n");
+}
+
+int 
+sdfinfo_main (int argc, char *const argv[])
+{
+  IMAGE *im;
+  char *in_file;
+  int opt_verbose = 0;
+  int opt_stats = 1;
+  int opt_labels = 1;
+  int opt_debug = 0;
+
+  while (1)
+    {
+      int c = getopt_long (argc, argv, "", my_options, NULL);
+      
+      if (c == -1)
+       break;
+      
+      switch (c)
+       {
+       case O_LABELS:
+         opt_labels = 1;
+         break;
+       case O_STATS:
+         opt_stats = 1;
+         break;
+       case O_NO_LABELS:
+         opt_labels = 0;
+         break;
+       case O_NO_STATS:
+         opt_stats = 0;
+         break;
+       case O_VERBOSE:
+         opt_verbose = 1;
+         break;
+       case O_DEBUG:
+         opt_debug = 0;
+         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 '?':
+         sdfinfo_usage(argv[0]);
+         return (0);
+       default:
+         sdfinfo_usage(argv[0]);
+         return (1);
+       }
+    }
+
+  if (optind + 1 != argc)
+    {
+      sdfinfo_usage(argv[0]);
+      return (1);
+    }
+  
+  in_file = argv[optind];
+
+  im = image_load (in_file);
+
+  if (opt_labels) 
+    {
+      int nlabels = im->dfp_2d->dfp->num_label;
+      int i;
+
+      for (i = 0; i < nlabels; i++)
+       {
+         SDF_BLK blk;
+         if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
+           break;
+         if (blk.lab.label_type == LT_HISTORY) {
+           fprintf(stdout, "History: %s\n", blk.lab.label_str);
+           fprintf(stdout, "  calc time = %.2f secs\n", blk.lab.calc_time);
+           fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
+         } else if (blk.lab.label_type == LT_TITLE) {
+           fprintf(stdout, "Title: %s\n", blk.lab.label_str);
+           fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
+         } else if (blk.lab.label_type == LT_NOTE) {
+           fprintf(stdout, "Note: %s\n", blk.lab.label_str);
+           fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
+         }
+       }
+    }
+
+  if (opt_stats)
+    {
+      double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
+      double mode = 0, mean = 0, stddev = 0;
+      double spread;
+      int hist[256];
+      int ibin, nbin = 256;
+      int max_bin, max_binindex; 
+      double maxbin;
+      int ix, iy;
+
+      maxbin = nbin - 1;
+      for (ix = 0; ix < im->nx; ix++)
+       {
+         for (iy = 0; iy < im->ny; iy++)
+           {
+             if (im->v[ix][iy] > maxfound)
+               maxfound = im->v[ix][iy];
+             if (im->v[ix][iy] < minfound)
+               minfound = im->v[ix][iy];
+             mean += im->v[ix][iy];
+           }
+       }
+      spread = maxfound - minfound;
+      if (spread == 0)
+       mode = minfound;
+      else
+       {
+         for (ibin = 0; ibin < nbin; ibin++)
+           hist[ibin] = 0;
+         for (ix = 0; ix < im->nx; ix++)
+           {
+             for (iy = 0; iy < im->ny; iy++)
+               {
+                 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
+                 hist[b]++;
+               }
+           }
+         max_binindex = 0;
+         max_bin = -1;
+         for (ibin = 0; ibin < nbin; ibin++)
+           {
+             if (hist[ibin] > max_bin)
+               {
+                 max_bin = hist[ibin];
+                 max_binindex = ibin;
+               }
+           }
+         mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
+       }
+
+      mean /= im->nx * im->ny;
+      for (ix = 0; ix < im->nx; ix++)
+       {
+         for (iy = 0; iy < im->ny; iy++)
+           {
+             double diff = (im->v[ix][iy] - mean);
+             stddev += diff * diff;
+           }
+       }
+      stddev = sqrt(stddev / (im->nx * im->ny));
+      fprintf(stdout,"nx=%d\n", im->nx);
+      fprintf(stdout,"ny=%d\n", im->ny);
+      fprintf(stdout,"min=%f\n", minfound);
+      fprintf(stdout,"max=%f\n", maxfound);
+      fprintf(stdout,"mean=%f\n", mean);
+      fprintf(stdout,"mode=%f\n", mode);
+      fprintf(stdout,"stddev=%f\n", stddev);
+    }
+  
+  return (0);
+}
+
+#ifndef NO_MAIN
+int 
+main (int argc, char *const argv[])
+{
+  return (sdfinfo_main(argc, argv));
+}
+#endif