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.2 2000/07/28 08:28:08 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 int opt_outputFile = 0;
84 int opt_columnPlot = -1;
89 int c = getopt_long (argc, argv, "", my_options, NULL);
108 opt_rowPlot = strtol(optarg, &endptr, 10);
109 if (endptr != optarg + strlen(optarg)) {
113 opt_columnPlot = strtol(optarg, &endptr, 10);
114 if (endptr != optarg + strlen(optarg)) {
125 cout << "Version " << VERSION << endl;
127 cout << "Unknown version number" << endl;
140 if (opt_outputFile && (optind + 3 != argc)) {
144 else if (! opt_outputFile && optind + 2 != argc) {
149 in_file1 = argv[optind];
150 in_file2 = argv[optind + 1];
152 out_file = argv[optind + 2];
154 pim_in1 = new ImageFile ();
155 pim_in2 = new ImageFile ();
156 ImageFile& im_in1 = *pim_in1;
157 ImageFile& im_in2 = *pim_in2;
159 if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
160 sys_error (ERR_WARNING, "Error reading an image");
164 if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
165 sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
166 im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
169 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
170 sys_error (ERR_SEVERE, "Error: Size of image < 0");
174 ImageFileArray v1 = im_in1.getArray();
175 ImageFileArray v2 = im_in2.getArray();
176 ImageFileArray vout = NULL;
178 if (opt_outputFile) {
179 pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
180 vout = pim_out->getArray();
184 int nx = im_in1.nx();
185 int ny = im_in1.ny();
186 int nx2 = im_in2.nx();
187 int ny2 = im_in2.ny();
190 strOperation = "Add Images";
191 for (int ix = 0; ix < nx; ix++) {
192 ImageFileColumn in1 = v1[ix];
193 ImageFileColumn in2 = v2[ix];
194 ImageFileColumn out = vout[ix];
195 for (int iy = 0; iy < ny; iy++)
196 *out++ = *in1++ + *in2++;
198 } else if (opt_sub) {
199 strOperation = "Subtract Images";
200 for (int ix = 0; ix < nx; ix++) {
201 ImageFileColumn in1 = v1[ix];
202 ImageFileColumn in2 = v2[ix];
203 ImageFileColumn out = vout[ix];
204 for (int iy = 0; iy < ny; iy++)
205 *out++ = *in1++ - *in2++;
207 } else if (opt_mul) {
208 strOperation = "Multiply Images";
209 for (int ix = 0; ix < nx; ix++) {
210 ImageFileColumn in1 = v1[ix];
211 ImageFileColumn in2 = v2[ix];
212 ImageFileColumn out = vout[ix];
213 for (int iy = 0; iy < ny; iy++)
214 *out++ = *in1++ * *in2++;
219 im_in1.comparativeStatistics (im_in2, d, r, e);
220 cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
222 if (opt_columnPlot > 0) {
223 if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
224 sys_error (ERR_SEVERE, "column-plot > nx");
227 double plot_xaxis [nx];
228 for (int i = 0; i < nx; i++)
231 SGPDriver driver ("Column Plot");
234 ezplot.ezset ("clear.");
235 ezplot.ezset ("xticks major 5.");
236 ezplot.ezset ("xlabel Column");
237 ezplot.ezset ("ylabel Pixel");
238 ezplot.ezset ("curves 2");
239 ezplot.ezset ("box.");
240 ezplot.ezset ("grid.");
241 ezplot.addCurve (v1[opt_columnPlot], plot_xaxis, im_in1.ny());
242 ezplot.addCurve (v2[opt_columnPlot], plot_xaxis, im_in2.ny());
243 cout << "Press enter to continue" << flush;
248 if (opt_rowPlot > 0) {
249 if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
250 sys_error (ERR_SEVERE, "row_plot > ny");
253 double plot_xaxis [ny];
254 double v1Row[nx], v2Row[nx2];
256 for (int i = 0; i < ny; i++)
258 for (int i = 0; i < nx; i++)
259 v1Row[i] = v1[opt_rowPlot][i];
260 for (int i = 0; i < nx2; i++)
261 v2Row[i] = v2[opt_rowPlot][i];
264 SGPDriver driver ("Column Plot");
267 ezplot.ezset ("clear.");
268 ezplot.ezset ("xticks major 5.");
269 ezplot.ezset ("xlabel Column");
270 ezplot.ezset ("ylabel Pixel");
271 ezplot.ezset ("curves 2");
272 ezplot.ezset ("box.");
273 ezplot.ezset ("grid.");
274 ezplot.addCurve (v1Row, plot_xaxis, im_in1.nx());
275 ezplot.addCurve (v2Row, plot_xaxis, im_in2.nx());
276 cout << "Press enter to continue" << flush;
281 if (opt_outputFile) {
282 pim_out->labelsCopy (im_in1, "if-2 file 1: ");
283 pim_out->labelsCopy (im_in2, "if-2 file 2: ");
284 pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
285 pim_out->fileWrite(out_file);
293 main (int argc, char *const argv[])
298 retval = if2_main(argc, argv);
299 } catch (exception e) {
300 cerr << "Exception: " << e.what() << endl;
302 cerr << "Unknown exception" << endl;