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