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