r163: *** empty log message ***
[ctsim.git] / tools / if-2.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if-2.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: if-2.cpp,v 1.3 2000/07/28 10:51:31 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 void 
49 if2_usage (const char *program)
50 {
51   cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]" << endl;
52   cout << "Perform functions on two input image files" << endl;
53   cout << endl;
54   cout << "     infile1            Name of first input IF file" << endl;
55   cout << "     infile2            Name of second input IF file" << endl;
56   cout << "     outfile            Name of output IF file" << endl;
57   cout << "     --add              Add images" << endl;
58   cout << "     --sub              Subtract image 2 from image 1" << endl;
59   cout << "     --mul              Multiply images" << endl;
60   cout << "     --comp             Compare images" << endl;
61   cout << "     --column-plot n    Plot column\n";
62   cout << "     --row-plot n       Plot row\n";
63   cout << "     --verbose          Verbose modem" << endl;
64   cout << "     --version          Print version" << endl;
65   cout << "     --help             Print this help message" << endl;
66 }
67
68 int 
69 if2_main (int argc, char *const argv[])
70 {
71   ImageFile* pim_in1;
72   ImageFile* pim_in2;
73   ImageFile* pim_out = NULL;
74   string in_file1;
75   string in_file2;
76   string out_file;
77   int opt_verbose = 0;
78   int opt_add = 0;
79   int opt_sub = 0;
80   int opt_mul = 0;
81   int opt_comp = 0;
82   bool opt_outputFile = false;
83   int opt_rowPlot = -1;
84   int opt_columnPlot = -1;
85   Timer timerProgram;
86
87   while (1) {
88     char* endptr;
89     int c = getopt_long (argc, argv, "", my_options, NULL);
90       
91     if (c == -1)
92       break;
93       
94     switch (c) {
95     case O_ADD:
96       opt_add = 1;
97       opt_outputFile = true;
98       break;
99     case O_SUB :
100       opt_sub = 1;
101       opt_outputFile = true;
102       break;
103     case O_MUL:
104       opt_mul = 1;
105       opt_outputFile = true;
106       break;
107     case O_ROW_PLOT:
108       opt_rowPlot = strtol(optarg, &endptr, 10);
109       if (endptr != optarg + strlen(optarg)) {
110         if2_usage(argv[0]);
111       }
112       break;
113     case O_COLUMN_PLOT:
114       opt_columnPlot = strtol(optarg, &endptr, 10);
115       if (endptr != optarg + strlen(optarg)) {
116         if2_usage(argv[0]);
117       }
118       break;
119     case O_COMP:
120       opt_comp = 1;
121       break;
122     case O_VERBOSE:
123       opt_verbose = 1;
124       break;
125     case O_VERSION:
126 #ifdef VERSION
127       cout << "Version " << VERSION << endl;
128 #else
129       cout << "Unknown version number" << endl;
130 #endif
131       return (0);
132     case O_HELP:
133     case '?':
134       if2_usage(argv[0]);
135       return (0);
136     default:
137       if2_usage(argv[0]);
138       return (1);
139     }
140   }
141
142   if (opt_outputFile && (optind + 3 != argc)) {
143     if2_usage(argv[0]);
144     return (1);
145   }
146   else if (! opt_outputFile && optind + 2 != argc) {
147     if2_usage(argv[0]);
148     return (1);
149   }
150   
151   in_file1 = argv[optind];
152   in_file2 = argv[optind + 1];
153   if (opt_outputFile)
154     out_file = argv[optind + 2];
155
156   pim_in1 = new ImageFile ();
157   pim_in2 = new ImageFile ();
158   ImageFile& im_in1 = *pim_in1;
159   ImageFile& im_in2 = *pim_in2;
160
161   if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
162       sys_error (ERR_WARNING, "Error reading an image");
163       return (1);
164   }
165
166   if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
167     sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
168             im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
169     return(1);
170   }
171   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
172       sys_error (ERR_SEVERE, "Error: Size of image < 0");
173       return(1);
174   }
175
176   ImageFileArray v1 = im_in1.getArray();
177   ImageFileArray v2 = im_in2.getArray();
178   ImageFileArray vout = NULL;
179
180   if (opt_outputFile) {
181     pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
182     vout = pim_out->getArray();
183   }
184
185   string strOperation;
186   int nx = im_in1.nx();
187   int ny = im_in1.ny();
188   int nx2 = im_in2.nx();
189   int ny2 = im_in2.ny();
190
191   if (opt_add) {
192     strOperation = "Add Images";
193     for (int ix = 0; ix < nx; ix++) {
194       ImageFileColumn in1 = v1[ix];
195       ImageFileColumn in2 = v2[ix];
196       ImageFileColumn out = vout[ix];
197       for (int iy = 0; iy < ny; iy++)
198         *out++ = *in1++ + *in2++;
199     }
200   } else if (opt_sub) {
201     strOperation = "Subtract Images";
202     for (int ix = 0; ix < nx; ix++) {
203       ImageFileColumn in1 = v1[ix];
204       ImageFileColumn in2 = v2[ix];
205       ImageFileColumn out = vout[ix];
206       for (int iy = 0; iy < ny; iy++)
207           *out++ = *in1++ - *in2++;
208     }
209   } else if (opt_mul) {
210     strOperation = "Multiply Images";
211     for (int ix = 0; ix < nx; ix++) {
212       ImageFileColumn in1 = v1[ix];
213       ImageFileColumn in2 = v2[ix];
214       ImageFileColumn out = vout[ix];
215       for (int iy = 0; iy < ny; iy++)
216         *out++ = *in1++ * *in2++;
217     }
218   }
219   if (opt_comp) {
220     double d, r, e;
221     im_in1.comparativeStatistics (im_in2, d, r, e);
222     cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
223   }
224   if (opt_columnPlot > 0) {
225     if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
226       sys_error (ERR_SEVERE, "column-plot > nx");
227       return (1);
228     }
229     double plot_xaxis [nx];
230     for (int i = 0; i < nx; i++)
231       plot_xaxis[i] = i;
232 #if HAVE_SGP
233     SGPDriver driver ("Column Plot");
234     SGP sgp (driver);
235     EZPlot ezplot (sgp);
236     ezplot.ezset ("clear.");
237     ezplot.ezset ("xticks major 5.");
238     ezplot.ezset ("xlabel Column");
239     ezplot.ezset ("title Column Plot");
240     ezplot.ezset ("ylabel Pixel");
241     ezplot.ezset ("box.");
242     ezplot.ezset ("grid.");
243     ezplot.addCurve (plot_xaxis, v1[opt_columnPlot], im_in1.ny());
244     ezplot.addCurve (plot_xaxis, v2[opt_columnPlot], im_in2.ny());
245     ezplot.plot();
246     cout << "Press enter to continue" << flush;
247     cio_kb_getc();
248 #endif
249   }
250
251   if (opt_rowPlot > 0) {
252     if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
253       sys_error (ERR_SEVERE, "row_plot > ny");
254       return (1);
255     }
256     double plot_xaxis [ny];
257     double v1Row[nx], v2Row[nx2];
258
259     for (int i = 0; i < ny; i++)
260       plot_xaxis[i] = i;
261     for (int i = 0; i < nx; i++)
262       v1Row[i] = v1[i][opt_rowPlot];
263     for (int i = 0; i < nx2; i++)
264       v2Row[i] = v2[i][opt_rowPlot];
265
266 #if HAVE_SGP
267     SGPDriver driver ("Row Plot");
268     SGP sgp (driver);
269     EZPlot ezplot (sgp);
270     ezplot.ezset ("clear.");
271     ezplot.ezset ("xticks major 5.");
272     ezplot.ezset ("title Row Plot");
273     ezplot.ezset ("xlabel Row");
274     ezplot.ezset ("ylabel Pixel");
275     ezplot.ezset ("box.");
276     ezplot.ezset ("grid.");
277     ezplot.addCurve (plot_xaxis, v1Row, im_in1.nx());
278     ezplot.addCurve (plot_xaxis, v2Row, im_in2.nx());
279     ezplot.plot();
280     cout << "Press enter to continue" << flush;
281     cio_kb_getc();
282 #endif
283   }
284
285   if (opt_outputFile) {
286     pim_out->labelsCopy (im_in1, "if-2 file 1: ");
287     pim_out->labelsCopy (im_in2, "if-2 file 2: ");
288     pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
289     pim_out->fileWrite(out_file);
290   }
291
292   return (0);
293 }
294
295 #ifndef NO_MAIN
296 int 
297 main (int argc, char *const argv[])
298 {
299   int retval = 1;
300
301   try {
302     retval = if2_main(argc, argv);
303   } catch (exception e) {
304     cerr << "Exception: " << e.what() << endl;
305   } catch (...) {
306     cerr << "Unknown exception" << endl;
307   }
308
309   return (retval);
310 }
311 #endif
312