r75: *** empty log message ***
[ctsim.git] / src / if-1.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: if-1.cpp,v 1.2 2000/06/07 03:50:27 kevin Exp $
6 **  $Log: if-1.cpp,v $
7 **  Revision 1.2  2000/06/07 03:50:27  kevin
8 **  *** empty log message ***
9 **
10 **  Revision 1.1  2000/06/07 02:29:05  kevin
11 **  Initial C++ versions
12 **
13 **  Revision 1.6  2000/05/24 22:50:04  kevin
14 **  Added support for new SGP library
15 **
16 **  Revision 1.5  2000/05/16 04:33:59  kevin
17 **  Improved option processing
18 **
19 **  Revision 1.4  2000/05/09 14:52:27  kevin
20 **  added sqr and sqrt functions
21 **
22 **  Revision 1.3  2000/05/08 20:02:32  kevin
23 **  ANSI C changes
24 **
25 **  Revision 1.2  2000/05/03 08:49:50  kevin
26 **  Code cleanup
27 **
28 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
29 **  Initial CVS import for first public release
30 **
31 **
32 **
33 **  This program is free software; you can redistribute it and/or modify
34 **  it under the terms of the GNU General Public License (version 2) as
35 **  published by the Free Software Foundation.
36 **
37 **  This program is distributed in the hope that it will be useful,
38 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
39 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40 **  GNU General Public License for more details.
41 **
42 **  You should have received a copy of the GNU General Public License
43 **  along with this program; if not, write to the Free Software
44 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45 ******************************************************************************/
46
47 /* FILE
48  *   if-1.c             Filter a single IF file
49  */
50
51 #include "ct.h"
52
53 enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
54
55 static struct option my_options[] =
56 {
57   {"invert", 0, 0, O_INVERT},
58   {"verbose", 0, 0, O_VERBOSE},
59   {"log", 0, 0, O_LOG},
60   {"exp", 0, 0, O_EXP},
61   {"sqr", 0, 0, O_SQR},
62   {"sqrt", 0, 0, O_SQRT},
63   {"help", 0, 0, O_HELP},
64   {"version", 0, 0, O_VERSION},
65   {0, 0, 0, 0}
66 };
67
68 void 
69 if1_usage (const char *program)
70 {
71   fprintf(stdout, "if1_usage: %s infile outfile [OPTIONS]\n", kbasename(program));
72   fprintf(stdout, "Generate a IF file from a IF file\n");
73   fprintf(stdout, "\n");
74   fprintf(stdout, "     --invert   Invert image\n");
75   fprintf(stdout, "     --log      Natural logrithm of image\n");
76   fprintf(stdout, "     --exp      Natural exponential of image\n");
77   fprintf(stdout, "     --sqr      Square of image\n");
78   fprintf(stdout, "     --sqrt     Square root of image\n");
79   fprintf(stdout, "     --verbose  Verbose modem\n");
80   fprintf(stdout, "     --version  Print version\n");
81   fprintf(stdout, "     --help     Print this help message\n");
82 }
83
84 int 
85 if1_main (int argc, char *const argv[])
86 {
87   ImageFile *im_in;
88   ImageFile *im_out;
89   char *in_file;
90   char *out_file;
91   int opt_verbose = 0;
92   int opt_invert = 0;
93   int opt_log = 0;
94   int opt_exp = 0;
95   int opt_sqr = 0;
96   int opt_sqrt = 0;
97
98   while (1)
99     {
100       int c = getopt_long (argc, argv, "", my_options, NULL);
101       
102       if (c == -1)
103         break;
104       
105       switch (c)
106         {
107         case O_INVERT:
108           opt_invert = 1;
109           break;
110         case O_LOG:
111           opt_log = 1;
112           break;
113         case O_SQR:
114           opt_sqr = 1;
115           break;
116         case O_SQRT:
117           opt_sqrt = 1;
118           break;
119         case O_EXP:
120           opt_exp = 1;
121           break;
122         case O_VERBOSE:
123           opt_verbose = 1;
124           break;
125         case O_VERSION:
126 #ifdef VERSION
127           fprintf(stdout, "Version %s\n", VERSION);
128 #else
129           fprintf(stderr, "Unknown version number");
130 #endif
131           exit(0);
132         case O_HELP:
133         case '?':
134           if1_usage(argv[0]);
135           return (0);
136         default:
137           if1_usage(argv[0]);
138           return (1);
139         }
140     }
141
142   if (optind + 2 != argc)
143     {
144       if1_usage(argv[0]);
145       return (1);
146     }
147   
148   in_file = argv[optind];
149   out_file = argv[optind + 1];
150
151
152   string histString;
153
154   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
155     int ix, iy;
156
157     im_in = new ImageFile (in_file);
158     im_in->adf.fileRead ();
159     int nx = im_in->adf.nx();
160     int ny = im_in->adf.ny();
161     im_out = new ImageFile (out_file, nx, ny);
162     im_out->adf.fileCreate ();
163
164     ImageFileArray vIn = im_in->adf.getArray();
165     ImageFileArray vOut = im_out->adf.getArray();
166
167     if (opt_invert) {
168       for (ix = 0; ix < nx; ix++)
169         for (iy = 0; iy < ny; iy++)
170           vOut[ix][iy] = - vIn[ix][iy];
171       histString = "Invert transformation";
172     }
173     if (opt_log) {
174       for (ix = 0; ix < nx; ix++)
175         for (iy = 0; iy < ny; iy++)
176           vOut[ix][iy] = log (vIn[ix][iy]);
177       histString = "Log transformation";
178     }
179     if (opt_exp) {
180       for (ix = 0; ix < nx; ix++)
181         for (iy = 0; iy < ny; iy++)
182           vOut[ix][iy] = exp (vIn[ix][iy]);
183       histString = "Exp transformation";
184     }
185     if (opt_sqr) {
186       for (ix = 0; ix < nx; ix++)
187         for (iy = 0; iy < ny; iy++)
188           vOut[ix][iy] = vIn[ix][iy] * vIn[ix][iy];
189       histString = "Sqr transformation";
190     }
191     if (opt_sqrt) {
192       for (ix = 0; ix < nx; ix++)
193         for (iy = 0; iy < ny; iy++)
194           vOut[ix][iy] = sqrt (vIn[ix][iy]);
195       histString = "Sqrt transformation";
196     }
197
198     im_out->adf.arrayDataWrite ();
199     for (int i = 0; i < im_in->adf.getNumLabels(); i++) {
200       Array2dFileLabel l;
201       im_in->adf.labelRead (l, i);
202       im_out->adf.labelAdd (l);
203     }
204     im_out->adf.labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
205     im_out->adf.fileClose ();
206   }
207
208   return (0);
209 }
210
211 #ifndef NO_MAIN
212 int 
213 main (int argc, char *const argv[])
214 {
215   return (if1_main(argc, argv));
216 }
217 #endif
218