Fix all remaining compiler warnings
[ctsim.git] / tools / ifexport.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ifexport.cpp
5 **   Purpose:       Convert an image file to a viewable image
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  April 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2010 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 #include "ct.h"
27
28
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 };
31
32 static struct option my_options[] =
33 {
34   {"scale", 1, 0, O_SCALE},
35   {"min", 1, 0, O_MIN},
36   {"max", 1, 0, O_MAX},
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},
46   {0, 0, 0, 0}
47 };
48
49 static const char* g_szIdStr = "$Id$";
50
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";
58
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";
63
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";
71
72 void
73 ifexport_usage (const char *program)
74 {
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";
85 #ifdef HAVE_PNG
86   std::cout << "         png    PNG (8-bit) format\n";
87   std::cout << "         png16  PNG (16-bit) format\n";
88 #endif
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";
109 }
110
111
112 int
113 ifexport_main (int argc, char *const argv[])
114 {
115   ImageFile* pim = NULL;
116   double densmin = HUGE_VAL, densmax = -HUGE_VAL;
117   char *in_file, *out_file;
118   int opt_verbose = 0;
119   int opt_scale = 1;
120   int opt_auto = O_AUTO_FULL;
121   int opt_set_max = 0;
122   int opt_set_min = 0;
123   int opt_stats = 0;
124   int opt_debug = 0;
125   int opt_center = O_CENTER_MEDIAN;
126   int opt_format = O_FORMAT_PGM;
127   int opt_labels = 0;
128   UNUSED(opt_verbose);
129   UNUSED(opt_debug);
130   
131   while (1)
132     {
133       int c = getopt_long (argc, argv, "", my_options, NULL);
134       char *endptr = NULL;
135       char *endstr;
136
137       if (c == -1)
138         break;
139
140       switch (c)
141         {
142         case O_MIN:
143           opt_set_min = 1;
144           densmin = strtod(optarg, &endptr);
145           endstr = optarg + strlen(optarg);
146           if (endptr != endstr)
147             {
148               sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
149               ifexport_usage(argv[0]);
150               return (1);
151             }
152           break;
153         case O_MAX:
154           opt_set_max = 1;
155           densmax = strtod(optarg, &endptr);
156           endstr = optarg + strlen(optarg);
157           if (endptr != endstr)
158             {
159               sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
160               ifexport_usage(argv[0]);
161               return (1);
162             }
163           break;
164         case O_SCALE:
165           opt_scale = strtol(optarg, &endptr, 10);
166           endstr = optarg + strlen(optarg);
167           if (endptr != endstr)
168             {
169               sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
170               ifexport_usage(argv[0]);
171               return (1);
172             }
173           break;
174         case O_AUTO:
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;
187           else
188             {
189               sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
190               ifexport_usage(argv[0]);
191               return (1);
192             }
193                 break;
194         case O_CENTER:
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;
201           else
202             {
203               sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
204               ifexport_usage(argv[0]);
205               return (1);
206             }
207           break;
208         case O_FORMAT:
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;
213 #if HAVE_PNG
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;
218 #endif
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;
223 #if HAVE_GIF
224           else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
225             opt_format = O_FORMAT_GIF;
226 #endif
227   else {
228               sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
229               ifexport_usage(argv[0]);
230               return (1);
231             }
232           break;
233         case O_LABELS:
234           opt_labels = 1;
235           break;
236         case O_VERBOSE:
237           opt_verbose = 1;
238           break;
239         case O_DEBUG:
240           opt_debug = 1;
241           break;
242         case O_STATS:
243           opt_stats = 1;
244           break;
245         case O_VERSION:
246 #ifdef VERSION
247           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
248 #else
249           std::cout << "Unknown version number\n";
250 #endif
251           return (0);
252         case O_HELP:
253         case '?':
254           ifexport_usage(argv[0]);
255           return (0);
256         default:
257           ifexport_usage(argv[0]);
258           return (1);
259         }
260     }
261
262   if (optind + 2 != argc) {
263     ifexport_usage(argv[0]);
264     return (1);
265   }
266
267   in_file = argv[optind];
268   out_file = argv[optind+1];
269
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);
274     return (1);
275   }
276
277   if (opt_labels)
278     im.printLabels(std::cout);
279
280   if (opt_stats || (! (opt_set_max && opt_set_min))) {
281       double min, max, mean, mode, median, stddev;
282       double window = 0;
283       im.statistics(min, max, mean, mode, median, stddev);
284
285       if (opt_auto == O_AUTO_FULL) {
286         if (! opt_set_max)
287             densmax = max;
288         if (! opt_set_min)
289             densmin = min;
290       }
291       if (opt_stats || opt_auto != O_AUTO_FULL) {
292
293           if (opt_auto == O_AUTO_FULL)
294               ;
295           else if (opt_auto == O_AUTO_STD1)
296               window = stddev;
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)
302               window = stddev * 2;
303           else if (opt_auto == O_AUTO_STD3)
304               window = stddev * 3;
305           else {
306               sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
307               return (1);
308           }
309       }
310       if (opt_stats) {
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;
318       }
319       if (opt_auto != O_AUTO_FULL) {
320           double center;
321
322           if (opt_center == O_CENTER_MEDIAN)
323               center = median;
324           else if (opt_center == O_CENTER_MODE)
325               center = mode;
326           else if (opt_center == O_CENTER_MEAN)
327               center = mean;
328           else {
329               sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
330               return (1);
331           }
332           if (! opt_set_max)
333               densmax = center + window;
334           if (! opt_set_min)
335               densmin = center - window;
336       }
337   }
338
339   if (opt_stats) {
340     std::cout << "min display: " << densmin << std::endl;
341     std::cout << "max display: " << densmax << std::endl;
342   }
343
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);
348 #if HAVE_PNG
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);
353 #endif
354 #if HAVE_GIF
355   else if (opt_format == O_FORMAT_GIF)
356     im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
357 #endif
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);
362   else
363     {
364       sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
365       return (1);
366     }
367   return (0);
368 }
369
370
371 #ifndef NO_MAIN
372 int
373 main (int argc, char *const argv[])
374 {
375   int retval = 1;
376
377   try {
378     retval = ifexport_main(argc, argv);
379   } catch (exception e) {
380           std::cerr << "Exception: " << e.what() << std::endl;
381   } catch (...) {
382           std::cerr << "Unknown exception\n";
383   }
384
385   return (retval);
386 }
387 #endif