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.6 2000/12/16 07:28:25 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},
48 static const char* g_szIdStr = "$Id: if-2.cpp,v 1.6 2000/12/16 07:28:25 kevin Exp $";
51 if2_usage (const char *program)
53 std::cout << "usage: " << fileBasename(program) << " infile1 infile2 outfile [OPTIONS]\n";
54 std::cout << "Perform functions on two input image files\n";
55 std::cout << std::endl;
56 std::cout << " infile1 Name of first input IF file\n";
57 std::cout << " infile2 Name of second input IF file\n";
58 std::cout << " outfile Name of output IF file\n";
59 std::cout << " --add Add images\n";
60 std::cout << " --sub Subtract image 2 from image 1\n";
61 std::cout << " --mul Multiply images\n";
62 std::cout << " --comp Compare images\n";
63 std::cout << " --column-plot n Plot column\n";
64 std::cout << " --row-plot n Plot row\n";
65 std::cout << " --verbose Verbose modem\n";
66 std::cout << " --version Print version\n";
67 std::cout << " --help Print this help message\n";
71 if2_main (int argc, char *const argv[])
75 ImageFile* pim_out = NULL;
84 bool opt_outputFile = false;
86 int opt_columnPlot = -1;
91 int c = getopt_long (argc, argv, "", my_options, NULL);
99 opt_outputFile = true;
103 opt_outputFile = true;
107 opt_outputFile = true;
110 opt_rowPlot = strtol(optarg, &endptr, 10);
111 if (endptr != optarg + strlen(optarg)) {
116 opt_columnPlot = strtol(optarg, &endptr, 10);
117 if (endptr != optarg + strlen(optarg)) {
129 std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
131 std::cout << "Unknown version number\n";
144 if (opt_outputFile && (optind + 3 != argc)) {
148 else if (! opt_outputFile && optind + 2 != argc) {
153 in_file1 = argv[optind];
154 in_file2 = argv[optind + 1];
156 out_file = argv[optind + 2];
158 pim_in1 = new ImageFile ();
159 pim_in2 = new ImageFile ();
160 ImageFile& im_in1 = *pim_in1;
161 ImageFile& im_in2 = *pim_in2;
163 if (! im_in1.fileRead(in_file1) || ! im_in2.fileRead(in_file2)) {
164 sys_error (ERR_WARNING, "Error reading an image");
168 if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
169 sys_error (ERR_SEVERE, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match",
170 im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
173 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
174 sys_error (ERR_SEVERE, "Error: Size of image < 0");
178 ImageFileArray v1 = im_in1.getArray();
179 ImageFileArray v2 = im_in2.getArray();
180 ImageFileArray vout = NULL;
182 if (opt_outputFile) {
183 pim_out = new ImageFile (im_in1.nx(), im_in1.ny());
184 vout = pim_out->getArray();
187 std::string strOperation;
188 int nx = im_in1.nx();
189 int ny = im_in1.ny();
190 int nx2 = im_in2.nx();
191 int ny2 = im_in2.ny();
194 strOperation = "Add Images";
195 for (int ix = 0; ix < nx; ix++) {
196 ImageFileColumn in1 = v1[ix];
197 ImageFileColumn in2 = v2[ix];
198 ImageFileColumn out = vout[ix];
199 for (int iy = 0; iy < ny; iy++)
200 *out++ = *in1++ + *in2++;
202 } else if (opt_sub) {
203 strOperation = "Subtract Images";
204 for (int ix = 0; ix < nx; ix++) {
205 ImageFileColumn in1 = v1[ix];
206 ImageFileColumn in2 = v2[ix];
207 ImageFileColumn out = vout[ix];
208 for (int iy = 0; iy < ny; iy++)
209 *out++ = *in1++ - *in2++;
211 } else if (opt_mul) {
212 strOperation = "Multiply Images";
213 for (int ix = 0; ix < nx; ix++) {
214 ImageFileColumn in1 = v1[ix];
215 ImageFileColumn in2 = v2[ix];
216 ImageFileColumn out = vout[ix];
217 for (int iy = 0; iy < ny; iy++)
218 *out++ = *in1++ * *in2++;
223 im_in1.comparativeStatistics (im_in2, d, r, e);
224 std::cout << "d=" << d << ", r=" << r << ", e=" << e << std::endl;
226 if (opt_columnPlot > 0) {
227 if (opt_columnPlot >= nx || opt_columnPlot >= nx2) {
228 sys_error (ERR_SEVERE, "column-plot > nx");
231 double plot_xaxis [nx];
232 for (int i = 0; i < nx; i++)
235 SGPDriver driver ("Column Plot");
238 ezplot.ezset ("clear.");
239 ezplot.ezset ("xticks major 5.");
240 ezplot.ezset ("xlabel Column");
241 ezplot.ezset ("title Column Plot");
242 ezplot.ezset ("ylabel Pixel");
243 ezplot.ezset ("box.");
244 ezplot.ezset ("grid.");
245 ezplot.addCurve (plot_xaxis, v1[opt_columnPlot], im_in1.ny());
246 ezplot.addCurve (plot_xaxis, v2[opt_columnPlot], im_in2.ny());
248 std::cout << "Press enter to continue" << flush;
253 if (opt_rowPlot > 0) {
254 if (opt_rowPlot >= ny || opt_rowPlot >= ny2) {
255 sys_error (ERR_SEVERE, "row_plot > ny");
258 double plot_xaxis [ny];
259 double v1Row[nx], v2Row[nx2];
261 for (int i = 0; i < ny; i++)
263 for (int i = 0; i < nx; i++)
264 v1Row[i] = v1[i][opt_rowPlot];
265 for (int i = 0; i < nx2; i++)
266 v2Row[i] = v2[i][opt_rowPlot];
269 SGPDriver driver ("Row Plot");
272 ezplot.ezset ("clear.");
273 ezplot.ezset ("xticks major 5.");
274 ezplot.ezset ("title Row Plot");
275 ezplot.ezset ("xlabel Row");
276 ezplot.ezset ("ylabel Pixel");
277 ezplot.ezset ("box.");
278 ezplot.ezset ("grid.");
279 ezplot.addCurve (plot_xaxis, v1Row, im_in1.nx());
280 ezplot.addCurve (plot_xaxis, v2Row, im_in2.nx());
282 std::cout << "Press enter to continue" << flush;
287 if (opt_outputFile) {
288 pim_out->labelsCopy (im_in1, "if-2 file 1: ");
289 pim_out->labelsCopy (im_in2, "if-2 file 2: ");
290 pim_out->labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str(), timerProgram.timerEnd());
291 pim_out->fileWrite(out_file);
299 main (int argc, char *const argv[])
304 retval = if2_main(argc, argv);
305 } catch (exception e) {
306 cerr << "Exception: " << e.what() << std::endl;
308 cerr << "Unknown exception\n";