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.10 2000/07/09 08:16:18 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_ROW_PLOT, O_COLUMN_PLOT, O_VERBOSE, O_HELP, O_VERSION};
37 static struct option my_options[] =
42 {"comp", 0, 0, O_COMP},
43 {"column-plot", 1, 0, O_COLUMN_PLOT},
44 {"row-plot", 1, 0, O_ROW_PLOT},
45 {"verbose", 0, 0, O_VERBOSE},
46 {"help", 0, 0, O_HELP},
47 {"version", 0, 0, O_VERSION},
52 if2_usage (const char *program)
54 cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]" << endl;
55 cout << "Perform functions on two input image files" << endl;
57 cout << " infile1 Name of first input IF file" << endl;
58 cout << " infile2 Name of second input IF file" << endl;
59 cout << " outfile Name of output IF file" << endl;
60 cout << " --add Add images" << endl;
61 cout << " --sub Subtract image 2 from image 1" << endl;
62 cout << " --mul Multiply images" << endl;
63 cout << " --comp Compare images" << endl;
64 cout << " --column-plot n Plot column\n";
65 cout << " --row-plot n Plot row\n";
66 cout << " --verbose Verbose modem" << endl;
67 cout << " --version Print version" << endl;
68 cout << " --help Print this help message" << endl;
72 if2_main (int argc, char *const argv[])
85 int opt_outputFile = 0;
87 int opt_columnPlot = -1;
92 int c = getopt_long (argc, argv, "", my_options, NULL);
111 opt_rowPlot = strtol(optarg, &endptr, 10);
112 if (endptr != optarg + strlen(optarg)) {
116 opt_columnPlot = strtol(optarg, &endptr, 10);
117 if (endptr != optarg + strlen(optarg)) {
128 cout << "Version " << VERSION << endl;
130 cout << "Unknown version number" << endl;
143 if (opt_outputFile && (optind + 3 != argc)) {
147 else if (! opt_outputFile && optind + 2 != argc) {
152 in_file1 = argv[optind];
153 in_file2 = argv[optind + 1];
155 out_file = argv[optind + 2];
157 pim_in1 = new ImageFile ();
158 pim_in2 = new ImageFile ();
159 ImageFile& im_in1 = *pim_in1;
160 ImageFile& im_in2 = *pim_in2;
162 if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
163 sys_error (ERR_WARNING, "Error reading an image");
167 if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
168 sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
169 im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
172 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
173 sys_error (ERR_SEVERE, "Error: Size of image < 0");
177 ImageFileArray v1 = im_in1.getArray();
178 ImageFileArray v2 = im_in2.getArray();
179 ImageFileArray vout = NULL;
181 if (opt_outputFile) {
182 pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
183 vout = pim_out->getArray();
188 strOperation = "Add Images";
189 for (int ix = 0; ix < im_in1.nx(); ix++) {
190 ImageFileColumn in1 = v1[ix];
191 ImageFileColumn in2 = v2[ix];
192 ImageFileColumn out = vout[ix];
193 for (int iy = 0; iy < im_in1.ny(); iy++)
194 *out++ = *in1++ + *in2++;
196 } else if (opt_sub) {
197 strOperation = "Subtract Images";
198 for (int ix = 0; ix < im_in1.nx(); ix++) {
199 ImageFileColumn in1 = v1[ix];
200 ImageFileColumn in2 = v2[ix];
201 ImageFileColumn out = vout[ix];
202 for (int iy = 0; iy < im_in1.ny(); iy++)
203 *out++ = *in1++ - *in2++;
205 } else if (opt_mul) {
206 strOperation = "Multiply Images";
207 for (int ix = 0; ix < im_in1.nx(); ix++) {
208 ImageFileColumn in1 = v1[ix];
209 ImageFileColumn in2 = v2[ix];
210 ImageFileColumn out = vout[ix];
211 for (int iy = 0; iy < im_in1.ny(); iy++)
212 *out++ = *in1++ * *in2++;
217 im_in1.comparativeStatistics (im_in2, d, r, e);
218 cout << "d=" << d << ", r=" << r << ", e=" << e << endl;
220 if (opt_columnPlot > 0) {
221 if (opt_columnPlot >= im_in1.nx() || opt_columnPlot >= im_in2.nx()) {
222 sys_error (ERR_SEVERE, "column-plot > nx");
225 double plot_xaxis [im_in1.nx()];
226 for (int i = 0; i < im_in1.nx(); i++)
232 ezset ("xticks major 5.");
233 ezset ("xlabel Column");
234 ezset ("ylabel Pixel");
238 ezplot (v1[opt_columnPlot], plot_xaxis, im_in1.ny());
239 ezplot (v2[opt_columnPlot], plot_xaxis, im_in2.ny());
242 cout << "Press enter to continue" << flush;
243 fgets(str, sizeof(str), stdin);
244 sgp2_close (sgp2_get_active_win());
248 if (opt_rowPlot > 0) {
249 if (opt_rowPlot >= im_in1.ny() || opt_rowPlot >= im_in2.ny()) {
250 sys_error (ERR_SEVERE, "row_plot > ny");
253 double plot_xaxis [im_in1.ny()];
254 double v1Row[im_in1.nx()], v2Row[im_in2.nx()];
256 for (int i = 0; i < im_in1.ny(); i++)
258 for (int i = 0; i < im_in1.nx(); i++)
259 v1Row[i] = v1[opt_rowPlot][i];
260 for (int i = 0; i < im_in2.nx(); i++)
261 v2Row[i] = v2[opt_rowPlot][i];
267 ezset ("xticks major 5.");
268 ezset ("xlabel Column");
269 ezset ("ylabel Pixel");
273 ezplot (v1Row, plot_xaxis, im_in1.nx());
274 ezplot (v2Row, plot_xaxis, im_in2.nx());
277 cout << "Press enter to continue" << flush;
278 fgets(str, sizeof(str), stdin);
279 sgp2_close (sgp2_get_active_win());
283 if (opt_outputFile) {
284 pim_out->labelsCopy (im_in1, "if-2 file 1: ");
285 pim_out->labelsCopy (im_in2, "if-2 file 2: ");
286 pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
287 pim_out->fileWrite(out_file);
295 main (int argc, char *const argv[])
300 retval = if2_main(argc, argv);
301 } catch (exception e) {
302 cerr << "Exception: " << e.what() << endl;
304 cerr << "Unknown exception" << endl;