Update copyright date; remove old CVS keyword
[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
90   while (1) {
91     char* endptr;
92     int c = getopt_long (argc, argv, "", my_options, NULL);
93
94     if (c == -1)
95       break;
96
97     switch (c) {
98     case O_ADD:
99       opt_add = 1;
100       opt_bImageOutputFile = true;
101       break;
102     case O_SUB :
103       opt_sub = 1;
104       opt_bImageOutputFile = true;
105       break;
106     case O_MUL:
107       opt_mul = 1;
108       opt_bImageOutputFile = true;
109       break;
110     case O_DIVIDE:
111       opt_divide = true;
112       opt_bImageOutputFile = true;
113       break;
114     case O_ROW_PLOT:
115       opt_rowPlot = strtol(optarg, &endptr, 10);
116       if (endptr != optarg + strlen(optarg)) {
117         if2_usage(argv[0]);
118       }
119       opt_bPlotOutputFile = true;
120       break;
121     case O_COLUMN_PLOT:
122       opt_columnPlot = strtol(optarg, &endptr, 10);
123       if (endptr != optarg + strlen(optarg)) {
124         if2_usage(argv[0]);
125       }
126       opt_bPlotOutputFile = true;
127       break;
128     case O_COMP:
129       opt_comp = 1;
130       break;
131     case O_VERBOSE:
132       opt_verbose = 1;
133       break;
134     case O_VERSION:
135 #ifdef VERSION
136       std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
137 #else
138       std::cout << "Unknown version number\n";
139 #endif
140       return (0);
141     case O_HELP:
142     case '?':
143       if2_usage(argv[0]);
144       return (0);
145     default:
146       if2_usage(argv[0]);
147       return (1);
148     }
149   }
150
151   if ((opt_bImageOutputFile || opt_bPlotOutputFile) && (optind + 3 != argc)) {
152     if2_usage(argv[0]);
153     return (1);
154   }
155   else if (! (opt_bImageOutputFile || opt_bPlotOutputFile) && optind + 2 != argc) {
156     if2_usage(argv[0]);
157     return (1);
158   }
159
160   in_file1 = argv[optind];
161   in_file2 = argv[optind + 1];
162   if (opt_bImageOutputFile || opt_bPlotOutputFile)
163     strOutFile = argv[optind + 2];
164
165   pim_in1 = new ImageFile ();
166   pim_in2 = new ImageFile ();
167   ImageFile& im_in1 = *pim_in1;
168   ImageFile& im_in2 = *pim_in2;
169
170   if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
171     sys_error (ERR_WARNING, "Error reading an image");
172     return (1);
173   }
174
175   if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
176     sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
177       im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
178     return(1);
179   }
180   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
181     sys_error (ERR_SEVERE, "Error: Size of image < 0");
182     return(1);
183   }
184
185   ImageFileArray v1 = im_in1.getArray();
186   ImageFileArray v2 = im_in2.getArray();
187   ImageFileArray vout = NULL;
188
189   if (opt_bImageOutputFile && opt_bPlotOutputFile) {
190     sys_error (ERR_SEVERE, "Both Image and Plot output files can not be selected simultaneously");
191     return (1);
192   }
193   if (opt_bImageOutputFile) {
194     pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
195     vout = pim_out->getArray();
196   }
197
198   std::string strOperation;
199   int nx = im_in1.nx();
200   int ny = im_in1.ny();
201   int nx2 = im_in2.nx();
202   int ny2 = im_in2.ny();
203
204   if (opt_add) {
205     strOperation = "Add Images";
206     im_in1.addImages (im_in2, *pim_out);
207   } else if (opt_sub) {
208     strOperation = "Subtract Images";
209     im_in1.subtractImages (im_in2, *pim_out);
210   } else if (opt_mul) {
211     strOperation = "Multiply Images";
212     im_in1.multiplyImages (im_in2, *pim_out);
213   } else if (opt_divide) {
214     strOperation = "Divide Images";
215     im_in1.divideImages (im_in2, *pim_out);
216   }
217   if (opt_comp) {
218     double d, r, e;
219     im_in1.comparativeStatistics (im_in2, d, r, e);
220     std::cout << "d=" << d << ", r=" << r << ", e=" << e << std::endl;
221   }
222
223   int i;
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 = new double [nx];
230     for (i = 0; i < nx; i++)
231       plot_xaxis[i] = i;
232
233     PlotFile plotFile (3, nx);
234
235     plotFile.addColumn (0, plot_xaxis);
236     plotFile.addColumn (1, v1[opt_columnPlot]);
237     plotFile.addColumn (2, v2[opt_columnPlot]);
238     std::ostringstream os;
239     os << "Column " << opt_columnPlot << " Comparison";
240     plotFile.addDescription (os.str().c_str());
241     std::string title("title ");
242     title += os.str();
243     plotFile.addEzsetCommand (title.c_str());
244     plotFile.addEzsetCommand ("xlabel Column");
245     plotFile.addEzsetCommand ("ylabel Pixel Value");
246     plotFile.addEzsetCommand ("box");
247     plotFile.addEzsetCommand ("grid");
248     plotFile.addEzsetCommand ("xticks major 5");
249
250     plotFile.fileWrite (strOutFile.c_str());
251
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 [ny];
262     double* v2Row = new double [ny2];
263
264     for (i = 0; i < ny; i++)
265       plot_xaxis[i] = i;
266     for (i = 0; i < ny; i++)
267       v1Row[i] = v1[i][opt_rowPlot];
268     for (i = 0; i < ny2; i++)
269       v2Row[i] = v2[i][opt_rowPlot];
270
271     PlotFile plotFile (3, ny);
272
273     plotFile.addColumn (0, plot_xaxis);
274     plotFile.addColumn (1, v1Row);
275     plotFile.addColumn (2, v2Row);
276     std::ostringstream os;
277     os << "Row " << opt_rowPlot << " Comparison";
278     plotFile.addDescription (os.str().c_str());
279     std::string title("title ");
280     title += os.str();
281     plotFile.addEzsetCommand (title.c_str());
282     plotFile.addEzsetCommand ("xlabel Row");
283     plotFile.addEzsetCommand ("ylabel Pixel Value");
284     plotFile.addEzsetCommand ("box");
285     plotFile.addEzsetCommand ("grid");
286     plotFile.addEzsetCommand ("xticks major 5");
287
288     plotFile.fileWrite (strOutFile.c_str());
289
290     delete plot_xaxis;
291     delete v1Row;
292     delete v2Row;
293   }
294
295   if (opt_bImageOutputFile) {
296     pim_out->labelsCopy (im_in1, "if2 file 1: ");
297     pim_out->labelsCopy (im_in2, "if2 file 2: ");
298     pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
299     pim_out->fileWrite (strOutFile);
300   }
301
302   return (0);
303 }
304
305 #ifndef NO_MAIN
306 int
307 main (int argc, char *const argv[])
308 {
309   int retval = 1;
310
311   try {
312     retval = if2_main(argc, argv);
313   } catch (exception e) {
314     std::cerr << "Exception: " << e.what() << std::endl;
315   } catch (...) {
316     std::cerr << "Unknown exception\n";
317   }
318
319   return (retval);
320 }
321 #endif
322