87603323b143673c0fd248bd3fc38120e01cc5dd
[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-2000 Kevin Rosenberg
11 **
12 **  $Id: ifexport.cpp,v 1.3 2001/01/09 22:31:47 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29
30
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 };
33
34 static struct option my_options[] =
35 {
36   {"scale", 1, 0, O_SCALE},
37   {"min", 1, 0, O_MIN},
38   {"max", 1, 0, O_MAX},
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},
48   {0, 0, 0, 0}
49 };
50
51 static const char* g_szIdStr = "$Id: ifexport.cpp,v 1.3 2001/01/09 22:31:47 kevin Exp $";
52
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";
60
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";
65
66 enum { O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC };
67 static const char O_FORMAT_PNG_STR[]="png" ;
68 static const char O_FORMAT_PNG16_STR[]="png16" ;
69 static const char O_FORMAT_PGM_STR[]="pgm";
70 static const char O_FORMAT_PGMASC_STR[]="pgmasc";
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 #ifdef HAVE_PNG
84   std::cout << "         png    PNG (8-bit) format\n";
85   std::cout << "         png16  PNG (16-bit) format\n";
86 #endif
87   std::cout << "     --center   Center of window\n";
88   std::cout << "         median Median is center of window (default)\n";
89   std::cout << "         mode   Mode is center of window\n";
90   std::cout << "         mean   Mean is center of window\n";
91   std::cout << "     --auto     Set auto window\n";
92   std::cout << "         full   Use full window (default)\n";
93   std::cout << "         std0.1 Use 0.1 standard deviation about center\n";
94   std::cout << "         std0.5 Use 0.5 standard deviation about center\n";
95   std::cout << "         std1   Use one standard deviation about center\n";
96   std::cout << "         std2   Use two standard deviations about center\n";
97   std::cout << "         std3   Use three standard deviations about center\n";
98   std::cout << "     --scale    Scaling factor for output size\n";
99   std::cout << "     --min      Set minimum intensity\n";
100   std::cout << "     --max      Set maximum intensity\n";
101   std::cout << "     --stats    Print image statistics\n";
102   std::cout << "     --labels   Print image labels\n";
103   std::cout << "     --debug    Set debug mode\n";
104   std::cout << "     --verbose  Set verbose mode\n";
105   std::cout << "     --version  Print version\n";
106   std::cout << "     --help     Print this help message\n";
107 }
108
109
110 int 
111 ifexport_main (int argc, char *const argv[])
112 {
113   ImageFile* pim = NULL;
114   double densmin = HUGE_VAL, densmax = -HUGE_VAL;
115   char *in_file, *out_file;
116   int opt_verbose = 0;
117   int opt_scale = 1;
118   int opt_auto = O_AUTO_FULL;
119   int opt_set_max = 0;
120   int opt_set_min = 0;
121   int opt_stats = 0;
122   int opt_debug = 0;
123   int opt_center = O_CENTER_MEDIAN;
124   int opt_format = O_FORMAT_PGM;
125   int opt_labels = 0;
126
127   while (1)
128     {
129       int c = getopt_long (argc, argv, "", my_options, NULL);
130       char *endptr = NULL;
131       char *endstr;
132       
133       if (c == -1)
134         break;
135       
136       switch (c)
137         {
138         case O_MIN:
139           opt_set_min = 1;
140           densmin = strtod(optarg, &endptr);
141           endstr = optarg + strlen(optarg);
142           if (endptr != endstr)
143             {
144               sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
145               ifexport_usage(argv[0]);
146               return (1);
147             }
148           break;
149         case O_MAX:
150           opt_set_max = 1;
151           densmax = strtod(optarg, &endptr);
152           endstr = optarg + strlen(optarg);
153           if (endptr != endstr)
154             {
155               sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
156               ifexport_usage(argv[0]);
157               return (1);
158             }
159           break;
160         case O_SCALE:
161           opt_scale = strtol(optarg, &endptr, 10);
162           endstr = optarg + strlen(optarg);
163           if (endptr != endstr)
164             {
165               sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
166               ifexport_usage(argv[0]);
167               return (1);
168             }
169           break;
170         case O_AUTO:
171           if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
172             opt_auto = O_AUTO_FULL;
173           else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
174             opt_auto = O_AUTO_STD1;
175           else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
176             opt_auto = O_AUTO_STD0_5;
177           else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
178             opt_auto = O_AUTO_STD0_1;
179           else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
180             opt_auto = O_AUTO_STD2;
181           else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
182             opt_auto = O_AUTO_STD3;
183           else
184             {
185               sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
186               ifexport_usage(argv[0]);
187               return (1);
188             }
189                 break;
190         case O_CENTER:
191           if (strcmp(optarg, O_CENTER_MEDIAN_STR) == 0)
192             opt_center = O_CENTER_MEDIAN;
193           else if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
194             opt_center = O_CENTER_MEAN;
195           else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
196             opt_center = O_CENTER_MODE;
197           else
198             {
199               sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
200               ifexport_usage(argv[0]);
201               return (1);
202             }
203           break;
204         case O_FORMAT:
205           if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
206             opt_format = O_FORMAT_PGM;
207           else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
208             opt_format = O_FORMAT_PGMASC;
209 #if HAVE_PNG
210           else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
211             opt_format = O_FORMAT_PNG;
212           else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
213             opt_format = O_FORMAT_PNG16;
214 #endif
215 #if HAVE_GIF
216           else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
217             opt_format = O_FORMAT_GIF;
218 #endif
219   else {
220               sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
221               ifexport_usage(argv[0]);
222               return (1);
223             }
224           break;
225         case O_LABELS:
226           opt_labels = 1;
227           break;
228         case O_VERBOSE:
229           opt_verbose = 1;
230           break;
231         case O_DEBUG:
232           opt_debug = 1;
233           break;
234         case O_STATS:
235           opt_stats = 1;
236           break;
237         case O_VERSION:
238 #ifdef VERSION
239           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
240 #else
241           std::cout << "Unknown version number\n";
242 #endif
243           return (0);
244         case O_HELP:
245         case '?':
246           ifexport_usage(argv[0]);
247           return (0);
248         default:
249           ifexport_usage(argv[0]);
250           return (1);
251         }
252     }
253
254   if (optind + 2 != argc) {
255     ifexport_usage(argv[0]);
256     return (1);
257   }
258   
259   in_file = argv[optind];
260   out_file = argv[optind+1];
261
262   pim = new ImageFile ();
263   ImageFile& im = *pim;
264   if (! im.fileRead(in_file)) {
265     sys_error (ERR_FATAL, "File %s does not exist", in_file);
266     return (1);
267   }
268
269   if (opt_labels)
270     im.printLabels(std::cout);
271
272   if (opt_stats || (! (opt_set_max && opt_set_min))) {
273       double min, max, mean, mode, median, stddev;
274       double window = 0;
275       im.statistics(min, max, mean, mode, median, stddev);
276     
277       if (opt_auto == O_AUTO_FULL) {
278         if (! opt_set_max)
279             densmax = max;
280         if (! opt_set_min)
281             densmin = min;
282       }
283       if (opt_stats || opt_auto != O_AUTO_FULL) {
284
285           if (opt_auto == O_AUTO_FULL)
286               ;
287           else if (opt_auto == O_AUTO_STD1)
288               window = stddev;
289           else if (opt_auto == O_AUTO_STD0_1)
290               window = stddev * 0.1;
291           else if (opt_auto == O_AUTO_STD0_5)
292               window = stddev * 0.5;
293           else if (opt_auto == O_AUTO_STD2)
294               window = stddev * 2;
295           else if (opt_auto == O_AUTO_STD3)
296               window = stddev * 3;
297           else {
298               sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
299               return (1);
300           }
301       }
302       if (opt_stats) {
303           std::cout <<"nx: " << im.nx() << std::endl;
304           std::cout <<"ny: " << im.ny() << std::endl;
305           std::cout <<"min: " << min << std::endl;
306           std::cout <<"max: " << max << std::endl;
307           std::cout <<"mean: " << mean << std::endl;
308           std::cout <<"mode: " << mode << std::endl;
309           std::cout <<"stddev: " << stddev << std::endl;
310       }
311       if (opt_auto != O_AUTO_FULL) {
312           double center;
313           
314           if (opt_center == O_CENTER_MEDIAN)
315               center = median;
316           else if (opt_center == O_CENTER_MODE)
317               center = mode;
318           else if (opt_center == O_CENTER_MEAN)
319               center = mean;
320           else {
321               sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
322               return (1);
323           }
324           if (! opt_set_max)
325               densmax = center + window;
326           if (! opt_set_min)
327               densmin = center - window;
328       }
329   }
330   
331   if (opt_stats) {
332     std::cout << "min display: " << densmin << std::endl;
333     std::cout << "max display: " << densmax << std::endl;
334   }
335   
336   if (opt_format == O_FORMAT_PGM)
337     im.writeImagePGM (out_file, opt_scale, opt_scale, densmin, densmax);
338   else if (opt_format == O_FORMAT_PGMASC)
339     im.writeImagePGMASCII (out_file, opt_scale, opt_scale, densmin, densmax);
340 #if HAVE_PNG
341   else if (opt_format == O_FORMAT_PNG)
342     im.writeImagePNG (out_file, 8, opt_scale, opt_scale, densmin, densmax);
343   else if (opt_format == O_FORMAT_PNG16)
344     im.writeImagePNG (out_file, 16, opt_scale, opt_scale, densmin, densmax);
345 #endif
346 #if HAVE_GIF
347   else if (opt_format == O_FORMAT_GIF) 
348     im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
349 #endif
350   else
351     {
352       sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
353       return (1);
354     }
355   return (0);
356 }
357
358
359 #ifndef NO_MAIN
360 int 
361 main (int argc, char *const argv[])
362 {
363   int retval = 1;
364
365   try {
366     retval = ifexport_main(argc, argv);
367   } catch (exception e) {
368           std::cerr << "Exception: " << e.what() << std::endl;
369   } catch (...) {
370           std::cerr << "Unknown exception\n";
371   }
372
373   return (retval);
374 }
375 #endif