1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: if-2.cpp,v 1.1 2000/06/07 07:43:19 kevin Exp $
7 ** Revision 1.1 2000/06/07 07:43:19 kevin
8 ** Converted to IF data files and C++
10 ** Revision 1.1 2000/06/07 02:29:05 kevin
11 ** Initial C++ versions
13 ** Revision 1.5 2000/05/24 22:50:04 kevin
14 ** Added support for new SGP library
16 ** Revision 1.4 2000/05/16 04:33:59 kevin
17 ** Improved option processing
19 ** Revision 1.3 2000/05/11 01:06:30 kevin
20 ** Changed sprintf to snprintf
22 ** Revision 1.2 2000/05/08 20:02:32 kevin
25 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
26 ** Initial CVS import for first public release
30 ** This program is free software; you can redistribute it and/or modify
31 ** it under the terms of the GNU General Public License (version 2) as
32 ** published by the Free Software Foundation.
34 ** This program is distributed in the hope that it will be useful,
35 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
36 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 ** GNU General Public License for more details.
39 ** You should have received a copy of the GNU General Public License
40 ** along with this program; if not, write to the Free Software
41 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 ******************************************************************************/
45 * sdf-2.c Generate a SDF file from two input SDF files
50 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
52 static struct option my_options[] =
57 {"comp", 0, 0, O_COMP},
58 {"verbose", 0, 0, O_VERBOSE},
59 {"help", 0, 0, O_HELP},
60 {"version", 0, 0, O_VERSION},
65 sdf2_usage (const char *program)
67 fprintf(stdout, "sdf2_usage: %s infile1 infile2 outfile [OPTIONS]\n", kbasename(program));
68 fprintf(stdout, "Generate an SDF2D file from two input SDF2D files\n");
69 fprintf(stdout, "\n");
70 fprintf(stdout, " infile1 Name of first input SDF file\n");
71 fprintf(stdout, " infile2 Name of second input SDF file\n");
72 fprintf(stdout, " outfile Name of output SDF file\n");
73 fprintf(stdout, " --add Add images\n");
74 fprintf(stdout, " --sub Subtract image 2 from image 1\n");
75 fprintf(stdout, " --mul Multiply images\n");
76 fprintf(stdout, " --comp Compare images\n");
77 fprintf(stdout, " --verbose Verbose modem\n");
78 fprintf(stdout, " --version Print version\n");
79 fprintf(stdout, " --help Print this help message\n");
83 sdf2_main (int argc, char *const argv[])
98 int c = getopt_long (argc, argv, "", my_options, NULL);
121 fprintf(stdout, "Version %s\n", VERSION);
123 fprintf(stderr, "Unknown version number");
136 if (optind + 3 != argc) {
141 in_file1 = argv[optind];
142 in_file2 = argv[optind + 1];
143 out_file = argv[optind + 2];
145 pim_in1 = new ImageFile (in_file1);
146 pim_in2 = new ImageFile (in_file2);
147 ImageFile& im_in1 = *pim_in1;
148 ImageFile& im_in2 = *pim_in2;
150 if (! im_in1.adf.fileRead() || ! im_in2.adf.fileRead()) {
151 sys_error (ERR_WARNING, "Error reading an image");
155 if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
156 fprintf(stderr, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match\n",
157 im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
160 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
161 fprintf(stderr, "Error: Size of image < 0");
165 pim_out = new ImageFile (out_file, im_in1.nx(), im_in1.ny());
166 ImageFile& im_out = *pim_out;
167 if (! im_out.adf.fileCreate()) {
168 sys_error (ERR_WARNING, "Could not open output file %s", out_file);
173 ImageFileArray v1 = im_in1.getArray();
174 ImageFileArray v2 = im_in2.getArray();
175 ImageFileArray vout = im_out.getArray();
178 strOperation = "Add Images";
179 for (int ix = 0; ix < im_in1.nx(); ix++) {
180 ImageFileColumn in1 = v1[ix];
181 ImageFileColumn in2 = v2[ix];
182 ImageFileColumn out = vout[ix];
183 for (int iy = 0; iy < im_in1.ny(); iy++)
184 *out++ = *in1++ + *in2++;
186 } else if (opt_sub) {
187 strOperation = "Subtract Images";
188 for (int ix = 0; ix < im_in1.nx(); ix++) {
189 ImageFileColumn in1 = v1[ix];
190 ImageFileColumn in2 = v2[ix];
191 ImageFileColumn out = vout[ix];
192 for (int iy = 0; iy < im_in1.ny(); iy++)
193 *out++ = *in1++ - *in2++;
195 } else if (opt_mul) {
196 strOperation = "Multiply Images";
197 for (int ix = 0; ix < im_in1.nx(); ix++) {
198 ImageFileColumn in1 = v1[ix];
199 ImageFileColumn in2 = v2[ix];
200 ImageFileColumn out = vout[ix];
201 for (int iy = 0; iy < im_in1.ny(); iy++)
202 *out++ = *in1++ * *in2++;
204 } else if (opt_comp) {
205 double abs_error = 0.;
206 strOperation = "Subtract 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 double diff = *in1++ - *in2++;
214 abs_error += fabs(diff);
217 abs_error /= (im_in1.nx() * im_in1.ny());
218 fprintf(stdout, "Average Error = %f\n", abs_error);
221 im_out.adf.arrayDataWrite();
222 im_out.adf.labelsCopy (im_in1.adf, "if-2 file 1: ");
223 im_out.adf.labelsCopy (im_in2.adf, "if-2 file 2: ");
224 im_out.adf.labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str());
226 im_out.adf.fileClose();
233 main (int argc, char *const argv[])
235 return (sdf2_main(argc, argv));