16e3b82ce46daac6dbf87f30d05a5769e4a89cab
[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.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: if-1.cpp,v $
7 **  Revision 1.1  2000/06/07 02:29:05  kevin
8 **  Initial C++ versions
9 **
10 **  Revision 1.6  2000/05/24 22:50:04  kevin
11 **  Added support for new SGP library
12 **
13 **  Revision 1.5  2000/05/16 04:33:59  kevin
14 **  Improved option processing
15 **
16 **  Revision 1.4  2000/05/09 14:52:27  kevin
17 **  added sqr and sqrt functions
18 **
19 **  Revision 1.3  2000/05/08 20:02:32  kevin
20 **  ANSI C changes
21 **
22 **  Revision 1.2  2000/05/03 08:49:50  kevin
23 **  Code cleanup
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  *   if-1.c             Filter a single IF file
46  */
47
48 #include "ct.h"
49
50 enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
51
52 static struct option my_options[] =
53 {
54   {"invert", 0, 0, O_INVERT},
55   {"verbose", 0, 0, O_VERBOSE},
56   {"log", 0, 0, O_LOG},
57   {"exp", 0, 0, O_EXP},
58   {"sqr", 0, 0, O_SQR},
59   {"sqrt", 0, 0, O_SQRT},
60   {"help", 0, 0, O_HELP},
61   {"version", 0, 0, O_VERSION},
62   {0, 0, 0, 0}
63 };
64
65 void 
66 if1_usage (const char *program)
67 {
68   fprintf(stdout, "if1_usage: %s infile outfile [OPTIONS]\n", kbasename(program));
69   fprintf(stdout, "Generate a IF file from a IF file\n");
70   fprintf(stdout, "\n");
71   fprintf(stdout, "     --invert   Invert image\n");
72   fprintf(stdout, "     --log      Natural logrithm of image\n");
73   fprintf(stdout, "     --exp      Natural exponential of image\n");
74   fprintf(stdout, "     --sqr      Square of image\n");
75   fprintf(stdout, "     --sqrt     Square root of image\n");
76   fprintf(stdout, "     --verbose  Verbose modem\n");
77   fprintf(stdout, "     --version  Print version\n");
78   fprintf(stdout, "     --help     Print this help message\n");
79 }
80
81 int 
82 if1_main (int argc, char *const argv[])
83 {
84   ImageFile *im_in;
85   ImageFile *im_out;
86   char *in_file;
87   char *out_file;
88   int opt_verbose = 0;
89   int opt_invert = 0;
90   int opt_log = 0;
91   int opt_exp = 0;
92   int opt_sqr = 0;
93   int opt_sqrt = 0;
94
95   while (1)
96     {
97       int c = getopt_long (argc, argv, "", my_options, NULL);
98       
99       if (c == -1)
100         break;
101       
102       switch (c)
103         {
104         case O_INVERT:
105           opt_invert = 1;
106           break;
107         case O_LOG:
108           opt_log = 1;
109           break;
110         case O_SQR:
111           opt_sqr = 1;
112           break;
113         case O_SQRT:
114           opt_sqrt = 1;
115           break;
116         case O_EXP:
117           opt_exp = 1;
118           break;
119         case O_VERBOSE:
120           opt_verbose = 1;
121           break;
122         case O_VERSION:
123 #ifdef VERSION
124           fprintf(stdout, "Version %s\n", VERSION);
125 #else
126           fprintf(stderr, "Unknown version number");
127 #endif
128           exit(0);
129         case O_HELP:
130         case '?':
131           if1_usage(argv[0]);
132           return (0);
133         default:
134           if1_usage(argv[0]);
135           return (1);
136         }
137     }
138
139   if (optind + 2 != argc)
140     {
141       if1_usage(argv[0]);
142       return (1);
143     }
144   
145   in_file = argv[optind];
146   out_file = argv[optind + 1];
147
148
149   string histString;
150
151   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
152     int ix, iy;
153
154     im_in = new ImageFile (in_file);
155     im_in->adf.fileRead ();
156     int nx = im_in->adf.nx;
157     int ny = im_in->adf.ny;
158     im_out = new ImageFile (out_file, nx, ny);
159     im_out->adf.fileCreate ();
160
161     ImageFileArray vIn = im_in->adf.getArray();
162     ImageFileArray vOut = im_out->adf.getArray();
163
164     if (opt_invert) {
165       for (ix = 0; ix < nx; ix++)
166         for (iy = 0; iy < ny; iy++)
167           vOut[ix][iy] = - vIn[ix][iy];
168       histString = "Invert transformation";
169     }
170     if (opt_log) {
171       for (ix = 0; ix < nx; ix++)
172         for (iy = 0; iy < ny; iy++)
173           vOut[ix][iy] = log (vIn[ix][iy]);
174       histString = "Log transformation";
175     }
176     if (opt_exp) {
177       for (ix = 0; ix < nx; ix++)
178         for (iy = 0; iy < ny; iy++)
179           vOut[ix][iy] = exp (vIn[ix][iy]);
180       histString = "Exp transformation";
181     }
182     if (opt_sqr) {
183       for (ix = 0; ix < nx; ix++)
184         for (iy = 0; iy < ny; iy++)
185           vOut[ix][iy] = vIn[ix][iy] * vIn[ix][iy];
186       histString = "Sqr transformation";
187     }
188     if (opt_sqrt) {
189       for (ix = 0; ix < nx; ix++)
190         for (iy = 0; iy < ny; iy++)
191           vOut[ix][iy] = sqrt (vIn[ix][iy]);
192       histString = "Sqrt transformation";
193     }
194
195     im_out->adf.arrayDataWrite ();
196     for (int i = 0; i < im_in->adf.getNumLabels(); i++) {
197       Array2dFileLabel l;
198       im_in->adf.labelRead (l, i);
199       im_out->adf.labelAdd (l);
200     }
201     im_out->adf.labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
202     im_out->adf.fileClose ();
203   }
204
205   return (0);
206 }
207
208 #ifndef NO_MAIN
209 int 
210 main (int argc, char *const argv[])
211 {
212   return (if1_main(argc, argv));
213 }
214 #endif
215