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.3 2000/07/28 10:51:31 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 ******************************************************************************/
32 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_ROW_PLOT, O_COLUMN_PLOT, O_VERBOSE, O_HELP, O_VERSION};
34 static struct option my_options[] =
39 {"comp", 0, 0, O_COMP},
40 {"column-plot", 1, 0, O_COLUMN_PLOT},
41 {"row-plot", 1, 0, O_ROW_PLOT},
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 << "Perform functions on two input image 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 << " --column-plot n Plot column\n";
62 cout << " --row-plot n Plot row\n";
63 cout << " --verbose Verbose modem" << endl;
64 cout << " --version Print version" << endl;
65 cout << " --help Print this help message" << endl;
69 if2_main (int argc, char *const argv[])
73 ImageFile* pim_out = NULL;
82 bool opt_outputFile = false;
84 int opt_columnPlot = -1;
89 int c = getopt_long (argc, argv, "", my_options, NULL);
97 opt_outputFile = true;
101 opt_outputFile = true;
105 opt_outputFile = true;
108 opt_rowPlot = strtol(optarg, &endptr, 10);
109 if (endptr != optarg + strlen(optarg)) {
114 opt_columnPlot = strtol(optarg, &endptr, 10);
115 if (endptr != optarg + strlen(optarg)) {
127 cout << "Version " << VERSION << endl;
129 cout << "Unknown version number" << endl;
142 if (opt_outputFile && (optind + 3 != argc)) {
146 else if (! opt_outputFile && optind + 2 != argc) {
151 in_file1 = argv[optind];
152 in_file2 = argv[optind + 1];
154 out_file = argv[optind + 2];
156 pim_in1 = new ImageFile ();
157 pim_in2 = new ImageFile ();
158 ImageFile& im_in1 = *pim_in1;
159 ImageFile& im_in2 = *pim_in2;
161 if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
162 sys_error (ERR_WARNING, "Error reading an image");
166 if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
167 sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
168 im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
171 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
172 sys_error (ERR_SEVERE, "Error: Size of image < 0");
176 ImageFileArray v1 = im_in1.getArray();
177 ImageFileArray v2 = im_in2.getArray();
178 ImageFileArray vout = NULL;
180 if (opt_outputFile) {
181 pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
182 vout = pim_out->getArray();
186 int nx = im_in1.nx();
187 int ny = im_in1.ny();
188 int nx2 = im_in2.nx();
189 int ny2 = im_in2.ny();
192 strOperation = "Add Images";
193 for (int ix = 0; ix < nx; ix++) {
194 ImageFileColumn in1 = v1[ix];
195 ImageFileColumn in2 = v2[ix];
196 ImageFileColumn out = vout[ix];
197 for (int iy = 0; iy < ny; iy++)
198 *out++ = *in1++ + *in2++;
200 } else if (opt_sub) {
201 strOperation = "Subtract Images";
202 for (int ix = 0; ix < nx; ix++) {
203 ImageFileColumn in1 = v1[ix];
204 ImageFileColumn in2 = v2[ix];
205 ImageFileColumn out = vout[ix];
206 for (int iy = 0; iy < ny; iy++)
207 *out++ = *in1++ - *in2++;
209 } else if (opt_mul) {
210 strOperation = "Multiply Images";
211 for (int ix = 0; ix < nx; ix++) {
212 ImageFileColumn in1 = v1[ix];
213 ImageFileColumn in2 = v2[ix];
214 ImageFileColumn out = vout[ix];
215 for (int iy = 0; iy < ny; iy++)
216 *out++ = *in1++ * *in2++;
221 im_in1.comparativeStatistics (im_in2, d, r, e);
222 cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
224 if (opt_columnPlot > 0) {
225 if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
226 sys_error (ERR_SEVERE, "column-plot > nx");
229 double plot_xaxis [nx];
230 for (int i = 0; i < nx; i++)
233 SGPDriver driver ("Column Plot");
236 ezplot.ezset ("clear.");
237 ezplot.ezset ("xticks major 5.");
238 ezplot.ezset ("xlabel Column");
239 ezplot.ezset ("title Column Plot");
240 ezplot.ezset ("ylabel Pixel");
241 ezplot.ezset ("box.");
242 ezplot.ezset ("grid.");
243 ezplot.addCurve (plot_xaxis, v1[opt_columnPlot], im_in1.ny());
244 ezplot.addCurve (plot_xaxis, v2[opt_columnPlot], im_in2.ny());
246 cout << "Press enter to continue" << flush;
251 if (opt_rowPlot > 0) {
252 if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
253 sys_error (ERR_SEVERE, "row_plot > ny");
256 double plot_xaxis [ny];
257 double v1Row[nx], v2Row[nx2];
259 for (int i = 0; i < ny; i++)
261 for (int i = 0; i < nx; i++)
262 v1Row[i] = v1[i][opt_rowPlot];
263 for (int i = 0; i < nx2; i++)
264 v2Row[i] = v2[i][opt_rowPlot];
267 SGPDriver driver ("Row Plot");
270 ezplot.ezset ("clear.");
271 ezplot.ezset ("xticks major 5.");
272 ezplot.ezset ("title Row Plot");
273 ezplot.ezset ("xlabel Row");
274 ezplot.ezset ("ylabel Pixel");
275 ezplot.ezset ("box.");
276 ezplot.ezset ("grid.");
277 ezplot.addCurve (plot_xaxis, v1Row, im_in1.nx());
278 ezplot.addCurve (plot_xaxis, v2Row, im_in2.nx());
280 cout << "Press enter to continue" << flush;
285 if (opt_outputFile) {
286 pim_out->labelsCopy (im_in1, "if-2 file 1: ");
287 pim_out->labelsCopy (im_in2, "if-2 file 2: ");
288 pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
289 pim_out->fileWrite(out_file);
297 main (int argc, char *const argv[])
302 retval = if2_main(argc, argv);
303 } catch (exception e) {
304 cerr << "Exception: " << e.what() << endl;
306 cerr << "Unknown exception" << endl;