f85a269176bcfd981eb369adbffca8beca75b4e2
[ctsim.git] / src / if-1.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if-1.cpp
5 **   Purpose:       Manipulate a single image file
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Aug 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: if-1.cpp,v 1.7 2000/06/18 10:27:11 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 /* FILE
29  *   if-1.c             Filter a single IF file
30  */
31
32 #include "ct.h"
33
34 enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
35
36 static struct option my_options[] =
37 {
38   {"invert", 0, 0, O_INVERT},
39   {"verbose", 0, 0, O_VERBOSE},
40   {"log", 0, 0, O_LOG},
41   {"exp", 0, 0, O_EXP},
42   {"sqr", 0, 0, O_SQR},
43   {"sqrt", 0, 0, O_SQRT},
44   {"help", 0, 0, O_HELP},
45   {"version", 0, 0, O_VERSION},
46   {0, 0, 0, 0}
47 };
48
49 void 
50 if1_usage (const char *program)
51 {
52   cout << "usage: " << fileBasename(program) << " infile outfile [OPTIONS]" << endl;
53   cout << "Generate a IF file from a IF file" << endl;
54   cout << endl;
55   cout << "     --invert   Invert image" << endl;
56   cout << "     --log      Natural logrithm of image" << endl;
57   cout << "     --exp      Natural exponential of image" << endl;
58   cout << "     --sqr      Square of image" << endl;
59   cout << "     --sqrt     Square root of image" << endl;
60   cout << "     --verbose  Verbose modem" << endl;
61   cout << "     --version  Print version" << endl;
62   cout << "     --help     Print this help message" << endl;
63 }
64
65 int 
66 if1_main (int argc, char *const argv[])
67 {
68   ImageFile *im_in;
69   ImageFile *im_out;
70   char *in_file;
71   char *out_file;
72   int opt_verbose = 0;
73   int opt_invert = 0;
74   int opt_log = 0;
75   int opt_exp = 0;
76   int opt_sqr = 0;
77   int opt_sqrt = 0;
78
79   while (1)
80     {
81       int c = getopt_long (argc, argv, "", my_options, NULL);
82       
83       if (c == -1)
84         break;
85       
86       switch (c)
87         {
88         case O_INVERT:
89           opt_invert = 1;
90           break;
91         case O_LOG:
92           opt_log = 1;
93           break;
94         case O_SQR:
95           opt_sqr = 1;
96           break;
97         case O_SQRT:
98           opt_sqrt = 1;
99           break;
100         case O_EXP:
101           opt_exp = 1;
102           break;
103         case O_VERBOSE:
104           opt_verbose = 1;
105           break;
106         case O_VERSION:
107 #ifdef VERSION
108           cout << "Version " <<  VERSION << endl;
109 #else
110           cout << "Unknown version number" << endl;
111 #endif
112           return (0);
113         case O_HELP:
114         case '?':
115           if1_usage(argv[0]);
116           return (0);
117         default:
118           if1_usage(argv[0]);
119           return (1);
120         }
121     }
122
123   if (optind + 2 != argc)
124     {
125       if1_usage(argv[0]);
126       return (1);
127     }
128   
129   in_file = argv[optind];
130   out_file = argv[optind + 1];
131
132
133   string histString;
134
135   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
136     int ix, iy;
137
138     im_in = new ImageFile (in_file);
139     im_in->fileRead ();
140     int nx = im_in->nx();
141     int ny = im_in->ny();
142     im_out = new ImageFile (out_file, nx, ny);
143     im_out->fileCreate ();
144
145     ImageFileArray vIn = im_in->getArray();
146     ImageFileArray vOut = im_out->getArray();
147
148     if (opt_invert) {
149       for (ix = 0; ix < nx; ix++)
150         for (iy = 0; iy < ny; iy++)
151           vOut[ix][iy] = - vIn[ix][iy];
152       histString = "Invert transformation";
153     }
154     if (opt_log) {
155       for (ix = 0; ix < nx; ix++)
156         for (iy = 0; iy < ny; iy++)
157           vOut[ix][iy] = log (vIn[ix][iy]);
158       histString = "Log transformation";
159     }
160     if (opt_exp) {
161       for (ix = 0; ix < nx; ix++)
162         for (iy = 0; iy < ny; iy++)
163           vOut[ix][iy] = exp (vIn[ix][iy]);
164       histString = "Exp transformation";
165     }
166     if (opt_sqr) {
167       for (ix = 0; ix < nx; ix++)
168         for (iy = 0; iy < ny; iy++)
169           vOut[ix][iy] = vIn[ix][iy] * vIn[ix][iy];
170       histString = "Sqr transformation";
171     }
172     if (opt_sqrt) {
173       for (ix = 0; ix < nx; ix++)
174         for (iy = 0; iy < ny; iy++)
175           vOut[ix][iy] = sqrt (vIn[ix][iy]);
176       histString = "Sqrt transformation";
177     }
178
179     im_out->arrayDataWrite ();
180     im_out->labelsCopy (*im_in);
181     im_out->labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
182     im_out->fileClose ();
183   }
184
185   return (0);
186 }
187
188 #ifndef NO_MAIN
189 int 
190 main (int argc, char *const argv[])
191 {
192   return (if1_main(argc, argv));
193 }
194 #endif
195