dcd1dc1a3ac25eb0144d11a7ac7041610477aa86
[ctsim.git] / tools / if1.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if1.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: if1.cpp,v 1.2 2000/12/23 18:12:35 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  *   if1.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 static const char* g_szIdStr = "$Id: if1.cpp,v 1.2 2000/12/23 18:12:35 kevin Exp $";
50
51 void 
52 if1_usage (const char *program)
53 {
54   std::cout << "usage: " << fileBasename(program) << " infile outfile [OPTIONS]" << std::endl;
55   std::cout << "Generate a IF file from a IF file" << std::endl;
56   std::cout << std::endl;
57   std::cout << "     --invert   Invert image" << std::endl;
58   std::cout << "     --log      Natural logrithm of image" << std::endl;
59   std::cout << "     --exp      Natural exponential of image" << std::endl;
60   std::cout << "     --sqr      Square of image" << std::endl;
61   std::cout << "     --sqrt     Square root of image" << std::endl;
62   std::cout << "     --verbose  Verbose modem" << std::endl;
63   std::cout << "     --version  Print version" << std::endl;
64   std::cout << "     --help     Print this help message" << std::endl;
65 }
66
67 int 
68 if1_main (int argc, char *const argv[])
69 {
70   ImageFile *im_in;
71   ImageFile *im_out;
72   char *in_file;
73   char *out_file;
74   int opt_verbose = 0;
75   int opt_invert = 0;
76   int opt_log = 0;
77   int opt_exp = 0;
78   int opt_sqr = 0;
79   int opt_sqrt = 0;
80
81   while (1)
82     {
83       int c = getopt_long (argc, argv, "", my_options, NULL);
84       
85       if (c == -1)
86         break;
87       
88       switch (c)
89         {
90         case O_INVERT:
91           opt_invert = 1;
92           break;
93         case O_LOG:
94           opt_log = 1;
95           break;
96         case O_SQR:
97           opt_sqr = 1;
98           break;
99         case O_SQRT:
100           opt_sqrt = 1;
101           break;
102         case O_EXP:
103           opt_exp = 1;
104           break;
105         case O_VERBOSE:
106           opt_verbose = 1;
107           break;
108         case O_VERSION:
109 #ifdef VERSION
110           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
111 #else
112           std::cout << "Unknown version number" << std::endl;
113 #endif
114           return (0);
115         case O_HELP:
116         case '?':
117           if1_usage(argv[0]);
118           return (0);
119         default:
120           if1_usage(argv[0]);
121           return (1);
122         }
123     }
124
125   if (optind + 2 != argc)
126     {
127       if1_usage(argv[0]);
128       return (1);
129     }
130   
131   in_file = argv[optind];
132   out_file = argv[optind + 1];
133
134
135   std::string histString;
136
137   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
138     int ix, iy;
139
140     im_in = new ImageFile ();
141     im_in->fileRead (in_file);
142     int nx = im_in->nx();
143     int ny = im_in->ny();
144     im_out = new ImageFile (nx, ny);
145
146     ImageFileArray vIn = im_in->getArray();
147     ImageFileArray vOut = im_out->getArray();
148
149     if (opt_invert) {
150       for (ix = 0; ix < nx; ix++)
151         for (iy = 0; iy < ny; iy++)
152           vOut[ix][iy] = - vIn[ix][iy];
153       histString = "Invert transformation";
154     }
155     if (opt_log) {
156       for (ix = 0; ix < nx; ix++)
157         for (iy = 0; iy < ny; iy++)\r
158           if (vIn[ix][iy] < 0)\r
159             vOut[ix][iy] = 0;
160           else\r
161             vOut[ix][iy] = log (vIn[ix][iy]);
162       histString = "Logrithmic transformation";
163     }
164     if (opt_exp) {
165       for (ix = 0; ix < nx; ix++)
166         for (iy = 0; iy < ny; iy++)
167           vOut[ix][iy] = exp (vIn[ix][iy]);
168       histString = "Exponential transformation";
169     }
170     if (opt_sqr) {
171       for (ix = 0; ix < nx; ix++)
172         for (iy = 0; iy < ny; iy++)
173           vOut[ix][iy] = vIn[ix][iy] * vIn[ix][iy];
174       histString = "Square transformation";
175     }
176     if (opt_sqrt) {
177       for (ix = 0; ix < nx; ix++)
178         for (iy = 0; iy < ny; iy++)\r
179           if (vIn[ix][iy] < 0)
180               vOut[ix][iy] = sqrt (-vIn[ix][iy]);\r
181           else\r
182               vOut[ix][iy] = sqrt (vIn[ix][iy]);\r
183       histString = "Square root transformation";
184     }
185
186     im_out->labelsCopy (*im_in);
187     im_out->labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
188     im_out->fileWrite (out_file);
189   }
190
191   return (0);
192 }
193
194 #ifndef NO_MAIN
195 int 
196 main (int argc, char *const argv[])
197 {
198   int retval = 1;
199
200   try {
201     retval = if1_main(argc, argv);
202   } catch (exception e) {
203     std::cerr << "Exception: " << e.what() << std::endl;
204   } catch (...) {
205     std::cerr << "Unknown exception" << std::endl;
206   }
207
208   return (retval);
209 }
210 #endif
211