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