r123: *** 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.9 2000/06/28 15:25:34 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_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   {"verbose", 0, 0, O_VERBOSE},
44   {"help", 0, 0, O_HELP},
45   {"version", 0, 0, O_VERSION},
46   {0, 0, 0, 0}
47 };
48
49 void 
50 if2_usage (const char *program)
51 {
52   cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]" << endl;
53   cout << "Generate an image file from two input image files files" << endl;
54   cout << endl;
55   cout << "     infile1    Name of first input IF file" << endl;
56   cout << "     infile2    Name of second input IF file" << endl;
57   cout << "     outfile    Name of output IF file" << endl;
58   cout << "     --add      Add images" << endl;
59   cout << "     --sub      Subtract image 2 from image 1" << endl;
60   cout << "     --mul      Multiply images" << endl;
61   cout << "     --comp     Compare images" << endl;
62   cout << "     --verbose  Verbose modem" << endl;
63   cout << "     --version  Print version" << endl;
64   cout << "     --help     Print this help message" << endl;
65 }
66
67 int 
68 if2_main (int argc, char *const argv[])
69 {
70   ImageFile* pim_in1;
71   ImageFile* pim_in2;
72   ImageFile* pim_out;
73   char *in_file1;
74   char *in_file2;
75   char *out_file;
76   int opt_verbose = 0;
77   int opt_add = 0;
78   int opt_sub = 0;
79   int opt_mul = 0;
80   int opt_comp = 0;
81
82   Timer timerProgram;
83
84   while (1) {
85     int c = getopt_long (argc, argv, "", my_options, NULL);
86       
87     if (c == -1)
88       break;
89       
90     switch (c) {
91     case O_ADD:
92       opt_add = 1;
93       break;
94     case O_SUB :
95       opt_sub = 1;
96       break;
97     case O_MUL:
98       opt_mul = 1;
99       break;
100     case O_COMP:
101       opt_comp = 1;
102       break;
103     case O_VERBOSE:
104       opt_verbose = 1;
105       break;
106     case O_VERSION:
107 #ifdef VERSION
108       cout << "Version " << VERSION << endl;
109 #else
110       cout << "Unknown version number" << endl;
111 #endif
112       return (0);
113     case O_HELP:
114     case '?':
115       if2_usage(argv[0]);
116       return (0);
117     default:
118       if2_usage(argv[0]);
119       return (1);
120     }
121   }
122   
123   if (optind + 3 != argc) {
124     if2_usage(argv[0]);
125     return (1);
126   }
127   
128   in_file1 = argv[optind];
129   in_file2 = argv[optind + 1];
130   out_file = argv[optind + 2];
131
132   pim_in1 = new ImageFile ();
133   pim_in2 = new ImageFile ();
134   ImageFile& im_in1 = *pim_in1;
135   ImageFile& im_in2 = *pim_in2;
136
137   if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
138       sys_error (ERR_WARNING, "Error reading an image");
139       return (1);
140   }
141
142   if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
143     sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
144             im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
145     return(1);
146   }
147   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
148       sys_error (ERR_SEVERE, "Error: Size of image < 0");
149       return(1);
150   }
151
152   pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
153   ImageFile& im_out = *pim_out;
154
155   string strOperation;
156   ImageFileArray v1 = im_in1.getArray();
157   ImageFileArray v2 = im_in2.getArray();
158   ImageFileArray vout = im_out.getArray();
159
160   if (opt_add) {
161     strOperation = "Add Images";
162     for (int ix = 0; ix < im_in1.nx(); ix++) {
163       ImageFileColumn in1 = v1[ix];
164       ImageFileColumn in2 = v2[ix];
165       ImageFileColumn out = vout[ix];
166       for (int iy = 0; iy < im_in1.ny(); iy++)
167         *out++ = *in1++ + *in2++;
168     }
169   } else if (opt_sub) {
170     strOperation = "Subtract Images";
171     for (int ix = 0; ix < im_in1.nx(); ix++) {
172       ImageFileColumn in1 = v1[ix];
173       ImageFileColumn in2 = v2[ix];
174       ImageFileColumn out = vout[ix];
175       for (int iy = 0; iy < im_in1.ny(); iy++)
176           *out++ = *in1++ - *in2++;
177     }
178   } else if (opt_mul) {
179     strOperation = "Multiply Images";
180     for (int ix = 0; ix < im_in1.nx(); ix++) {
181       ImageFileColumn in1 = v1[ix];
182       ImageFileColumn in2 = v2[ix];
183       ImageFileColumn out = vout[ix];
184       for (int iy = 0; iy < im_in1.ny(); iy++)
185         *out++ = *in1++ * *in2++;
186     }
187   } else if (opt_comp) {
188     double abs_error = 0.;
189     strOperation = "Subtract Images";
190     for (int ix = 0; ix < im_in1.nx(); ix++) {
191       ImageFileColumn in1 = v1[ix];
192       ImageFileColumn in2 = v2[ix];
193       ImageFileColumn out = vout[ix];
194       for (int iy = 0; iy < im_in1.ny(); iy++) {
195         double diff = *in1++ - *in2++;
196         *out++ = diff;
197       }
198     }
199     double d, r, e;
200     im_in1.comparativeStatistics (im_in2, d, r, e);
201     cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
202   }
203
204   im_out.labelsCopy (im_in1, "if-2 file 1: ");
205   im_out.labelsCopy (im_in2, "if-2 file 2: ");
206   im_out.labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
207
208   im_out.fileWrite(out_file);
209
210   return (0);
211 }
212
213 #ifndef NO_MAIN
214 int 
215 main (int argc, char *const argv[])
216 {
217   int retval = 1;
218
219   try {
220     retval = if2_main(argc, argv);
221   } catch (exception e) {
222     cerr << "Exception: " << e.what() << endl;
223   } catch (...) {
224     cerr << "Unknown exception" << endl;
225   }
226
227   return (retval);
228 }
229 #endif
230