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