Debian Codegraph @

tools/if1.cpp

raw
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 UNUSED(opt_verbose);
77
78 while (1)
79 {
80 int c = getopt_long (argc, argv, "", my_options, NULL);
81
82 if (c == -1)
83 break;
84
85 switch (c)
86 {
87 case O_INVERT:
88 opt_invert = 1;
89 break;
90 case O_LOG:
91 opt_log = 1;
92 break;
93 case O_SQR:
94 opt_sqr = 1;
95 break;
96 case O_SQRT:
97 opt_sqrt = 1;
98 break;
99 case O_EXP:
100 opt_exp = 1;
101 break;
102 case O_VERBOSE:
103 opt_verbose = 1;
104 break;
105 case O_VERSION:
106 #ifdef VERSION
107 std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
108 #else
109 std::cout << "Unknown version number" << std::endl;
110 #endif
111 return (0);
112 case O_HELP:
113 case '?':
114 if1_usage(argv[0]);
115 return (0);
116 default:
117 if1_usage(argv[0]);
118 return (1);
119 }
120 }
121
122 if (optind + 2 != argc)
123 {
124 if1_usage(argv[0]);
125 return (1);
126 }
127
128 in_file = argv[optind];
129 out_file = argv[optind + 1];
130
131 std::string histString;
132
133 if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
134 ImageFile im_in;
135 im_in.fileRead (in_file);
136 int nx = im_in.nx();
137 int ny = im_in.ny();
138 ImageFile im_out (nx, ny);
139
140 if (opt_invert) {
141 im_in.invertPixelValues (im_out);
142 histString = "Invert transformation";
143 }
144 if (opt_log) {
145 im_in.log (im_out);
146 histString = "Logrithmic transformation";
147 }
148 if (opt_exp) {
149 im_in.exp (im_out);
150 histString = "Exponential transformation";
151 }
152 if (opt_sqr) {
153 im_in.square (im_out);
154 histString = "Square transformation";
155 }
156 if (opt_sqrt) {
157 im_in.sqrt (im_out);
158 histString = "Square root transformation";
159 }
160
161 im_out.labelsCopy (im_in);
162 im_out.labelAdd (Array2dFileLabel::L_HISTORY, histString.c_str());
163 im_out.fileWrite (out_file);
164 }
165
166 return (0);
167 }
168
169 #ifndef NO_MAIN
170 int
171 main (int argc, char *const argv[])
172 {
173 int retval = 1;
174
175 try {
176 retval = if1_main(argc, argv);
177 } catch (exception e) {
178 std::cerr << "Exception: " << e.what() << std::endl;
179 } catch (...) {
180 std::cerr << "Unknown exception" << std::endl;
181 }
182
183 return (retval);
184 }
185 #endif
186