Use setprecision for std::cout
[ctsim.git] / tools / if2.cpp
index db92c228aee511cbc53b788b4f0e65539f5a4db7..b264046360d4437c333d0e5750da553fcc0d1efe 100644 (file)
@@ -7,9 +7,7 @@
 **   Date Started:  May 2000
 **
 **  This is part of the CTSim program
-**  Copyright (C) 1983-2000 Kevin Rosenberg
-**
-**  $Id: if2.cpp,v 1.3 2000/12/19 21:37:51 kevin Exp $
+**  Copyright (C) 1983-2009 Kevin Rosenberg
 **
 **  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
 #include "ct.h"
 #include "timer.h"
 
-enum {O_ADD, O_SUB, O_MUL, O_COMP, O_ROW_PLOT, O_COLUMN_PLOT, O_VERBOSE, O_HELP, O_VERSION};
+enum {O_ADD, O_SUB, O_MUL, O_DIVIDE, O_COMP, O_ROW_PLOT, O_COLUMN_PLOT, 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},
+  {"multiply", 0, 0, O_MUL},
+  {"divide", 0, 0, O_DIVIDE},
   {"comp", 0, 0, O_COMP},
   {"column-plot", 1, 0, O_COLUMN_PLOT},
   {"row-plot", 1, 0, O_ROW_PLOT},
@@ -45,9 +44,9 @@ static struct option my_options[] =
   {0, 0, 0, 0}
 };
 
-static const char* g_szIdStr = "$Id: if2.cpp,v 1.3 2000/12/19 21:37:51 kevin Exp $";
+static const char* g_szIdStr = "$Id$";
 
-void 
+void
 if2_usage (const char *program)
 {
   std::cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]\n";
@@ -67,7 +66,7 @@ if2_usage (const char *program)
   std::cout << "     --help             Print this help message\n";
 }
 
-int 
+int
 if2_main (int argc, char *const argv[])
 {
   ImageFile* pim_in1;
@@ -80,20 +79,22 @@ if2_main (int argc, char *const argv[])
   int opt_add = 0;
   int opt_sub = 0;
   int opt_mul = 0;
+  bool opt_divide = false;
   int opt_comp = 0;
   bool opt_bImageOutputFile = false;
   bool opt_bPlotOutputFile = false;
   int opt_rowPlot = -1;
   int opt_columnPlot = -1;
   Timer timerProgram;
-
+  UNUSED(opt_verbose);
+  
   while (1) {
     char* endptr;
     int c = getopt_long (argc, argv, "", my_options, NULL);
-      
+
     if (c == -1)
       break;
-      
+
     switch (c) {
     case O_ADD:
       opt_add = 1;
@@ -107,17 +108,21 @@ if2_main (int argc, char *const argv[])
       opt_mul = 1;
       opt_bImageOutputFile = true;
       break;
+    case O_DIVIDE:
+      opt_divide = true;
+      opt_bImageOutputFile = true;
+      break;
     case O_ROW_PLOT:
       opt_rowPlot = strtol(optarg, &endptr, 10);
       if (endptr != optarg + strlen(optarg)) {
-       if2_usage(argv[0]);
+        if2_usage(argv[0]);
       }
       opt_bPlotOutputFile = true;
       break;
     case O_COLUMN_PLOT:
       opt_columnPlot = strtol(optarg, &endptr, 10);
       if (endptr != optarg + strlen(optarg)) {
-       if2_usage(argv[0]);
+        if2_usage(argv[0]);
       }
       opt_bPlotOutputFile = true;
       break;
@@ -152,7 +157,7 @@ if2_main (int argc, char *const argv[])
     if2_usage(argv[0]);
     return (1);
   }
-  
+
   in_file1 = argv[optind];
   in_file2 = argv[optind + 1];
   if (opt_bImageOutputFile || opt_bPlotOutputFile)
@@ -164,33 +169,34 @@ if2_main (int argc, char *const argv[])
   ImageFile& im_in2 = *pim_in2;
 
   if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
-      sys_error (ERR_WARNING, "Error reading an image");
-      return (1);
+    sys_error (ERR_WARNING, "Error reading an image");
+    return (1);
   }
 
   if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
     sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
-           im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
+      im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
     return(1);
   }
   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
-      sys_error (ERR_SEVERE, "Error: Size of image < 0");
-      return(1);
+    sys_error (ERR_SEVERE, "Error: Size of image < 0");
+    return(1);
   }
 
-  ImageFileArray v1 = im_in1.getArray();
-  ImageFileArray v2 = im_in2.getArray();
-  ImageFileArray vout = NULL;
-
   if (opt_bImageOutputFile && opt_bPlotOutputFile) {
     sys_error (ERR_SEVERE, "Both Image and Plot output files can not be selected simultaneously");
     return (1);
   }
+
+  ImageFileArray v1 = im_in1.getArray();
+  ImageFileArray v2 = im_in2.getArray();
+  ImageFileArray vout = NULL;
   if (opt_bImageOutputFile) {
     pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
     vout = pim_out->getArray();
   }
-
+  UNUSED(vout);
+  
   std::string strOperation;
   int nx = im_in1.nx();
   int ny = im_in1.ny();
@@ -199,38 +205,24 @@ if2_main (int argc, char *const argv[])
 
   if (opt_add) {
     strOperation = "Add Images";
-    for (int ix = 0; ix < nx; ix++) {
-      ImageFileColumn in1 = v1[ix];
-      ImageFileColumn in2 = v2[ix];
-      ImageFileColumn out = vout[ix];
-      for (int iy = 0; iy < ny; iy++)
-       *out++ = *in1++ + *in2++;
-    }
+    im_in1.addImages (im_in2, *pim_out);
   } else if (opt_sub) {
     strOperation = "Subtract Images";
-    for (int ix = 0; ix < nx; ix++) {
-      ImageFileColumn in1 = v1[ix];
-      ImageFileColumn in2 = v2[ix];
-      ImageFileColumn out = vout[ix];
-      for (int iy = 0; iy < ny; iy++)
-         *out++ = *in1++ - *in2++;
-    }
+    im_in1.subtractImages (im_in2, *pim_out);
   } else if (opt_mul) {
     strOperation = "Multiply Images";
-    for (int ix = 0; ix < nx; ix++) {
-      ImageFileColumn in1 = v1[ix];
-      ImageFileColumn in2 = v2[ix];
-      ImageFileColumn out = vout[ix];
-      for (int iy = 0; iy < ny; iy++)
-       *out++ = *in1++ * *in2++;
-    }
+    im_in1.multiplyImages (im_in2, *pim_out);
+  } else if (opt_divide) {
+    strOperation = "Divide Images";
+    im_in1.divideImages (im_in2, *pim_out);
   }
   if (opt_comp) {
     double d, r, e;
     im_in1.comparativeStatistics (im_in2, d, r, e);
-    std::cout << "d=" << d << ", r=" << r << ", e=" << e << std::endl;
+    std::cout << std::setprecision(7) <<
+      "d=" << d << ", r=" << r << ", e=" << e << std::endl;
   }
-\r
+
   int i;
   if (opt_columnPlot > 0) {
     if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
@@ -240,35 +232,26 @@ if2_main (int argc, char *const argv[])
     double* plot_xaxis = new double [nx];
     for (i = 0; i < nx; i++)
       plot_xaxis[i] = i;
-\r
-    PlotFile plotFile (3, nx);\r
-\r
-    plotFile.addColumn (0, plot_xaxis);\r
-    plotFile.addColumn (1, v1[opt_columnPlot]);\r
-    plotFile.addColumn (2, v2[opt_columnPlot]);\r
-    plotFile.setTitle ("Column Plot");\r
-    plotFile.setXLabel ("Column");\r
-    plotFile.setYLabel ("Pixel Value");\r
-\r
-    plotFile.fileWrite (strOutFile.c_str());\r
-\r
-#if HAVE_SGP
-    SGPDriver driver ("Column Plot");
-    SGP sgp (driver);
-    EZPlot ezplot (sgp);
-    ezplot.ezset ("clear.");
-    ezplot.ezset ("xticks major 5.");
-    ezplot.ezset ("xlabel Column");
-    ezplot.ezset ("title Column Plot");
-    ezplot.ezset ("ylabel Pixel");
-    ezplot.ezset ("box.");
-    ezplot.ezset ("grid.");
-    ezplot.addCurve (plot_xaxis, v1[opt_columnPlot], im_in1.ny());
-    ezplot.addCurve (plot_xaxis, v2[opt_columnPlot], im_in2.ny());
-    ezplot.plot();
-    std::cout << "Press enter to continue" << flush;
-    cio_kb_getc();
-#endif
+
+    PlotFile plotFile (3, nx);
+
+    plotFile.addColumn (0, plot_xaxis);
+    plotFile.addColumn (1, v1[opt_columnPlot]);
+    plotFile.addColumn (2, v2[opt_columnPlot]);
+    std::ostringstream os;
+    os << "Column " << opt_columnPlot << " Comparison";
+    plotFile.addDescription (os.str().c_str());
+    std::string title("title ");
+    title += os.str();
+    plotFile.addEzsetCommand (title.c_str());
+    plotFile.addEzsetCommand ("xlabel Column");
+    plotFile.addEzsetCommand ("ylabel Pixel Value");
+    plotFile.addEzsetCommand ("box");
+    plotFile.addEzsetCommand ("grid");
+    plotFile.addEzsetCommand ("xticks major 5");
+
+    plotFile.fileWrite (strOutFile.c_str());
+
     delete plot_xaxis;
   }
 
@@ -278,8 +261,8 @@ if2_main (int argc, char *const argv[])
       return (1);
     }
     double* plot_xaxis = new double [ny];
-    double* v1Row = new double [ny];\r
-       double* v2Row = new double [ny2];
+    double* v1Row = new double [ny];
+    double* v2Row = new double [ny2];
 
     for (i = 0; i < ny; i++)
       plot_xaxis[i] = i;
@@ -293,32 +276,23 @@ if2_main (int argc, char *const argv[])
     plotFile.addColumn (0, plot_xaxis);
     plotFile.addColumn (1, v1Row);
     plotFile.addColumn (2, v2Row);
-    plotFile.setTitle ("Row Plot");
-    plotFile.setXLabel ("Row");
-    plotFile.setYLabel ("Pixel Value");
+    std::ostringstream os;
+    os << "Row " << opt_rowPlot << " Comparison";
+    plotFile.addDescription (os.str().c_str());
+    std::string title("title ");
+    title += os.str();
+    plotFile.addEzsetCommand (title.c_str());
+    plotFile.addEzsetCommand ("xlabel Row");
+    plotFile.addEzsetCommand ("ylabel Pixel Value");
+    plotFile.addEzsetCommand ("box");
+    plotFile.addEzsetCommand ("grid");
+    plotFile.addEzsetCommand ("xticks major 5");
 
     plotFile.fileWrite (strOutFile.c_str());
 
-#if HAVE_SGP
-    SGPDriver driver ("Row Plot");
-    SGP sgp (driver);
-    EZPlot ezplot (sgp);
-    ezplot.ezset ("clear.");
-    ezplot.ezset ("xticks major 5.");
-    ezplot.ezset ("title Row Plot");
-    ezplot.ezset ("xlabel Row");
-    ezplot.ezset ("ylabel Pixel");
-    ezplot.ezset ("box.");
-    ezplot.ezset ("grid.");
-    ezplot.addCurve (plot_xaxis, v1Row, im_in1.nx());
-    ezplot.addCurve (plot_xaxis, v2Row, im_in2.nx());
-    ezplot.plot();
-    std::cout << "Press enter to continue" << flush;
-    cio_kb_getc();
-#endif
-       delete plot_xaxis;\r
-       delete v1Row;\r
-       delete v2Row;\r
+    delete plot_xaxis;
+    delete v1Row;
+    delete v2Row;
   }
 
   if (opt_bImageOutputFile) {
@@ -327,12 +301,12 @@ if2_main (int argc, char *const argv[])
     pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
     pim_out->fileWrite (strOutFile);
   }
-\r
+
   return (0);
 }
 
 #ifndef NO_MAIN
-int 
+int
 main (int argc, char *const argv[])
 {
   int retval = 1;