70b206d9f4eaecf4d794168891dee9eabb515e22
[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.6 2000/12/16 07:28:25 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: if-2.cpp,v 1.6 2000/12/16 07:28:25 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   }
226   if (opt_columnPlot > 0) {
227     if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
228       sys_error (ERR_SEVERE, "column-plot > nx");
229       return (1);
230     }
231     double plot_xaxis [nx];
232     for (int i = 0; i < nx; i++)
233       plot_xaxis[i] = i;
234 #if HAVE_SGP
235     SGPDriver driver ("Column Plot");
236     SGP sgp (driver);
237     EZPlot ezplot (sgp);
238     ezplot.ezset ("clear.");
239     ezplot.ezset ("xticks major 5.");
240     ezplot.ezset ("xlabel Column");
241     ezplot.ezset ("title Column Plot");
242     ezplot.ezset ("ylabel Pixel");
243     ezplot.ezset ("box.");
244     ezplot.ezset ("grid.");
245     ezplot.addCurve (plot_xaxis, v1[opt_columnPlot], im_in1.ny());
246     ezplot.addCurve (plot_xaxis, v2[opt_columnPlot], im_in2.ny());
247     ezplot.plot();
248     std::cout << "Press enter to continue" << flush;
249     cio_kb_getc();
250 #endif
251   }
252
253   if (opt_rowPlot > 0) {
254     if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
255       sys_error (ERR_SEVERE, "row_plot > ny");
256       return (1);
257     }
258     double plot_xaxis [ny];
259     double v1Row[nx], v2Row[nx2];
260
261     for (int i = 0; i < ny; i++)
262       plot_xaxis[i] = i;
263     for (int i = 0; i < nx; i++)
264       v1Row[i] = v1[i][opt_rowPlot];
265     for (int i = 0; i < nx2; i++)
266       v2Row[i] = v2[i][opt_rowPlot];
267
268 #if HAVE_SGP
269     SGPDriver driver ("Row Plot");
270     SGP sgp (driver);
271     EZPlot ezplot (sgp);
272     ezplot.ezset ("clear.");
273     ezplot.ezset ("xticks major 5.");
274     ezplot.ezset ("title Row Plot");
275     ezplot.ezset ("xlabel Row");
276     ezplot.ezset ("ylabel Pixel");
277     ezplot.ezset ("box.");
278     ezplot.ezset ("grid.");
279     ezplot.addCurve (plot_xaxis, v1Row, im_in1.nx());
280     ezplot.addCurve (plot_xaxis, v2Row, im_in2.nx());
281     ezplot.plot();
282     std::cout << "Press enter to continue" << flush;
283     cio_kb_getc();
284 #endif
285   }
286
287   if (opt_outputFile) {
288     pim_out->labelsCopy (im_in1, "if-2 file 1: ");
289     pim_out->labelsCopy (im_in2, "if-2 file 2: ");
290     pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
291     pim_out->fileWrite(out_file);
292   }
293
294   return (0);
295 }
296
297 #ifndef NO_MAIN
298 int 
299 main (int argc, char *const argv[])
300 {
301   int retval = 1;
302
303   try {
304     retval = if2_main(argc, argv);
305   } catch (exception e) {
306     cerr << "Exception: " << e.what() << std::endl;
307   } catch (...) {
308     cerr << "Unknown exception\n";
309   }
310
311   return (retval);
312 }
313 #endif
314