Update copyright date; remove old CVS keyword
[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-2009 Kevin Rosenberg
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26 /* FILE
27  *   if1.c             Filter a single IF file
28  */
29
30 #include "ct.h"
31
32 enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
33
34 static struct option my_options[] =
35 {
36   {"invert", 0, 0, O_INVERT},
37   {"verbose", 0, 0, O_VERBOSE},
38   {"log", 0, 0, O_LOG},
39   {"exp", 0, 0, O_EXP},
40   {"sqr", 0, 0, O_SQR},
41   {"sqrt", 0, 0, O_SQRT},
42   {"help", 0, 0, O_HELP},
43   {"version", 0, 0, O_VERSION},
44   {0, 0, 0, 0}
45 };
46
47 static const char* g_szIdStr = "$Id$";
48
49 void
50 if1_usage (const char *program)
51 {
52   std::cout << "usage: " << fileBasename(program) << " infile outfile [OPTIONS]" << std::endl;
53   std::cout << "Generate a IF file from a IF file" << std::endl;
54   std::cout << std::endl;
55   std::cout << "     --invert   Invert image" << std::endl;
56   std::cout << "     --log      Natural logrithm of image" << std::endl;
57   std::cout << "     --exp      Natural exponential of image" << std::endl;
58   std::cout << "     --sqr      Square of image" << std::endl;
59   std::cout << "     --sqrt     Square root of image" << std::endl;
60   std::cout << "     --verbose  Verbose mode" << std::endl;
61   std::cout << "     --version  Print version" << std::endl;
62   std::cout << "     --help     Print this help message" << std::endl;
63 }
64
65 int
66 if1_main (int argc, char *const argv[])
67 {
68   char *in_file;
69   char *out_file;
70   int opt_verbose = 0;
71   int opt_invert = 0;
72   int opt_log = 0;
73   int opt_exp = 0;
74   int opt_sqr = 0;
75   int opt_sqrt = 0;
76
77   while (1)
78     {
79       int c = getopt_long (argc, argv, "", my_options, NULL);
80
81       if (c == -1)
82         break;
83
84       switch (c)
85         {
86         case O_INVERT:
87           opt_invert = 1;
88           break;
89         case O_LOG:
90           opt_log = 1;
91           break;
92         case O_SQR:
93           opt_sqr = 1;
94           break;
95         case O_SQRT:
96           opt_sqrt = 1;
97           break;
98         case O_EXP:
99           opt_exp = 1;
100           break;
101         case O_VERBOSE:
102           opt_verbose = 1;
103           break;
104         case O_VERSION:
105 #ifdef VERSION
106           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
107 #else
108           std::cout << "Unknown version number" << std::endl;
109 #endif
110           return (0);
111         case O_HELP:
112         case '?':
113           if1_usage(argv[0]);
114           return (0);
115         default:
116           if1_usage(argv[0]);
117           return (1);
118         }
119     }
120
121   if (optind + 2 != argc)
122     {
123       if1_usage(argv[0]);
124       return (1);
125     }
126
127   in_file = argv[optind];
128   out_file = argv[optind + 1];
129
130   std::string histString;
131
132   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
133     ImageFile im_in;
134     im_in.fileRead (in_file);
135     int nx = im_in.nx();
136     int ny = im_in.ny();
137     ImageFile im_out (nx, ny);
138
139     if (opt_invert) {
140       im_in.invertPixelValues (im_out);
141       histString = "Invert transformation";
142     }
143     if (opt_log) {
144       im_in.log (im_out);
145       histString = "Logrithmic transformation";
146     }
147     if (opt_exp) {
148       im_in.exp (im_out);
149       histString = "Exponential transformation";
150     }
151     if (opt_sqr) {
152       im_in.square (im_out);
153       histString = "Square transformation";
154     }
155     if (opt_sqrt) {
156       im_in.sqrt (im_out);
157       histString = "Square root transformation";
158     }
159
160     im_out.labelsCopy (im_in);
161     im_out.labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
162     im_out.fileWrite (out_file);
163   }
164
165   return (0);
166 }
167
168 #ifndef NO_MAIN
169 int
170 main (int argc, char *const argv[])
171 {
172   int retval = 1;
173
174   try {
175     retval = if1_main(argc, argv);
176   } catch (exception e) {
177     std::cerr << "Exception: " << e.what() << std::endl;
178   } catch (...) {
179     std::cerr << "Unknown exception" << std::endl;
180   }
181
182   return (retval);
183 }
184 #endif
185