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-2000 Kevin Rosenberg
12 ** $Id: ifexport.cpp,v 1.2 2000/12/17 23:00:33 kevin Exp $
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.
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.
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 ******************************************************************************/
31 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
32 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
34 static struct option my_options[] =
36 {"scale", 1, 0, O_SCALE},
39 {"auto", 1, 0, O_AUTO},
40 {"center", 1, 0, O_CENTER},
41 {"format", 1, 0, O_FORMAT},
42 {"labels", 0, 0, O_LABELS},
43 {"stats", 0, 0, O_STATS},
44 {"verbose", 0, 0, O_VERBOSE},
45 {"debug", 0, 0, O_DEBUG},
46 {"help", 0, 0, O_HELP},
47 {"version", 0, 0, O_VERSION},
51 static const char* g_szIdStr = "$Id: ifexport.cpp,v 1.2 2000/12/17 23:00:33 kevin Exp $";
53 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
54 static const char O_AUTO_FULL_STR[]="full";
55 static const char O_AUTO_STD0_1_STR[]="std0.1";
56 static const char O_AUTO_STD0_5_STR[]="std0.5";
57 static const char O_AUTO_STD1_STR[]="std1";
58 static const char O_AUTO_STD2_STR[]="std2";
59 static const char O_AUTO_STD3_STR[]="std3";
61 enum { O_CENTER_MEDIAN, O_CENTER_MEAN, O_CENTER_MODE };
62 static const char O_CENTER_MEAN_STR[]="mean";
63 static const char O_CENTER_MODE_STR[]="mode";
64 static const char O_CENTER_MEDIAN_STR[]="median";
66 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
67 static const char O_FORMAT_GIF_STR[]="gif";
68 static const char O_FORMAT_PNG_STR[]="png" ;
69 static const char O_FORMAT_PNG16_STR[]="png16" ;
70 static const char O_FORMAT_PGM_STR[]="pgm";
71 static const char O_FORMAT_PGMASC_STR[]="pgmasc";
72 static const char O_FORMAT_DISP_STR[]="disp";
75 ifexport_usage (const char *program)
77 std::cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]\n";
78 std::cout << "Convert IF file to an image file\n";
79 std::cout << std::endl;
80 std::cout << " ifname Name of input file\n";
81 std::cout << " outfile Name of output file\n";
82 std::cout << " --format Output format\n";
83 std::cout << " pgm PGM (portable graymap) format (default)\n";
84 std::cout << " pgmasc PGM (portable graymap) ASCII format\n";
86 std::cout << " png PNG (8-bit) format\n";
87 std::cout << " png16 PNG (16-bit) format\n";
90 std::cout << " gif GIF format\n";
92 std::cout << " disp Display on screen\n";
93 std::cout << " --center Center of window\n";
94 std::cout << " median Median is center of window (default)\n";
95 std::cout << " mode Mode is center of window\n";
96 std::cout << " mean Mean is center of window\n";
97 std::cout << " --auto Set auto window\n";
98 std::cout << " full Use full window (default)\n";
99 std::cout << " std0.1 Use 0.1 standard deviation about center\n";
100 std::cout << " std0.5 Use 0.5 standard deviation about center\n";
101 std::cout << " std1 Use one standard deviation about center\n";
102 std::cout << " std2 Use two standard deviations about center\n";
103 std::cout << " std3 Use three standard deviations about center\n";
104 std::cout << " --scale Scaling factor for output size\n";
105 std::cout << " --min Set minimum intensity\n";
106 std::cout << " --max Set maximum intensity\n";
107 std::cout << " --stats Print image statistics\n";
108 std::cout << " --labels Print image labels\n";
109 std::cout << " --debug Set debug mode\n";
110 std::cout << " --verbose Set verbose mode\n";
111 std::cout << " --version Print version\n";
112 std::cout << " --help Print this help message\n";
117 ifexport_main (int argc, char *const argv[])
119 ImageFile* pim = NULL;
120 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
121 char *in_file, *out_file;
124 int opt_auto = O_AUTO_FULL;
129 int opt_center = O_CENTER_MEDIAN;
130 int opt_format = O_FORMAT_PGM;
135 int c = getopt_long (argc, argv, "", my_options, NULL);
146 densmin = strtod(optarg, &endptr);
147 endstr = optarg + strlen(optarg);
148 if (endptr != endstr)
150 sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
151 ifexport_usage(argv[0]);
157 densmax = strtod(optarg, &endptr);
158 endstr = optarg + strlen(optarg);
159 if (endptr != endstr)
161 sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
162 ifexport_usage(argv[0]);
167 opt_scale = strtol(optarg, &endptr, 10);
168 endstr = optarg + strlen(optarg);
169 if (endptr != endstr)
171 sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
172 ifexport_usage(argv[0]);
177 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
178 opt_auto = O_AUTO_FULL;
179 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
180 opt_auto = O_AUTO_STD1;
181 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
182 opt_auto = O_AUTO_STD0_5;
183 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
184 opt_auto = O_AUTO_STD0_1;
185 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
186 opt_auto = O_AUTO_STD2;
187 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
188 opt_auto = O_AUTO_STD3;
191 sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
192 ifexport_usage(argv[0]);
197 if (strcmp(optarg, O_CENTER_MEDIAN_STR) == 0)
198 opt_center = O_CENTER_MEDIAN;
199 else if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
200 opt_center = O_CENTER_MEAN;
201 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
202 opt_center = O_CENTER_MODE;
205 sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
206 ifexport_usage(argv[0]);
211 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
212 opt_format = O_FORMAT_PGM;
213 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
214 opt_format = O_FORMAT_PGMASC;
216 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
217 opt_format = O_FORMAT_PNG;
218 else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
219 opt_format = O_FORMAT_PNG16;
222 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
223 opt_format = O_FORMAT_GIF;
225 else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
226 opt_format = O_FORMAT_DISP;
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]);
263 if (optind + 1 == argc)
264 opt_format = O_FORMAT_DISP; // display image if no options
267 if ((opt_format == O_FORMAT_DISP && optind + 1 != argc)
268 || (opt_format != O_FORMAT_DISP && optind + 2 != argc)) {
269 ifexport_usage(argv[0]);
273 in_file = argv[optind];
275 if (opt_format != O_FORMAT_DISP)
276 out_file = argv[optind+1];
277 else out_file = NULL;
279 pim = new ImageFile ();
280 ImageFile& im = *pim;
281 if (! im.fileRead(in_file)) {
282 sys_error (ERR_FATAL, "File %s does not exist", in_file);
287 im.printLabels(std::cout);
289 if (opt_stats || (! (opt_set_max && opt_set_min))) {
290 double min, max, mean, mode, median, stddev;
292 im.statistics(min, max, mean, mode, median, stddev);
294 if (opt_auto == O_AUTO_FULL) {
300 if (opt_stats || opt_auto != O_AUTO_FULL) {
302 if (opt_auto == O_AUTO_FULL)
304 else if (opt_auto == O_AUTO_STD1)
306 else if (opt_auto == O_AUTO_STD0_1)
307 window = stddev * 0.1;
308 else if (opt_auto == O_AUTO_STD0_5)
309 window = stddev * 0.5;
310 else if (opt_auto == O_AUTO_STD2)
312 else if (opt_auto == O_AUTO_STD3)
315 sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
320 std::cout <<"nx: " << im.nx() << std::endl;
321 std::cout <<"ny: " << im.ny() << std::endl;
322 std::cout <<"min: " << min << std::endl;
323 std::cout <<"max: " << max << std::endl;
324 std::cout <<"mean: " << mean << std::endl;
325 std::cout <<"mode: " << mode << std::endl;
326 std::cout <<"stddev: " << stddev << std::endl;
328 if (opt_auto != O_AUTO_FULL) {
331 if (opt_center == O_CENTER_MEDIAN)
333 else if (opt_center == O_CENTER_MODE)
335 else if (opt_center == O_CENTER_MEAN)
338 sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
342 densmax = center + window;
344 densmin = center - window;
349 std::cout << "min display: " << densmin << std::endl;
350 std::cout << "max display: " << densmax << std::endl;
353 if (opt_format == O_FORMAT_PGM)
354 im.writeImagePGM (out_file, opt_scale, opt_scale, densmin, densmax);
355 else if (opt_format == O_FORMAT_PGMASC)
356 im.writeImagePGMASCII (out_file, opt_scale, opt_scale, densmin, densmax);
358 else if (opt_format == O_FORMAT_PNG)
359 im.writeImagePNG (out_file, 8, opt_scale, opt_scale, densmin, densmax);
360 else if (opt_format == O_FORMAT_PNG16)
361 im.writeImagePNG (out_file, 16, opt_scale, opt_scale, densmin, densmax);
364 else if (opt_format == O_FORMAT_GIF)
365 im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
367 else if (opt_format == O_FORMAT_DISP) {
369 im.displayScaling (opt_scale, densmin, densmax);
370 std::cout << "Press enter to continue\n";
376 sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
385 main (int argc, char *const argv[])
390 retval = ifexport_main(argc, argv);
391 } catch (exception e) {
392 std::cerr << "Exception: " << e.what() << std::endl;
394 std::cerr << "Unknown exception\n";