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