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/06/08 16:43:10 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 * sdf-2.c Generate a SDF file from two input SDF files
34 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
36 static struct option my_options[] =
41 {"comp", 0, 0, O_COMP},
42 {"verbose", 0, 0, O_VERBOSE},
43 {"help", 0, 0, O_HELP},
44 {"version", 0, 0, O_VERSION},
49 sdf2_usage (const char *program)
51 fprintf(stdout, "sdf2_usage: %s infile1 infile2 outfile [OPTIONS]\n", kbasename(program));
52 fprintf(stdout, "Generate an SDF2D file from two input SDF2D files\n");
53 fprintf(stdout, "\n");
54 fprintf(stdout, " infile1 Name of first input SDF file\n");
55 fprintf(stdout, " infile2 Name of second input SDF file\n");
56 fprintf(stdout, " outfile Name of output SDF file\n");
57 fprintf(stdout, " --add Add images\n");
58 fprintf(stdout, " --sub Subtract image 2 from image 1\n");
59 fprintf(stdout, " --mul Multiply images\n");
60 fprintf(stdout, " --comp Compare images\n");
61 fprintf(stdout, " --verbose Verbose modem\n");
62 fprintf(stdout, " --version Print version\n");
63 fprintf(stdout, " --help Print this help message\n");
67 sdf2_main (int argc, char *const argv[])
82 int c = getopt_long (argc, argv, "", my_options, NULL);
105 fprintf(stdout, "Version %s\n", VERSION);
107 fprintf(stderr, "Unknown version number");
120 if (optind + 3 != argc) {
125 in_file1 = argv[optind];
126 in_file2 = argv[optind + 1];
127 out_file = argv[optind + 2];
129 pim_in1 = new ImageFile (in_file1);
130 pim_in2 = new ImageFile (in_file2);
131 ImageFile& im_in1 = *pim_in1;
132 ImageFile& im_in2 = *pim_in2;
134 if (! im_in1.adf.fileRead() || ! im_in2.adf.fileRead()) {
135 sys_error (ERR_WARNING, "Error reading an image");
139 if (im_in1.nx() != im_in2.nx() || im_in1.ny() != im_in2.ny()) {
140 fprintf(stderr, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match\n",
141 im_in1.nx(), im_in1.ny(), im_in2.nx(), im_in2.ny());
144 if (im_in1.nx() < 0 || im_in1.ny() < 0) {
145 fprintf(stderr, "Error: Size of image < 0");
149 pim_out = new ImageFile (out_file, im_in1.nx(), im_in1.ny());
150 ImageFile& im_out = *pim_out;
151 if (! im_out.adf.fileCreate()) {
152 sys_error (ERR_WARNING, "Could not open output file %s", out_file);
157 ImageFileArray v1 = im_in1.getArray();
158 ImageFileArray v2 = im_in2.getArray();
159 ImageFileArray vout = im_out.getArray();
162 strOperation = "Add Images";
163 for (int ix = 0; ix < im_in1.nx(); ix++) {
164 ImageFileColumn in1 = v1[ix];
165 ImageFileColumn in2 = v2[ix];
166 ImageFileColumn out = vout[ix];
167 for (int iy = 0; iy < im_in1.ny(); iy++)
168 *out++ = *in1++ + *in2++;
170 } else if (opt_sub) {
171 strOperation = "Subtract Images";
172 for (int ix = 0; ix < im_in1.nx(); ix++) {
173 ImageFileColumn in1 = v1[ix];
174 ImageFileColumn in2 = v2[ix];
175 ImageFileColumn out = vout[ix];
176 for (int iy = 0; iy < im_in1.ny(); iy++)
177 *out++ = *in1++ - *in2++;
179 } else if (opt_mul) {
180 strOperation = "Multiply Images";
181 for (int ix = 0; ix < im_in1.nx(); ix++) {
182 ImageFileColumn in1 = v1[ix];
183 ImageFileColumn in2 = v2[ix];
184 ImageFileColumn out = vout[ix];
185 for (int iy = 0; iy < im_in1.ny(); iy++)
186 *out++ = *in1++ * *in2++;
188 } else if (opt_comp) {
189 double abs_error = 0.;
190 strOperation = "Subtract Images";
191 for (int ix = 0; ix < im_in1.nx(); ix++) {
192 ImageFileColumn in1 = v1[ix];
193 ImageFileColumn in2 = v2[ix];
194 ImageFileColumn out = vout[ix];
195 for (int iy = 0; iy < im_in1.ny(); iy++) {
196 double diff = *in1++ - *in2++;
198 abs_error += fabs(diff);
201 abs_error /= (im_in1.nx() * im_in1.ny());
202 fprintf(stdout, "Average Error = %f\n", abs_error);
205 im_out.adf.arrayDataWrite();
206 im_out.adf.labelsCopy (im_in1.adf, "if-2 file 1: ");
207 im_out.adf.labelsCopy (im_in2.adf, "if-2 file 2: ");
208 im_out.adf.labelAdd (Array2dFileLabel::L_HISTORY, strOperation.c_str());
210 im_out.adf.fileClose();
217 main (int argc, char *const argv[])
219 return (sdf2_main(argc, argv));