Version 5.2.0: Add text ifexport option
[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
129   while (1)
130     {
131       int c = getopt_long (argc, argv, "", my_options, NULL);
132       char *endptr = NULL;
133       char *endstr;
134
135       if (c == -1)
136         break;
137
138       switch (c)
139         {
140         case O_MIN:
141           opt_set_min = 1;
142           densmin = strtod(optarg, &endptr);
143           endstr = optarg + strlen(optarg);
144           if (endptr != endstr)
145             {
146               sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
147               ifexport_usage(argv[0]);
148               return (1);
149             }
150           break;
151         case O_MAX:
152           opt_set_max = 1;
153           densmax = strtod(optarg, &endptr);
154           endstr = optarg + strlen(optarg);
155           if (endptr != endstr)
156             {
157               sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
158               ifexport_usage(argv[0]);
159               return (1);
160             }
161           break;
162         case O_SCALE:
163           opt_scale = strtol(optarg, &endptr, 10);
164           endstr = optarg + strlen(optarg);
165           if (endptr != endstr)
166             {
167               sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
168               ifexport_usage(argv[0]);
169               return (1);
170             }
171           break;
172         case O_AUTO:
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;
185           else
186             {
187               sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
188               ifexport_usage(argv[0]);
189               return (1);
190             }
191                 break;
192         case O_CENTER:
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;
199           else
200             {
201               sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
202               ifexport_usage(argv[0]);
203               return (1);
204             }
205           break;
206         case O_FORMAT:
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;
211 #if HAVE_PNG
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;
216 #endif
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;
221 #if HAVE_GIF
222           else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
223             opt_format = O_FORMAT_GIF;
224 #endif
225   else {
226               sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
227               ifexport_usage(argv[0]);
228               return (1);
229             }
230           break;
231         case O_LABELS:
232           opt_labels = 1;
233           break;
234         case O_VERBOSE:
235           opt_verbose = 1;
236           break;
237         case O_DEBUG:
238           opt_debug = 1;
239           break;
240         case O_STATS:
241           opt_stats = 1;
242           break;
243         case O_VERSION:
244 #ifdef VERSION
245           std::cout << "Version " << VERSION << std::endl << g_szIdStr << std::endl;
246 #else
247           std::cout << "Unknown version number\n";
248 #endif
249           return (0);
250         case O_HELP:
251         case '?':
252           ifexport_usage(argv[0]);
253           return (0);
254         default:
255           ifexport_usage(argv[0]);
256           return (1);
257         }
258     }
259
260   if (optind + 2 != argc) {
261     ifexport_usage(argv[0]);
262     return (1);
263   }
264
265   in_file = argv[optind];
266   out_file = argv[optind+1];
267
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);
272     return (1);
273   }
274
275   if (opt_labels)
276     im.printLabels(std::cout);
277
278   if (opt_stats || (! (opt_set_max && opt_set_min))) {
279       double min, max, mean, mode, median, stddev;
280       double window = 0;
281       im.statistics(min, max, mean, mode, median, stddev);
282
283       if (opt_auto == O_AUTO_FULL) {
284         if (! opt_set_max)
285             densmax = max;
286         if (! opt_set_min)
287             densmin = min;
288       }
289       if (opt_stats || opt_auto != O_AUTO_FULL) {
290
291           if (opt_auto == O_AUTO_FULL)
292               ;
293           else if (opt_auto == O_AUTO_STD1)
294               window = stddev;
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)
300               window = stddev * 2;
301           else if (opt_auto == O_AUTO_STD3)
302               window = stddev * 3;
303           else {
304               sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
305               return (1);
306           }
307       }
308       if (opt_stats) {
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;
316       }
317       if (opt_auto != O_AUTO_FULL) {
318           double center;
319
320           if (opt_center == O_CENTER_MEDIAN)
321               center = median;
322           else if (opt_center == O_CENTER_MODE)
323               center = mode;
324           else if (opt_center == O_CENTER_MEAN)
325               center = mean;
326           else {
327               sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
328               return (1);
329           }
330           if (! opt_set_max)
331               densmax = center + window;
332           if (! opt_set_min)
333               densmin = center - window;
334       }
335   }
336
337   if (opt_stats) {
338     std::cout << "min display: " << densmin << std::endl;
339     std::cout << "max display: " << densmax << std::endl;
340   }
341
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);
346 #if HAVE_PNG
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);
351 #endif
352 #if HAVE_GIF
353   else if (opt_format == O_FORMAT_GIF)
354     im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
355 #endif
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);
360   else
361     {
362       sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
363       return (1);
364     }
365   return (0);
366 }
367
368
369 #ifndef NO_MAIN
370 int
371 main (int argc, char *const argv[])
372 {
373   int retval = 1;
374
375   try {
376     retval = ifexport_main(argc, argv);
377   } catch (exception e) {
378           std::cerr << "Exception: " << e.what() << std::endl;
379   } catch (...) {
380           std::cerr << "Unknown exception\n";
381   }
382
383   return (retval);
384 }
385 #endif