r72: Initial C++ versions
[ctsim.git] / src / ifinfo.cpp
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