r285: rename if-2 to if2
[ctsim.git] / tools / if2.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if2.cpp
5 **   Purpose:       Manipulate two image files
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  May 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: if2.cpp,v 1.1 2000/12/18 00:24:39 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28
29 #include "ct.h"
30 #include "timer.h"
31
32 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_ROW_PLOT, O_COLUMN_PLOT, O_VERBOSE, O_HELP, O_VERSION};
33
34 static struct option my_options[] =
35 {
36   {"add", 0, 0, O_ADD},
37   {"sub", 0, 0, O_SUB},
38   {"mul", 0, 0, O_MUL},
39   {"comp", 0, 0, O_COMP},
40   {"column-plot", 1, 0, O_COLUMN_PLOT},
41   {"row-plot", 1, 0, O_ROW_PLOT},
42   {"verbose", 0, 0, O_VERBOSE},
43   {"help", 0, 0, O_HELP},
44   {"version", 0, 0, O_VERSION},
45   {0, 0, 0, 0}
46 };
47
48 static const char* g_szIdStr = "$Id: if2.cpp,v 1.1 2000/12/18 00:24:39 kevin Exp $";
49
50 void 
51 if2_usage (const char *program)
52 {
53   std::cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]\n";
54   std::cout << "Perform functions on two input image files\n";
55   std::cout << std::endl;
56   std::cout << "     infile1            Name of first input IF file\n";
57   std::cout << "     infile2            Name of second input IF file\n";
58   std::cout << "     outfile            Name of output IF file\n";
59   std::cout << "     --add              Add images\n";
60   std::cout << "     --sub              Subtract image 2 from image 1\n";
61   std::cout << "     --mul              Multiply images\n";
62   std::cout << "     --comp             Compare images\n";
63   std::cout << "     --column-plot n    Plot column\n";
64   std::cout << "     --row-plot n       Plot row\n";
65   std::cout << "     --verbose          Verbose modem\n";
66   std::cout << "     --version          Print version\n";
67   std::cout << "     --help             Print this help message\n";
68 }
69
70 int 
71 if2_main (int argc, char *const argv[])
72 {
73   ImageFile* pim_in1;
74   ImageFile* pim_in2;
75   ImageFile* pim_out = NULL;
76   std::string in_file1;
77   std::string in_file2;
78   std::string out_file;
79   int opt_verbose = 0;
80   int opt_add = 0;
81   int opt_sub = 0;
82   int opt_mul = 0;
83   int opt_comp = 0;
84   bool opt_outputFile = false;
85   int opt_rowPlot = -1;
86   int opt_columnPlot = -1;
87   Timer timerProgram;
88
89   while (1) {
90     char* endptr;
91     int c = getopt_long (argc, argv, "", my_options, NULL);
92       
93     if (c == -1)
94       break;
95       
96     switch (c) {
97     case O_ADD:
98       opt_add = 1;
99       opt_outputFile = true;
100       break;
101     case O_SUB :
102       opt_sub = 1;
103       opt_outputFile = true;
104       break;
105     case O_MUL:
106       opt_mul = 1;
107       opt_outputFile = true;
108       break;
109     case O_ROW_PLOT:
110       opt_rowPlot = strtol(optarg, &endptr, 10);
111       if (endptr != optarg + strlen(optarg)) {
112         if2_usage(argv[0]);
113       }
114       break;
115     case O_COLUMN_PLOT:
116       opt_columnPlot = strtol(optarg, &endptr, 10);
117       if (endptr != optarg + strlen(optarg)) {
118         if2_usage(argv[0]);
119       }
120       break;
121     case O_COMP:
122       opt_comp = 1;
123       break;
124     case O_VERBOSE:
125       opt_verbose = 1;
126       break;
127     case O_VERSION:
128 #ifdef VERSION
129       std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
130 #else
131       std::cout << "Unknown version number\n";
132 #endif
133       return (0);
134     case O_HELP:
135     case '?':
136       if2_usage(argv[0]);
137       return (0);
138     default:
139       if2_usage(argv[0]);
140       return (1);
141     }
142   }
143
144   if (opt_outputFile && (optind + 3 != argc)) {
145     if2_usage(argv[0]);
146     return (1);
147   }
148   else if (! opt_outputFile && optind + 2 != argc) {
149     if2_usage(argv[0]);
150     return (1);
151   }
152   
153   in_file1 = argv[optind];
154   in_file2 = argv[optind + 1];
155   if (opt_outputFile)
156     out_file = argv[optind + 2];
157
158   pim_in1 = new ImageFile ();
159   pim_in2 = new ImageFile ();
160   ImageFile& im_in1 = *pim_in1;
161   ImageFile& im_in2 = *pim_in2;
162
163   if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
164       sys_error (ERR_WARNING, "Error reading an image");
165       return (1);
166   }
167
168   if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
169     sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
170             im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
171     return(1);
172   }
173   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
174       sys_error (ERR_SEVERE, "Error: Size of image < 0");
175       return(1);
176   }
177
178   ImageFileArray v1 = im_in1.getArray();
179   ImageFileArray v2 = im_in2.getArray();
180   ImageFileArray vout = NULL;
181
182   if (opt_outputFile) {
183     pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
184     vout = pim_out->getArray();
185   }
186
187   std::string strOperation;
188   int nx = im_in1.nx();
189   int ny = im_in1.ny();
190   int nx2 = im_in2.nx();
191   int ny2 = im_in2.ny();
192
193   if (opt_add) {
194     strOperation = "Add Images";
195     for (int ix = 0; ix < nx; ix++) {
196       ImageFileColumn in1 = v1[ix];
197       ImageFileColumn in2 = v2[ix];
198       ImageFileColumn out = vout[ix];
199       for (int iy = 0; iy < ny; iy++)
200         *out++ = *in1++ + *in2++;
201     }
202   } else if (opt_sub) {
203     strOperation = "Subtract Images";
204     for (int ix = 0; ix < nx; ix++) {
205       ImageFileColumn in1 = v1[ix];
206       ImageFileColumn in2 = v2[ix];
207       ImageFileColumn out = vout[ix];
208       for (int iy = 0; iy < ny; iy++)
209           *out++ = *in1++ - *in2++;
210     }
211   } else if (opt_mul) {
212     strOperation = "Multiply Images";
213     for (int ix = 0; ix < nx; ix++) {
214       ImageFileColumn in1 = v1[ix];
215       ImageFileColumn in2 = v2[ix];
216       ImageFileColumn out = vout[ix];
217       for (int iy = 0; iy < ny; iy++)
218         *out++ = *in1++ * *in2++;
219     }
220   }
221   if (opt_comp) {
222     double d, r, e;
223     im_in1.comparativeStatistics (im_in2, d, r, e);
224     std::cout << "d=" << d << ", r=" << r << ", e=" << e << std::endl;
225   }\r
226   int i;
227   if (opt_columnPlot > 0) {
228     if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
229       sys_error (ERR_SEVERE, "column-plot > nx");
230       return (1);
231     }
232     double* plot_xaxis = new double [nx];
233     for (i = 0; i < nx; i++)
234       plot_xaxis[i] = i;
235 #if HAVE_SGP
236     SGPDriver driver ("Column Plot");
237     SGP sgp (driver);
238     EZPlot ezplot (sgp);
239     ezplot.ezset ("clear.");
240     ezplot.ezset ("xticks major 5.");
241     ezplot.ezset ("xlabel Column");
242     ezplot.ezset ("title Column Plot");
243     ezplot.ezset ("ylabel Pixel");
244     ezplot.ezset ("box.");
245     ezplot.ezset ("grid.");
246     ezplot.addCurve (plot_xaxis, v1[opt_columnPlot], im_in1.ny());
247     ezplot.addCurve (plot_xaxis, v2[opt_columnPlot], im_in2.ny());
248     ezplot.plot();
249     std::cout << "Press enter to continue" << flush;
250     cio_kb_getc();\r
251 #endif\r
252         delete plot_xaxis;
253   }
254
255   if (opt_rowPlot > 0) {
256     if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
257       sys_error (ERR_SEVERE, "row_plot > ny");
258       return (1);
259     }
260     double* plot_xaxis = new double [ny];
261     double* v1Row = new double [nx];\r
262         double* v2Row = new double [nx2];
263
264     for (i = 0; i < ny; i++)
265       plot_xaxis[i] = i;
266     for (i = 0; i < nx; i++)
267       v1Row[i] = v1[i][opt_rowPlot];
268     for (i = 0; i < nx2; i++)
269       v2Row[i] = v2[i][opt_rowPlot];
270
271 #if HAVE_SGP
272     SGPDriver driver ("Row Plot");
273     SGP sgp (driver);
274     EZPlot ezplot (sgp);
275     ezplot.ezset ("clear.");
276     ezplot.ezset ("xticks major 5.");
277     ezplot.ezset ("title Row Plot");
278     ezplot.ezset ("xlabel Row");
279     ezplot.ezset ("ylabel Pixel");
280     ezplot.ezset ("box.");
281     ezplot.ezset ("grid.");
282     ezplot.addCurve (plot_xaxis, v1Row, im_in1.nx());
283     ezplot.addCurve (plot_xaxis, v2Row, im_in2.nx());
284     ezplot.plot();
285     std::cout << "Press enter to continue" << flush;
286     cio_kb_getc();
287 #endif
288         delete plot_xaxis;\r
289         delete v1Row;\r
290         delete v2Row;\r
291   }
292
293 \r
294         if (opt_outputFile) {
295     pim_out->labelsCopy (im_in1, "if2 file 1: ");
296     pim_out->labelsCopy (im_in2, "if2 file 2: ");
297     pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
298     pim_out->fileWrite(out_file);
299   }
300
301   return (0);
302 }
303
304 #ifndef NO_MAIN
305 int 
306 main (int argc, char *const argv[])
307 {
308   int retval = 1;
309
310   try {
311     retval = if2_main(argc, argv);
312   } catch (exception e) {
313     std::cerr << "Exception: " << e.what() << std::endl;
314   } catch (...) {
315     std::cerr << "Unknown exception\n";
316   }
317
318   return (retval);
319 }
320 #endif
321