525330ea568fcf0813da766d724ba2ad412a21b4
[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.1 2000/07/13 07:01:35 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   int opt_outputFile = 0;
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 = 1;
98       break;
99     case O_SUB :
100       opt_sub = 1;
101       opt_outputFile = 1;
102       break;
103     case O_MUL:
104       opt_mul = 1;
105       opt_outputFile = 1;
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     case O_COLUMN_PLOT:
113       opt_columnPlot = strtol(optarg, &endptr, 10);
114       if (endptr != optarg + strlen(optarg)) {
115         if2_usage(argv[0]);
116       }
117     case O_COMP:
118       opt_comp = 1;
119       break;
120     case O_VERBOSE:
121       opt_verbose = 1;
122       break;
123     case O_VERSION:
124 #ifdef VERSION
125       cout << "Version " << VERSION << endl;
126 #else
127       cout << "Unknown version number" << endl;
128 #endif
129       return (0);
130     case O_HELP:
131     case '?':
132       if2_usage(argv[0]);
133       return (0);
134     default:
135       if2_usage(argv[0]);
136       return (1);
137     }
138   }
139
140   if (opt_outputFile && (optind + 3 != argc)) {
141     if2_usage(argv[0]);
142     return (1);
143   }
144   else if (! opt_outputFile && optind + 2 != argc) {
145     if2_usage(argv[0]);
146     return (1);
147   }
148   
149   in_file1 = argv[optind];
150   in_file2 = argv[optind + 1];
151   if (opt_outputFile)
152     out_file = argv[optind + 2];
153
154   pim_in1 = new ImageFile ();
155   pim_in2 = new ImageFile ();
156   ImageFile& im_in1 = *pim_in1;
157   ImageFile& im_in2 = *pim_in2;
158
159   if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
160       sys_error (ERR_WARNING, "Error reading an image");
161       return (1);
162   }
163
164   if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
165     sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
166             im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
167     return(1);
168   }
169   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
170       sys_error (ERR_SEVERE, "Error: Size of image < 0");
171       return(1);
172   }
173
174   ImageFileArray v1 = im_in1.getArray();
175   ImageFileArray v2 = im_in2.getArray();
176   ImageFileArray vout = NULL;
177
178   if (opt_outputFile) {
179     pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
180     vout = pim_out->getArray();
181   }
182
183   string strOperation;
184   int nx = im_in1.nx();
185   int ny = im_in1.ny();
186   int nx2 = im_in2.nx();
187   int ny2 = im_in2.ny();
188
189   if (opt_add) {
190     strOperation = "Add Images";
191     for (int ix = 0; ix < nx; ix++) {
192       ImageFileColumn in1 = v1[ix];
193       ImageFileColumn in2 = v2[ix];
194       ImageFileColumn out = vout[ix];
195       for (int iy = 0; iy < ny; iy++)
196         *out++ = *in1++ + *in2++;
197     }
198   } else if (opt_sub) {
199     strOperation = "Subtract Images";
200     for (int ix = 0; ix < nx; ix++) {
201       ImageFileColumn in1 = v1[ix];
202       ImageFileColumn in2 = v2[ix];
203       ImageFileColumn out = vout[ix];
204       for (int iy = 0; iy < ny; iy++)
205           *out++ = *in1++ - *in2++;
206     }
207   } else if (opt_mul) {
208     strOperation = "Multiply Images";
209     for (int ix = 0; ix < nx; ix++) {
210       ImageFileColumn in1 = v1[ix];
211       ImageFileColumn in2 = v2[ix];
212       ImageFileColumn out = vout[ix];
213       for (int iy = 0; iy < ny; iy++)
214         *out++ = *in1++ * *in2++;
215     }
216   }
217   if (opt_comp) {
218     double d, r, e;
219     im_in1.comparativeStatistics (im_in2, d, r, e);
220     cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
221   }
222   if (opt_columnPlot > 0) {
223     if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
224       sys_error (ERR_SEVERE, "column-plot > nx");
225       return (1);
226     }
227     double plot_xaxis [nx];
228     for (int i = 0; i < nx; i++)
229       plot_xaxis[i] = i;
230 #if HAVE_SGP
231 #if 0
232 #else
233     ezset  ("clear.");
234     ezset  ("xticks major 5.");
235     ezset  ("xlabel Column");
236     ezset  ("ylabel Pixel");
237     ezset ("curves 2");
238     ezset  ("box.");
239     ezset  ("grid.");
240     ezplot (v1[opt_columnPlot], plot_xaxis, im_in1.ny());
241     ezplot (v2[opt_columnPlot], plot_xaxis, im_in2.ny());
242 #endif
243     char str[256];
244     cout << "Press enter to continue" << flush;
245     fgets(str, sizeof(str), stdin);
246     sgp2_close (sgp2_get_active_win());
247 #endif
248   }
249
250   if (opt_rowPlot > 0) {
251     if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
252       sys_error (ERR_SEVERE, "row_plot > ny");
253       return (1);
254     }
255     double plot_xaxis [ny];
256     double v1Row[nx], v2Row[nx2];
257
258     for (int i = 0; i < ny; i++)
259       plot_xaxis[i] = i;
260     for (int i = 0; i < nx; i++)
261       v1Row[i] = v1[opt_rowPlot][i];
262     for (int i = 0; i < nx2; i++)
263       v2Row[i] = v2[opt_rowPlot][i];
264
265 #if HAVE_SGP
266 #if 0
267 #else
268     ezset  ("clear.");
269     ezset  ("xticks major 5.");
270     ezset  ("xlabel Column");
271     ezset  ("ylabel Pixel");
272     ezset ("curves 2");
273     ezset  ("box.");
274     ezset  ("grid.");
275     ezplot (v1Row, plot_xaxis, im_in1.nx());
276     ezplot (v2Row, plot_xaxis, im_in2.nx());
277 #endif
278     char str[256];
279     cout << "Press enter to continue" << flush;
280     fgets(str, sizeof(str), stdin);
281     sgp2_close (sgp2_get_active_win());
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