1 /*****************************************************************************
5 ** Purpose: Manipulate two image files
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: May 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: if-2.cpp,v 1.9 2000/06/28 15:25:34 kevin Exp $
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.
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.
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 ******************************************************************************/
29 * if-2.c Generate a IF file from two input IF files
35 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
37 static struct option my_options[] =
42 {"comp", 0, 0, O_COMP},
43 {"verbose", 0, 0, O_VERBOSE},
44 {"help", 0, 0, O_HELP},
45 {"version", 0, 0, O_VERSION},
50 if2_usage (const char *program)
52 cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]" << endl;
53 cout << "Generate an image file from two input image files files" << 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;
68 if2_main (int argc, char *const argv[])
85 int c = getopt_long (argc, argv, "", my_options, NULL);
108 cout << "Version " << VERSION << endl;
110 cout << "Unknown version number" << endl;
123 if (optind + 3 != argc) {
128 in_file1 = argv[optind];
129 in_file2 = argv[optind + 1];
130 out_file = argv[optind + 2];
132 pim_in1 = new ImageFile ();
133 pim_in2 = new ImageFile ();
134 ImageFile& im_in1 = *pim_in1;
135 ImageFile& im_in2 = *pim_in2;
137 if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
138 sys_error (ERR_WARNING, "Error reading an image");
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());
147 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
148 sys_error (ERR_SEVERE, "Error: Size of image < 0");
152 pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
153 ImageFile& im_out = *pim_out;
156 ImageFileArray v1 = im_in1.getArray();
157 ImageFileArray v2 = im_in2.getArray();
158 ImageFileArray vout = im_out.getArray();
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++;
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++;
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++;
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++;
200 im_in1.comparativeStatistics (im_in2, d, r, e);
201 cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
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());
208 im_out.fileWrite(out_file);
215 main (int argc, char *const argv[])
220 retval = if2_main(argc, argv);
221 } catch (exception e) {
222 cerr << "Exception: " << e.what() << endl;
224 cerr << "Unknown exception" << endl;