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;
131 int c = getopt_long (argc, argv, "", my_options, NULL);
142 densmin = strtod(optarg, &endptr);
143 endstr = optarg + strlen(optarg);
144 if (endptr != endstr)
146 sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
147 ifexport_usage(argv[0]);
153 densmax = strtod(optarg, &endptr);
154 endstr = optarg + strlen(optarg);
155 if (endptr != endstr)
157 sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
158 ifexport_usage(argv[0]);
163 opt_scale = strtol(optarg, &endptr, 10);
164 endstr = optarg + strlen(optarg);
165 if (endptr != endstr)
167 sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
168 ifexport_usage(argv[0]);
173 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
174 opt_auto = O_AUTO_FULL;
175 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
176 opt_auto = O_AUTO_STD1;
177 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
178 opt_auto = O_AUTO_STD0_5;
179 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
180 opt_auto = O_AUTO_STD0_1;
181 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
182 opt_auto = O_AUTO_STD2;
183 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
184 opt_auto = O_AUTO_STD3;
187 sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
188 ifexport_usage(argv[0]);
193 if (strcmp(optarg, O_CENTER_MEDIAN_STR) == 0)
194 opt_center = O_CENTER_MEDIAN;
195 else if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
196 opt_center = O_CENTER_MEAN;
197 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
198 opt_center = O_CENTER_MODE;
201 sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
202 ifexport_usage(argv[0]);
207 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
208 opt_format = O_FORMAT_PGM;
209 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
210 opt_format = O_FORMAT_PGMASC;
212 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
213 opt_format = O_FORMAT_PNG;
214 else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
215 opt_format = O_FORMAT_PNG16;
217 else if (strcmp(optarg, O_FORMAT_TEXT_STR) == 0)
218 opt_format = O_FORMAT_TEXT;
219 else if (strcmp(optarg, O_FORMAT_RAW_STR) == 0)
220 opt_format = O_FORMAT_RAW;
222 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
223 opt_format = O_FORMAT_GIF;
226 sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
227 ifexport_usage(argv[0]);
245 std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
247 std::cout << "Unknown version number\n";
252 ifexport_usage(argv[0]);
255 ifexport_usage(argv[0]);
260 if (optind + 2 != argc) {
261 ifexport_usage(argv[0]);
265 in_file = argv[optind];
266 out_file = argv[optind+1];
268 pim = new ImageFile ();
269 ImageFile& im = *pim;
270 if (! im.fileRead(in_file)) {
271 sys_error (ERR_FATAL, "File %s does not exist", in_file);
276 im.printLabels(std::cout);
278 if (opt_stats || (! (opt_set_max && opt_set_min))) {
279 double min, max, mean, mode, median, stddev;
281 im.statistics(min, max, mean, mode, median, stddev);
283 if (opt_auto == O_AUTO_FULL) {
289 if (opt_stats || opt_auto != O_AUTO_FULL) {
291 if (opt_auto == O_AUTO_FULL)
293 else if (opt_auto == O_AUTO_STD1)
295 else if (opt_auto == O_AUTO_STD0_1)
296 window = stddev * 0.1;
297 else if (opt_auto == O_AUTO_STD0_5)
298 window = stddev * 0.5;
299 else if (opt_auto == O_AUTO_STD2)
301 else if (opt_auto == O_AUTO_STD3)
304 sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
309 std::cout <<"nx: " << im.nx() << std::endl;
310 std::cout <<"ny: " << im.ny() << std::endl;
311 std::cout <<"min: " << min << std::endl;
312 std::cout <<"max: " << max << std::endl;
313 std::cout <<"mean: " << mean << std::endl;
314 std::cout <<"mode: " << mode << std::endl;
315 std::cout <<"stddev: " << stddev << std::endl;
317 if (opt_auto != O_AUTO_FULL) {
320 if (opt_center == O_CENTER_MEDIAN)
322 else if (opt_center == O_CENTER_MODE)
324 else if (opt_center == O_CENTER_MEAN)
327 sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
331 densmax = center + window;
333 densmin = center - window;
338 std::cout << "min display: " << densmin << std::endl;
339 std::cout << "max display: " << densmax << std::endl;
342 if (opt_format == O_FORMAT_PGM)
343 im.writeImagePGM (out_file, opt_scale, opt_scale, densmin, densmax);
344 else if (opt_format == O_FORMAT_PGMASC)
345 im.writeImagePGMASCII (out_file, opt_scale, opt_scale, densmin, densmax);
347 else if (opt_format == O_FORMAT_PNG)
348 im.writeImagePNG (out_file, 8, opt_scale, opt_scale, densmin, densmax);
349 else if (opt_format == O_FORMAT_PNG16)
350 im.writeImagePNG (out_file, 16, opt_scale, opt_scale, densmin, densmax);
353 else if (opt_format == O_FORMAT_GIF)
354 im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
356 else if (opt_format == O_FORMAT_TEXT)
357 im.writeImageText (out_file);
358 else if (opt_format == O_FORMAT_RAW)
359 im.writeImageRaw (out_file, opt_scale, opt_scale);
362 sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
371 main (int argc, char *const argv[])
376 retval = ifexport_main(argc, argv);
377 } catch (exception e) {
378 std::cerr << "Exception: " << e.what() << std::endl;
380 std::cerr << "Unknown exception\n";