cac6df3f1cf40821dbd1aa058eaa895544bf9db0
[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.4 2000/12/29 19:30:08 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.4 2000/12/29 19:30:08 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 mode" << 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   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           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
109 #else
110           std::cout << "Unknown version number" << std::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   std::string histString;
133
134   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
135     ImageFile im_in;
136     im_in.fileRead (in_file);
137     int nx = im_in.nx();
138     int ny = im_in.ny();
139     ImageFile im_out (nx, ny);
140
141     if (opt_invert) {\r
142       im_in.invertPixelValues (im_out);
143       histString = "Invert transformation";
144     }
145     if (opt_log) {\r
146       im_in.log (im_out);
147       histString = "Logrithmic transformation";
148     }
149     if (opt_exp) {\r
150       im_in.exp (im_out);
151       histString = "Exponential transformation";
152     }
153     if (opt_sqr) {\r
154       im_in.square (im_out);
155       histString = "Square transformation";
156     }
157     if (opt_sqrt) {\r
158       im_in.sqrt (im_out);
159       histString = "Square root transformation";
160     }
161
162     im_out.labelsCopy (im_in);
163     im_out.labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
164     im_out.fileWrite (out_file);
165   }
166
167   return (0);
168 }
169
170 #ifndef NO_MAIN
171 int 
172 main (int argc, char *const argv[])
173 {
174   int retval = 1;
175
176   try {
177     retval = if1_main(argc, argv);
178   } catch (exception e) {
179     std::cerr << "Exception: " << e.what() << std::endl;
180   } catch (...) {
181     std::cerr << "Unknown exception" << std::endl;
182   }
183
184   return (retval);
185 }
186 #endif
187