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.8 2000/06/26 21:15:24 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
34 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
36 static struct option my_options[] =
41 {"comp", 0, 0, O_COMP},
42 {"verbose", 0, 0, O_VERBOSE},
43 {"help", 0, 0, O_HELP},
44 {"version", 0, 0, O_VERSION},
49 if2_usage (const char *program)
51 cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]" << endl;
52 cout << "Generate an image file from two input image files files" << 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;
67 if2_main (int argc, char *const argv[])
82 int c = getopt_long (argc, argv, "", my_options, NULL);
105 cout << "Version " << VERSION << endl;
107 cout << "Unknown version number" << endl;
120 if (optind + 3 != argc) {
125 in_file1 = argv[optind];
126 in_file2 = argv[optind + 1];
127 out_file = argv[optind + 2];
129 pim_in1 = new ImageFile ();
130 pim_in2 = new ImageFile ();
131 ImageFile& im_in1 = *pim_in1;
132 ImageFile& im_in2 = *pim_in2;
134 if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
135 sys_error (ERR_WARNING, "Error reading an image");
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());
144 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
145 sys_error (ERR_SEVERE, "Error: Size of image < 0");
149 pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
150 ImageFile& im_out = *pim_out;
153 ImageFileArray v1 = im_in1.getArray();
154 ImageFileArray v2 = im_in2.getArray();
155 ImageFileArray vout = im_out.getArray();
158 strOperation = "Add Images";
159 for (int ix = 0; ix < im_in1.nx(); ix++) {
160 ImageFileColumn in1 = v1[ix];
161 ImageFileColumn in2 = v2[ix];
162 ImageFileColumn out = vout[ix];
163 for (int iy = 0; iy < im_in1.ny(); iy++)
164 *out++ = *in1++ + *in2++;
166 } else if (opt_sub) {
167 strOperation = "Subtract Images";
168 for (int ix = 0; ix < im_in1.nx(); ix++) {
169 ImageFileColumn in1 = v1[ix];
170 ImageFileColumn in2 = v2[ix];
171 ImageFileColumn out = vout[ix];
172 for (int iy = 0; iy < im_in1.ny(); iy++)
173 *out++ = *in1++ - *in2++;
175 } else if (opt_mul) {
176 strOperation = "Multiply Images";
177 for (int ix = 0; ix < im_in1.nx(); ix++) {
178 ImageFileColumn in1 = v1[ix];
179 ImageFileColumn in2 = v2[ix];
180 ImageFileColumn out = vout[ix];
181 for (int iy = 0; iy < im_in1.ny(); iy++)
182 *out++ = *in1++ * *in2++;
184 } else if (opt_comp) {
185 double abs_error = 0.;
186 strOperation = "Subtract Images";
187 for (int ix = 0; ix < im_in1.nx(); ix++) {
188 ImageFileColumn in1 = v1[ix];
189 ImageFileColumn in2 = v2[ix];
190 ImageFileColumn out = vout[ix];
191 for (int iy = 0; iy < im_in1.ny(); iy++) {
192 double diff = *in1++ - *in2++;
197 im_in1.comparativeStatistics (im_in2, d, r, e);
198 cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
201 im_out.labelsCopy (im_in1, "if-2 file 1: ");
202 im_out.labelsCopy (im_in2, "if-2 file 2: ");
203 im_out.labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str());
205 im_out.fileWrite(out_file);
212 main (int argc, char *const argv[])
217 retval = if2_main(argc, argv);
218 } catch (exception e) {
219 cerr << "Exception: " << e.what() << endl;
221 cerr << "Unknown exception" << endl;