b00c1775f9f03093c8864d66a505b03ce53bc01e
[ctsim.git] / tools / if2img.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if2img.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: if2img.cpp,v 1.1 2000/07/13 07:01:35 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 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_MEAN, O_CENTER_MODE };
60 static const char O_CENTER_MEAN_STR[]="mean";
61 static const char O_CENTER_MODE_STR[]="mode";
62
63 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
64 static const char O_FORMAT_GIF_STR[]="gif";
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_DISP_STR[]="disp";
70
71 void 
72 if2img_usage (const char *program)
73 {
74   cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]" << endl;
75   cout << "Convert IF file to an image file" << endl;
76   cout << endl;
77   cout << "     ifname     Name of input file" << endl;
78   cout << "     outfile    Name of output file" << endl;
79   cout << "     --format   Output format" << endl;
80   cout << "         pgm    PGM (portable graymap) format (default)" << endl;
81   cout << "         pgmasc PGM (portable graymap) ASCII format" << endl;
82 #ifdef HAVE_PNG
83   cout << "         png    PNG (8-bit) format" << endl;
84   cout << "         png16  PNG (16-bit) format" << endl;
85 #endif
86 #if HAVE_G2
87   cout << "         gif    GIF format" << endl;
88 #endif
89   cout << "         disp   Display on screen" << endl;
90   cout << "     --center   Center of window" << endl;
91   cout << "         mode   Mode is center of window (default)" << endl;
92   cout << "         mean   Mean is center of window" << endl;
93   cout << "     --auto     Set auto window" << endl;
94   cout << "         full   Use full window (default)" << endl;
95   cout << "         std0.1 Use 0.1 standard deviation about center" << endl;
96   cout << "         std0.5 Use 0.5 standard deviation about center" << endl;
97   cout << "         std1   Use one standard deviation about center" << endl;
98   cout << "         std2   Use two standard deviations about center" << endl;
99   cout << "         std3   Use three standard deviations about center" << endl;
100   cout << "     --scale    Scaling factor for output size" << endl;
101   cout << "     --min      Set minimum intensity" << endl;
102   cout << "     --max      Set maximum intensity" << endl;
103   cout << "     --stats    Print image statistics" << endl;
104   cout << "     --labels   Print image labels" << endl;
105   cout << "     --debug    Set debug mode" << endl;
106   cout << "     --verbose  Set verbose mode" << endl;
107   cout << "     --version  Print version" << endl;
108   cout << "     --help     Print this help message" << endl;
109 }
110
111
112 int 
113 if2img_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_MODE;
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               if2img_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               if2img_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               if2img_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               if2img_usage(argv[0]);
189               return (1);
190             }
191                 break;
192         case O_CENTER:
193           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               if2img_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 if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
220             opt_format = O_FORMAT_DISP;
221           else {
222               sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
223               if2img_usage(argv[0]);
224               return (1);
225             }
226           break;
227         case O_LABELS:
228           opt_labels = 1;
229           break;
230         case O_VERBOSE:
231           opt_verbose = 1;
232           break;
233         case O_DEBUG:
234           opt_debug = 1;
235           break;
236         case O_STATS:
237           opt_stats = 1;
238           break;
239         case O_VERSION:
240 #ifdef VERSION
241           cout << "Version " << VERSION;
242 #else
243           cout << "Unknown version number" << endl;
244 #endif
245           return (0);
246         case O_HELP:
247         case '?':
248           if2img_usage(argv[0]);
249           return (0);
250         default:
251           if2img_usage(argv[0]);
252           return (1);
253         }
254     }
255
256   if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) 
257       || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
258     {
259         if2img_usage(argv[0]);
260         return (1);
261     }
262   
263   in_file = argv[optind];
264
265   if (opt_format != O_FORMAT_DISP)
266       out_file = argv[optind+1];
267   else out_file = NULL;
268
269   pim = new ImageFile ();
270   ImageFile& im = *pim;
271   if (! im.fileRead(in_file)) {
272     sys_error (ERR_FATAL, "File %s does not exist", in_file);
273     return (1);
274   }
275
276   if (opt_labels)
277     im.printLabels(cout);
278
279   if (opt_stats || (! (opt_set_max && opt_set_min))) {
280       double min, max, mean, mode, median, stddev;
281       double window = 0;
282       im.statistics(min, max, mean, mode, median, stddev);
283     
284       if (opt_auto == O_AUTO_FULL) {
285         if (! opt_set_max)
286             densmax = max;
287         if (! opt_set_min)
288             densmin = min;
289       }
290       if (opt_stats || opt_auto != O_AUTO_FULL) {
291
292           if (opt_auto == O_AUTO_FULL)
293               ;
294           else if (opt_auto == O_AUTO_STD1)
295               window = stddev;
296           else if (opt_auto == O_AUTO_STD0_1)
297               window = stddev * 0.1;
298           else if (opt_auto == O_AUTO_STD0_5)
299               window = stddev * 0.5;
300           else if (opt_auto == O_AUTO_STD2)
301               window = stddev * 2;
302           else if (opt_auto == O_AUTO_STD3)
303               window = stddev * 3;
304           else {
305               sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
306               return (1);
307           }
308       }
309       if (opt_stats) {
310           cout <<"nx: " << im.nx() << endl;
311           cout <<"ny: " << im.ny() << endl;
312           cout <<"min: " << min << endl;
313           cout <<"max: " << max << endl;
314           cout <<"mean: " << mean << endl;
315           cout <<"mode: " << mode << endl;
316           cout <<"stddev: " << stddev << endl;
317       }
318       if (opt_auto != O_AUTO_FULL) {
319           double center;
320           
321           if (opt_center == O_CENTER_MODE)
322               center = mode;
323           else if (opt_center == O_CENTER_MEAN)
324               center = mean;
325           else {
326               sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
327               return (1);
328           }
329           if (! opt_set_max)
330               densmax = center + window;
331           if (! opt_set_min)
332               densmin = center - window;
333       }
334   }
335   
336   if (opt_stats) {
337     cout << "min display: " << densmin << endl;
338     cout << "max display: " << densmax << endl;
339   }
340   
341   if (opt_format == O_FORMAT_PGM)
342     im.writeImagePGM (out_file, opt_scale, opt_scale, densmin, densmax);
343   else if (opt_format == O_FORMAT_PGMASC)
344     im.writeImagePGMASCII (out_file, opt_scale, opt_scale, densmin, densmax);
345 #if HAVE_PNG
346   else if (opt_format == O_FORMAT_PNG)
347     im.writeImagePNG (out_file, 8, opt_scale, opt_scale, densmin, densmax);
348   else if (opt_format == O_FORMAT_PNG16)
349     im.writeImagePNG (out_file, 16, opt_scale, opt_scale, densmin, densmax);
350 #endif
351 #if HAVE_GIF
352   else if (opt_format == O_FORMAT_GIF) 
353     im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
354 #endif
355   else if (opt_format == O_FORMAT_DISP) {
356 #if HAVE_SGP
357     im.displayScaling (opt_scale, densmin, densmax);
358     cout << "Press enter to continue\n";
359     cio_kb_getc();
360     sgp2_close(sgp2_get_active_win());
361 #endif
362   }
363   else
364     {
365       sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
366       return (1);
367     }
368   return (0);
369 }
370
371
372 #ifndef NO_MAIN
373 int 
374 main (int argc, char *const argv[])
375 {
376   int retval = 1;
377
378   try {
379     retval = if2img_main(argc, argv);
380   } catch (exception e) {
381     cerr << "Exception: " << e.what() << endl;
382   } catch (...) {
383     cerr << "Unknown exception" << endl;
384   }
385
386   return (retval);
387 }
388 #endif