1 /*****************************************************************************
5 ** Purpose: Convert an image file to a viewable image
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: April 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2010 Kevin Rosenberg
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.
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.
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 ******************************************************************************/
29 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
30 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
32 static struct option my_options[] =
34 {"scale", 1, 0, O_SCALE},
37 {"auto", 1, 0, O_AUTO},
38 {"center", 1, 0, O_CENTER},
39 {"format", 1, 0, O_FORMAT},
40 {"labels", 0, 0, O_LABELS},
41 {"stats", 0, 0, O_STATS},
42 {"verbose", 0, 0, O_VERBOSE},
43 {"debug", 0, 0, O_DEBUG},
44 {"help", 0, 0, O_HELP},
45 {"version", 0, 0, O_VERSION},
49 static const char* g_szIdStr = "$Id$";
51 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
52 static const char O_AUTO_FULL_STR[]="full";
53 static const char O_AUTO_STD0_1_STR[]="std0.1";
54 static const char O_AUTO_STD0_5_STR[]="std0.5";
55 static const char O_AUTO_STD1_STR[]="std1";
56 static const char O_AUTO_STD2_STR[]="std2";
57 static const char O_AUTO_STD3_STR[]="std3";
59 enum { O_CENTER_MEDIAN, O_CENTER_MEAN, O_CENTER_MODE };
60 static const char O_CENTER_MEAN_STR[]="mean";
61 static const char O_CENTER_MODE_STR[]="mode";
62 static const char O_CENTER_MEDIAN_STR[]="median";
64 enum { O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_RAW, O_FORMAT_TEXT };
65 static const char O_FORMAT_PNG_STR[]="png" ;
66 static const char O_FORMAT_PNG16_STR[]="png16" ;
67 static const char O_FORMAT_PGM_STR[]="pgm";
68 static const char O_FORMAT_PGMASC_STR[]="pgmasc";
69 static const char O_FORMAT_TEXT_STR[]="text";
70 static const char O_FORMAT_RAW_STR[]="raw";
73 ifexport_usage (const char *program)
75 std::cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]\n";
76 std::cout << "Convert IF file to an image file\n";
77 std::cout << std::endl;
78 std::cout << " ifname Name of input file\n";
79 std::cout << " outfile Name of output file\n";
80 std::cout << " --format Output format\n";
81 std::cout << " pgm PGM (portable graymap) format (default)\n";
82 std::cout << " pgmasc PGM (portable graymap) ASCII format\n";
83 std::cout << " text Text format\n";
84 std::cout << " raw Raw format\n";
86 std::cout << " png PNG (8-bit) format\n";
87 std::cout << " png16 PNG (16-bit) format\n";
89 std::cout << " --center Center of window\n";
90 std::cout << " median Median is center of window (default)\n";
91 std::cout << " mode Mode is center of window\n";
92 std::cout << " mean Mean is center of window\n";
93 std::cout << " --auto Set auto window\n";
94 std::cout << " full Use full window (default)\n";
95 std::cout << " std0.1 Use 0.1 standard deviation about center\n";
96 std::cout << " std0.5 Use 0.5 standard deviation about center\n";
97 std::cout << " std1 Use one standard deviation about center\n";
98 std::cout << " std2 Use two standard deviations about center\n";
99 std::cout << " std3 Use three standard deviations about center\n";
100 std::cout << " --scale Scaling factor for output size\n";
101 std::cout << " --min Set minimum intensity\n";
102 std::cout << " --max Set maximum intensity\n";
103 std::cout << " --stats Print image statistics\n";
104 std::cout << " --labels Print image labels\n";
105 std::cout << " --debug Set debug mode\n";
106 std::cout << " --verbose Set verbose mode\n";
107 std::cout << " --version Print version\n";
108 std::cout << " --help Print this help message\n";
113 ifexport_main (int argc, char *const argv[])
115 ImageFile* pim = NULL;
116 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
117 char *in_file, *out_file;
120 int opt_auto = O_AUTO_FULL;
125 int opt_center = O_CENTER_MEDIAN;
126 int opt_format = O_FORMAT_PGM;
133 int c = getopt_long (argc, argv, "", my_options, NULL);
144 densmin = strtod(optarg, &endptr);
145 endstr = optarg + strlen(optarg);
146 if (endptr != endstr)
148 sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
149 ifexport_usage(argv[0]);
155 densmax = strtod(optarg, &endptr);
156 endstr = optarg + strlen(optarg);
157 if (endptr != endstr)
159 sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
160 ifexport_usage(argv[0]);
165 opt_scale = strtol(optarg, &endptr, 10);
166 endstr = optarg + strlen(optarg);
167 if (endptr != endstr)
169 sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
170 ifexport_usage(argv[0]);
175 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
176 opt_auto = O_AUTO_FULL;
177 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
178 opt_auto = O_AUTO_STD1;
179 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
180 opt_auto = O_AUTO_STD0_5;
181 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
182 opt_auto = O_AUTO_STD0_1;
183 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
184 opt_auto = O_AUTO_STD2;
185 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
186 opt_auto = O_AUTO_STD3;
189 sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
190 ifexport_usage(argv[0]);
195 if (strcmp(optarg, O_CENTER_MEDIAN_STR) == 0)
196 opt_center = O_CENTER_MEDIAN;
197 else if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
198 opt_center = O_CENTER_MEAN;
199 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
200 opt_center = O_CENTER_MODE;
203 sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
204 ifexport_usage(argv[0]);
209 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
210 opt_format = O_FORMAT_PGM;
211 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
212 opt_format = O_FORMAT_PGMASC;
214 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
215 opt_format = O_FORMAT_PNG;
216 else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
217 opt_format = O_FORMAT_PNG16;
219 else if (strcmp(optarg, O_FORMAT_TEXT_STR) == 0)
220 opt_format = O_FORMAT_TEXT;
221 else if (strcmp(optarg, O_FORMAT_RAW_STR) == 0)
222 opt_format = O_FORMAT_RAW;
224 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
225 opt_format = O_FORMAT_GIF;
228 sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
229 ifexport_usage(argv[0]);
247 std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
249 std::cout << "Unknown version number\n";
254 ifexport_usage(argv[0]);
257 ifexport_usage(argv[0]);
262 if (optind + 2 != argc) {
263 ifexport_usage(argv[0]);
267 in_file = argv[optind];
268 out_file = argv[optind+1];
270 pim = new ImageFile ();
271 ImageFile& im = *pim;
272 if (! im.fileRead(in_file)) {
273 sys_error (ERR_FATAL, "File %s does not exist", in_file);
278 im.printLabels(std::cout);
280 if (opt_stats || (! (opt_set_max && opt_set_min))) {
281 double min, max, mean, mode, median, stddev;
283 im.statistics(min, max, mean, mode, median, stddev);
285 if (opt_auto == O_AUTO_FULL) {
291 if (opt_stats || opt_auto != O_AUTO_FULL) {
293 if (opt_auto == O_AUTO_FULL)
295 else if (opt_auto == O_AUTO_STD1)
297 else if (opt_auto == O_AUTO_STD0_1)
298 window = stddev * 0.1;
299 else if (opt_auto == O_AUTO_STD0_5)
300 window = stddev * 0.5;
301 else if (opt_auto == O_AUTO_STD2)
303 else if (opt_auto == O_AUTO_STD3)
306 sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
311 std::cout <<"nx: " << im.nx() << std::endl;
312 std::cout <<"ny: " << im.ny() << std::endl;
313 std::cout <<"min: " << min << std::endl;
314 std::cout <<"max: " << max << std::endl;
315 std::cout <<"mean: " << mean << std::endl;
316 std::cout <<"mode: " << mode << std::endl;
317 std::cout <<"stddev: " << stddev << std::endl;
319 if (opt_auto != O_AUTO_FULL) {
322 if (opt_center == O_CENTER_MEDIAN)
324 else if (opt_center == O_CENTER_MODE)
326 else if (opt_center == O_CENTER_MEAN)
329 sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
333 densmax = center + window;
335 densmin = center - window;
340 std::cout << "min display: " << densmin << std::endl;
341 std::cout << "max display: " << densmax << std::endl;
344 if (opt_format == O_FORMAT_PGM)
345 im.writeImagePGM (out_file, opt_scale, opt_scale, densmin, densmax);
346 else if (opt_format == O_FORMAT_PGMASC)
347 im.writeImagePGMASCII (out_file, opt_scale, opt_scale, densmin, densmax);
349 else if (opt_format == O_FORMAT_PNG)
350 im.writeImagePNG (out_file, 8, opt_scale, opt_scale, densmin, densmax);
351 else if (opt_format == O_FORMAT_PNG16)
352 im.writeImagePNG (out_file, 16, opt_scale, opt_scale, densmin, densmax);
355 else if (opt_format == O_FORMAT_GIF)
356 im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
358 else if (opt_format == O_FORMAT_TEXT)
359 im.writeImageText (out_file);
360 else if (opt_format == O_FORMAT_RAW)
361 im.writeImageRaw (out_file, opt_scale, opt_scale);
364 sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
373 main (int argc, char *const argv[])
378 retval = ifexport_main(argc, argv);
379 } catch (exception e) {
380 std::cerr << "Exception: " << e.what() << std::endl;
382 std::cerr << "Unknown exception\n";