r140: *** empty log message ***
[ctsim.git] / src / if-2.cpp
index 545920ad0d74475310afc64ef449a6dd2e05d3bf..7757f72da9071fddb45c1b2afd4e0d6f46e6277b 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: if-2.cpp,v 1.9 2000/06/28 15:25:34 kevin Exp $
+**  $Id: if-2.cpp,v 1.10 2000/07/09 08:16:18 kevin Exp $
 **
 **  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
@@ -32,7 +32,7 @@
 #include "ct.h"
 #include "timer.h"
 
-enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
+enum {O_ADD, O_SUB, O_MUL, O_COMP, O_ROW_PLOT, O_COLUMN_PLOT, O_VERBOSE, O_HELP, O_VERSION};
 
 static struct option my_options[] =
 {
@@ -40,6 +40,8 @@ static struct option my_options[] =
   {"sub", 0, 0, O_SUB},
   {"mul", 0, 0, O_MUL},
   {"comp", 0, 0, O_COMP},
+  {"column-plot", 1, 0, O_COLUMN_PLOT},
+  {"row-plot", 1, 0, O_ROW_PLOT},
   {"verbose", 0, 0, O_VERBOSE},
   {"help", 0, 0, O_HELP},
   {"version", 0, 0, O_VERSION},
@@ -50,18 +52,20 @@ void
 if2_usage (const char *program)
 {
   cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]" << endl;
-  cout << "Generate an image file from two input image files files" << endl;
+  cout << "Perform functions on two input image files" << endl;
   cout << endl;
-  cout << "     infile1    Name of first input IF file" << endl;
-  cout << "     infile2    Name of second input IF file" << endl;
-  cout << "     outfile    Name of output IF file" << endl;
-  cout << "     --add      Add images" << endl;
-  cout << "     --sub      Subtract image 2 from image 1" << endl;
-  cout << "     --mul      Multiply images" << endl;
-  cout << "     --comp     Compare images" << endl;
-  cout << "     --verbose  Verbose modem" << endl;
-  cout << "     --version  Print version" << endl;
-  cout << "     --help     Print this help message" << endl;
+  cout << "     infile1            Name of first input IF file" << endl;
+  cout << "     infile2            Name of second input IF file" << endl;
+  cout << "     outfile            Name of output IF file" << endl;
+  cout << "     --add              Add images" << endl;
+  cout << "     --sub              Subtract image 2 from image 1" << endl;
+  cout << "     --mul              Multiply images" << endl;
+  cout << "     --comp             Compare images" << endl;
+  cout << "     --column-plot n    Plot column\n";
+  cout << "     --row-plot n       Plot row\n";
+  cout << "     --verbose          Verbose modem" << endl;
+  cout << "     --version          Print version" << endl;
+  cout << "     --help             Print this help message" << endl;
 }
 
 int 
@@ -70,18 +74,21 @@ if2_main (int argc, char *const argv[])
   ImageFile* pim_in1;
   ImageFile* pim_in2;
   ImageFile* pim_out;
-  char *in_file1;
-  char *in_file2;
-  char *out_file;
+  string in_file1;
+  string in_file2;
+  string out_file;
   int opt_verbose = 0;
   int opt_add = 0;
   int opt_sub = 0;
   int opt_mul = 0;
   int opt_comp = 0;
-
+  int opt_outputFile = 0;
+  int opt_rowPlot = -1;
+  int opt_columnPlot = -1;
   Timer timerProgram;
 
   while (1) {
+    char* endptr;
     int c = getopt_long (argc, argv, "", my_options, NULL);
       
     if (c == -1)
@@ -90,13 +97,26 @@ if2_main (int argc, char *const argv[])
     switch (c) {
     case O_ADD:
       opt_add = 1;
+      opt_outputFile = 1;
       break;
     case O_SUB :
       opt_sub = 1;
+      opt_outputFile = 1;
       break;
     case O_MUL:
       opt_mul = 1;
+      opt_outputFile = 1;
       break;
+    case O_ROW_PLOT:
+      opt_rowPlot = strtol(optarg, &endptr, 10);
+      if (endptr != optarg + strlen(optarg)) {
+       if2_usage(argv[0]);
+      }
+    case O_COLUMN_PLOT:
+      opt_columnPlot = strtol(optarg, &endptr, 10);
+      if (endptr != optarg + strlen(optarg)) {
+       if2_usage(argv[0]);
+      }
     case O_COMP:
       opt_comp = 1;
       break;
@@ -119,15 +139,20 @@ if2_main (int argc, char *const argv[])
       return (1);
     }
   }
-  
-  if (optind + 3 != argc) {
+
+  if (opt_outputFile && (optind + 3 != argc)) {
+    if2_usage(argv[0]);
+    return (1);
+  }
+  else if (! opt_outputFile && optind + 2 != argc) {
     if2_usage(argv[0]);
     return (1);
   }
   
   in_file1 = argv[optind];
   in_file2 = argv[optind + 1];
-  out_file = argv[optind + 2];
+  if (opt_outputFile)
+    out_file = argv[optind + 2];
 
   pim_in1 = new ImageFile ();
   pim_in2 = new ImageFile ();
@@ -149,14 +174,16 @@ if2_main (int argc, char *const argv[])
       return(1);
   }
 
-  pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
-  ImageFile& im_out = *pim_out;
-
-  string strOperation;
   ImageFileArray v1 = im_in1.getArray();
   ImageFileArray v2 = im_in2.getArray();
-  ImageFileArray vout = im_out.getArray();
+  ImageFileArray vout = NULL;
+
+  if (opt_outputFile) {
+    pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
+    vout = pim_out->getArray();
+  }
 
+  string strOperation;
   if (opt_add) {
     strOperation = "Add Images";
     for (int ix = 0; ix < im_in1.nx(); ix++) {
@@ -184,28 +211,81 @@ if2_main (int argc, char *const argv[])
       for (int iy = 0; iy < im_in1.ny(); iy++)
        *out++ = *in1++ * *in2++;
     }
-  } else if (opt_comp) {
-    double abs_error = 0.;
-    strOperation = "Subtract Images";
-    for (int ix = 0; ix < im_in1.nx(); ix++) {
-      ImageFileColumn in1 = v1[ix];
-      ImageFileColumn in2 = v2[ix];
-      ImageFileColumn out = vout[ix];
-      for (int iy = 0; iy < im_in1.ny(); iy++) {
-       double diff = *in1++ - *in2++;
-       *out++ = diff;
-      }
-    }
+  }
+  if (opt_comp) {
     double d, r, e;
     im_in1.comparativeStatistics (im_in2, d, r, e);
     cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
   }
+  if (opt_columnPlot > 0) {
+    if (opt_columnPlot >= im_in1.nx() || opt_columnPlot >= im_in2.nx()) {
+      sys_error (ERR_SEVERE, "column-plot > nx");
+      return (1);
+    }
+    double plot_xaxis [im_in1.nx()];
+    for (int i = 0; i < im_in1.nx(); i++)
+      plot_xaxis[i] = i;
+#if HAVE_SGP
+#if 0
+#else
+    ezset  ("clear.");
+    ezset  ("xticks major 5.");
+    ezset  ("xlabel Column");
+    ezset  ("ylabel Pixel");
+    ezset ("curves 2");
+    ezset  ("box.");
+    ezset  ("grid.");
+    ezplot (v1[opt_columnPlot], plot_xaxis, im_in1.ny());
+    ezplot (v2[opt_columnPlot], plot_xaxis, im_in2.ny());
+#endif
+    char str[256];
+    cout << "Press enter to continue" << flush;
+    fgets(str, sizeof(str), stdin);
+    sgp2_close (sgp2_get_active_win());
+#endif
+  }
 
-  im_out.labelsCopy (im_in1, "if-2 file 1: ");
-  im_out.labelsCopy (im_in2, "if-2 file 2: ");
-  im_out.labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
+  if (opt_rowPlot > 0) {
+    if (opt_rowPlot >= im_in1.ny() || opt_rowPlot >= im_in2.ny()) {
+      sys_error (ERR_SEVERE, "row_plot > ny");
+      return (1);
+    }
+    double plot_xaxis [im_in1.ny()];
+    double v1Row[im_in1.nx()], v2Row[im_in2.nx()];
+
+    for (int i = 0; i < im_in1.ny(); i++)
+      plot_xaxis[i] = i;
+    for (int i = 0; i < im_in1.nx(); i++)
+      v1Row[i] = v1[opt_rowPlot][i];
+    for (int i = 0; i < im_in2.nx(); i++)
+      v2Row[i] = v2[opt_rowPlot][i];
 
-  im_out.fileWrite(out_file);
+#if HAVE_SGP
+#if 0
+#else
+    ezset  ("clear.");
+    ezset  ("xticks major 5.");
+    ezset  ("xlabel Column");
+    ezset  ("ylabel Pixel");
+    ezset ("curves 2");
+    ezset  ("box.");
+    ezset  ("grid.");
+    ezplot (v1Row, plot_xaxis, im_in1.nx());
+    ezplot (v2Row, plot_xaxis, im_in2.nx());
+#endif
+    char str[256];
+    cout << "Press enter to continue" << flush;
+    fgets(str, sizeof(str), stdin);
+    sgp2_close (sgp2_get_active_win());
+#endif
+  }
+
+  if (opt_outputFile) {
+    pim_out->labelsCopy (im_in1, "if-2 file 1: ");
+    pim_out->labelsCopy (im_in2, "if-2 file 2: ");
+    pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
+    pim_out->fileWrite(out_file);
+  }
 
   return (0);
 }