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