r102: fixed mpi bug
[ctsim.git] / src / ifinfo.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ifinfo.cpp
5 **   Purpose:       Display information about an image file
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: ifinfo.cpp,v 1.9 2000/06/19 17:58:13 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 /* FILE
29  *   ifinfo.c             Display info on sdf files
30  */
31
32 #include "ct.h"
33
34 enum { O_LABELS, O_STATS, O_NO_STATS, O_NO_LABELS, O_VERBOSE, O_HELP, O_VERSION, O_DEBUG };
35
36 static struct option my_options[] =
37 {
38   {"labels", 0, 0, O_LABELS},
39   {"no-labels", 0, 0, O_NO_LABELS},
40   {"stats", 0, 0, O_STATS},
41   {"no-stats", 0, 0, O_NO_STATS},
42   {"debug", 0, 0, O_DEBUG},
43   {"verbose", 0, 0, O_VERBOSE},
44   {"help", 0, 0, O_HELP},
45   {"version", 0, 0, O_VERSION},
46   {0, 0, 0, 0}
47 };
48
49 void 
50 ifinfo_usage (const char *program)
51 {
52   cout << "usage: " << fileBasename(program) << " infile [OPTIONS]" << endl;
53   cout << "Imagefile information" << endl;
54   cout << endl;
55   cout << "     infile       Name of input IF file" << endl;
56   cout << "     --display    Display image" << endl;
57   cout << "     --labels     Print image labels (default)" << endl;
58   cout << "     --no-labels  Do not print image labels" << endl;
59   cout << "     --stats      Print image statistics (default)" << endl;
60   cout << "     --no-stats   Do not print image statistics" << endl;
61   cout << "     --debug      Debug mode" << endl;
62   cout << "     --verbose    Verbose mode" << endl;
63   cout << "     --version    Print version" << endl;
64   cout << "     --help       Print this help message" << endl;
65 }
66
67 int 
68 ifinfo_main (int argc, char *const argv[])
69 {
70   ImageFile *im;
71   char *in_file;
72   int opt_verbose = 0;
73   int opt_stats = 1;
74   int opt_labels = 1;
75   int opt_debug = 0;
76
77   while (1)
78     {
79       int c = getopt_long (argc, argv, "", my_options, NULL);
80       
81       if (c == -1)
82         break;
83       
84       switch (c)
85         {
86         case O_LABELS:
87           opt_labels = 1;
88           break;
89         case O_STATS:
90           opt_stats = 1;
91           break;
92         case O_NO_LABELS:
93           opt_labels = 0;
94           break;
95         case O_NO_STATS:
96           opt_stats = 0;
97           break;
98         case O_VERBOSE:
99           opt_verbose = 1;
100           break;
101         case O_DEBUG:
102           opt_debug = 0;
103           break;
104         case O_VERSION:
105 #ifdef VERSION
106           cout << "Version " <<  VERSION << endl;
107 #else
108           cout << "Unknown version number" << endl;
109 #endif
110           return (0);
111         case O_HELP:
112         case '?':
113           ifinfo_usage(argv[0]);
114           return (0);
115         default:
116           ifinfo_usage(argv[0]);
117           return (1);
118         }
119     }
120
121   if (optind + 1 != argc)
122     {
123       ifinfo_usage(argv[0]);
124       return (1);
125     }
126   
127   in_file = argv[optind];
128
129   im = new ImageFile (in_file);
130   if (! im->fileRead ()) {
131     sys_error (ERR_WARNING, "Unable to read file %s", in_file);
132     return (1);
133   }
134
135   if (opt_labels) 
136     {
137       int nlabels = im->getNumLabels();
138       int i;
139
140       for (i = 0; i < nlabels; i++)
141         {
142             Array2dFileLabel label;
143             im->labelRead (label, i);
144
145             if (label.getLabelType() == Array2dFileLabel::L_HISTORY) {
146               cout << "History: " << endl;
147               cout << "  " << label.getLabelString() << endl;
148               cout << "  calc time = " << label.getCalcTime() << " secs" << endl;
149               cout << "  Timestamp = " << label.getDateString() << endl;
150             } else if (label.getLabelType() == Array2dFileLabel::L_USER) {
151               cout << "Note: " <<  label.getLabelString() << endl;
152               cout << "  Timestamp = %s" << label.getDateString() << endl;
153             }
154             cout << endl;
155         }
156     }
157
158   if (opt_stats)
159     {
160       double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
161       double mode = 0, mean = 0, stddev = 0;
162       double spread;
163       int hist[256];
164       int ibin, nbin = 256;
165       int max_bin, max_binindex; 
166       double maxbin;
167       int ix, iy;
168
169       maxbin = nbin - 1;
170       ImageFileArray v = im->getArray();
171       int nx = im->nx();
172       int ny = im->ny();
173
174       for (ix = 0; ix < nx; ix++)
175         {
176           for (iy = 0; iy < ny; iy++)
177             {
178               if (v[ix][iy] > maxfound)
179                 maxfound = v[ix][iy];
180               if (v[ix][iy] < minfound)
181                 minfound = v[ix][iy];
182               mean += v[ix][iy];
183             }
184         }
185       spread = maxfound - minfound;
186       if (spread == 0)
187         mode = minfound;
188       else
189         {
190           for (ibin = 0; ibin < nbin; ibin++)
191             hist[ibin] = 0;
192           for (ix = 0; ix < nx; ix++)
193             {
194               for (iy = 0; iy < ny; iy++)
195                 {
196                   int b = (int) ((((v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
197                   hist[b]++;
198                 }
199             }
200           max_binindex = 0;
201           max_bin = -1;
202           for (ibin = 0; ibin < nbin; ibin++)
203             {
204               if (hist[ibin] > max_bin)
205                 {
206                   max_bin = hist[ibin];
207                   max_binindex = ibin;
208                 }
209             }
210           mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
211         }
212
213       mean /= nx * ny;
214       for (ix = 0; ix < nx; ix++)
215         {
216           for (iy = 0; iy < ny; iy++)
217             {
218               double diff = (v[ix][iy] - mean);
219               stddev += diff * diff;
220             }
221         }
222       stddev = sqrt(stddev / (nx * ny));
223       cout << "    nx: " << nx << endl;
224       cout << "    nx: " << ny << endl;
225       cout << "   min: " << minfound << endl;
226       cout << "   max: " << maxfound << endl;
227       cout << "  mean: " << mean << endl;
228       cout << "  mode: " << mode << endl;
229       cout << "stddef: " << stddev << endl;
230     }
231   
232   return (0);
233 }
234
235 #ifndef NO_MAIN
236 int 
237 main (int argc, char *const argv[])
238 {
239   int retval = 1;
240
241   try {
242     retval = ifinfo_main(argc, argv);
243   } catch (exception e) {
244     cerr << "Exception: " << e.what() << endl;
245   } catch (...) {
246     cerr << "Unknown exception" << endl;
247   }
248
249   return (retval);
250 }
251 #endif