r83: Converted to IF data files and C++
[ctsim.git] / src / if-2.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: if-2.cpp,v 1.1 2000/06/07 07:43:19 kevin Exp $
6 **  $Log: if-2.cpp,v $
7 **  Revision 1.1  2000/06/07 07:43:19  kevin
8 **  Converted to IF data files and C++
9 **
10 **  Revision 1.1  2000/06/07 02:29:05  kevin
11 **  Initial C++ versions
12 **
13 **  Revision 1.5  2000/05/24 22:50:04  kevin
14 **  Added support for new SGP library
15 **
16 **  Revision 1.4  2000/05/16 04:33:59  kevin
17 **  Improved option processing
18 **
19 **  Revision 1.3  2000/05/11 01:06:30  kevin
20 **  Changed sprintf to snprintf
21 **
22 **  Revision 1.2  2000/05/08 20:02:32  kevin
23 **  ANSI C changes
24 **
25 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
26 **  Initial CVS import for first public release
27 **
28 **
29 **
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.
33 **
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.
38 **
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 ******************************************************************************/
43
44 /* FILE
45  *   sdf-2.c         Generate a SDF file from two input SDF files
46  */
47
48 #include "ct.h"
49
50 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
51
52 static struct option my_options[] =
53 {
54   {"add", 0, 0, O_ADD},
55   {"sub", 0, 0, O_SUB},
56   {"mul", 0, 0, O_MUL},
57   {"comp", 0, 0, O_COMP},
58   {"verbose", 0, 0, O_VERBOSE},
59   {"help", 0, 0, O_HELP},
60   {"version", 0, 0, O_VERSION},
61   {0, 0, 0, 0}
62 };
63
64 void 
65 sdf2_usage (const char *program)
66 {
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");
80 }
81
82 int 
83 sdf2_main (int argc, char *const argv[])
84 {
85   ImageFile* pim_in1;
86   ImageFile* pim_in2;
87   ImageFile* pim_out;
88   char *in_file1;
89   char *in_file2;
90   char *out_file;
91   int opt_verbose = 0;
92   int opt_add = 0;
93   int opt_sub = 0;
94   int opt_mul = 0;
95   int opt_comp = 0;
96
97   while (1) {
98     int c = getopt_long (argc, argv, "", my_options, NULL);
99       
100     if (c == -1)
101       break;
102       
103     switch (c) {
104     case O_ADD:
105       opt_add = 1;
106       break;
107     case O_SUB :
108       opt_sub = 1;
109       break;
110     case O_MUL:
111       opt_mul = 1;
112       break;
113     case O_COMP:
114       opt_comp = 1;
115       break;
116     case O_VERBOSE:
117       opt_verbose = 1;
118       break;
119     case O_VERSION:
120 #ifdef VERSION
121       fprintf(stdout, "Version %s\n", VERSION);
122 #else
123       fprintf(stderr, "Unknown version number");
124 #endif
125       exit(0);
126     case O_HELP:
127     case '?':
128       sdf2_usage(argv[0]);
129       return (0);
130     default:
131       sdf2_usage(argv[0]);
132       return (1);
133     }
134   }
135   
136   if (optind + 3 != argc) {
137     sdf2_usage(argv[0]);
138     return (1);
139   }
140   
141   in_file1 = argv[optind];
142   in_file2 = argv[optind + 1];
143   out_file = argv[optind + 2];
144
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;
149
150   if (! im_in1.adf.fileRead() || ! im_in2.adf.fileRead()) {
151       sys_error (ERR_WARNING, "Error reading an image");
152       return (1);
153   }
154
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());
158     return(1);
159   }
160   if (im_in1.nx() < 0 || im_in1.ny() < 0) {
161       fprintf(stderr, "Error: Size of image < 0");
162       return(1);
163   }
164
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);
169     return (1);
170   }
171
172   string strOperation;
173   ImageFileArray v1 = im_in1.getArray();
174   ImageFileArray v2 = im_in2.getArray();
175   ImageFileArray vout = im_out.getArray();
176
177   if (opt_add) {
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++;
185     }
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++;
194     }
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++;
203     }
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++;
213         *out++ = diff;
214         abs_error += fabs(diff);
215       }
216     }
217     abs_error /= (im_in1.nx() * im_in1.ny());
218     fprintf(stdout, "Average Error = %f\n", abs_error);
219   }
220
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());
225
226   im_out.adf.fileClose();
227
228   return (0);
229 }
230
231 #ifndef NO_MAIN
232 int 
233 main (int argc, char *const argv[])
234 {
235   return (sdf2_main(argc, argv));
236 }
237 #endif
238