b1d352bb5e994a43a3eb315719fc0a841a535613
[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.9 2000/06/26 21:15:24 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 ();
139     im_in->fileRead (in_file);
140     int nx = im_in->nx();
141     int ny = im_in->ny();
142     im_out = new ImageFile (nx, ny);
143
144     ImageFileArray vIn = im_in->getArray();
145     ImageFileArray vOut = im_out->getArray();
146
147     if (opt_invert) {
148       for (ix = 0; ix < nx; ix++)
149         for (iy = 0; iy < ny; iy++)
150           vOut[ix][iy] = - vIn[ix][iy];
151       histString = "Invert transformation";
152     }
153     if (opt_log) {
154       for (ix = 0; ix < nx; ix++)
155         for (iy = 0; iy < ny; iy++)
156           vOut[ix][iy] = log (vIn[ix][iy]);
157       histString = "Log transformation";
158     }
159     if (opt_exp) {
160       for (ix = 0; ix < nx; ix++)
161         for (iy = 0; iy < ny; iy++)
162           vOut[ix][iy] = exp (vIn[ix][iy]);
163       histString = "Exp transformation";
164     }
165     if (opt_sqr) {
166       for (ix = 0; ix < nx; ix++)
167         for (iy = 0; iy < ny; iy++)
168           vOut[ix][iy] = vIn[ix][iy] * vIn[ix][iy];
169       histString = "Sqr transformation";
170     }
171     if (opt_sqrt) {
172       for (ix = 0; ix < nx; ix++)
173         for (iy = 0; iy < ny; iy++)
174           vOut[ix][iy] = sqrt (vIn[ix][iy]);
175       histString = "Sqrt transformation";
176     }
177
178     im_out->labelsCopy (*im_in);
179     im_out->labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
180     im_out->fileWrite (out_file);
181   }
182
183   return (0);
184 }
185
186 #ifndef NO_MAIN
187 int 
188 main (int argc, char *const argv[])
189 {
190   int retval = 1;
191
192   try {
193     retval = if1_main(argc, argv);
194   } catch (exception e) {
195     cerr << "Exception: " << e.what() << endl;
196   } catch (...) {
197     cerr << "Unknown exception" << endl;
198   }
199
200   return (retval);
201 }
202 #endif
203