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