r72: Initial C++ versions
[ctsim.git] / src / ifinfo.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: ifinfo.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: ifinfo.cpp,v $
7 **  Revision 1.1  2000/06/07 02:29:05  kevin
8 **  Initial C++ versions
9 **
10 **  Revision 1.3  2000/05/16 04:33:59  kevin
11 **  Improved option processing
12 **
13 **  Revision 1.2  2000/05/08 20:02:32  kevin
14 **  ANSI C changes
15 **
16 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
17 **  Initial CVS import for first public release
18 **
19 **
20 **
21 **  This program is free software; you can redistribute it and/or modify
22 **  it under the terms of the GNU General Public License (version 2) as
23 **  published by the Free Software Foundation.
24 **
25 **  This program is distributed in the hope that it will be useful,
26 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
27 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 **  GNU General Public License for more details.
29 **
30 **  You should have received a copy of the GNU General Public License
31 **  along with this program; if not, write to the Free Software
32 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 ******************************************************************************/
34
35 /* FILE
36  *   ifinfo.c             Display info on sdf files
37  */
38
39 #include "ct.h"
40
41 enum { O_LABELS, O_STATS, O_NO_STATS, O_NO_LABELS, O_VERBOSE, O_HELP, O_VERSION, O_DEBUG };
42
43 static struct option my_options[] =
44 {
45   {"labels", 0, 0, O_LABELS},
46   {"no-labels", 0, 0, O_NO_LABELS},
47   {"stats", 0, 0, O_STATS},
48   {"no-stats", 0, 0, O_NO_STATS},
49   {"debug", 0, 0, O_DEBUG},
50   {"verbose", 0, 0, O_VERBOSE},
51   {"help", 0, 0, O_HELP},
52   {"version", 0, 0, O_VERSION},
53   {0, 0, 0, 0}
54 };
55
56 void 
57 ifinfo_usage (const char *program)
58 {
59   fprintf(stdout, "usage: %s infile [OPTIONS]\n", kbasename(program));
60   fprintf(stdout, "Imagefile information\n");
61   fprintf(stdout, "\n");
62   fprintf(stdout, "     infile       Name of input SDF file\n");
63   fprintf(stdout, "     --display    Display image\n");
64   fprintf(stdout, "     --labels     Print image labels (default)\n");
65   fprintf(stdout, "     --no-labels  Do not print image labels\n");
66   fprintf(stdout, "     --stats      Print image statistics (default)\n");
67   fprintf(stdout, "     --no-stats   Do not print image statistics\n");
68   fprintf(stdout, "     --debug      Debug mode\n");
69   fprintf(stdout, "     --verbose    Verbose mode\n");
70   fprintf(stdout, "     --version    Print version\n");
71   fprintf(stdout, "     --help       Print this help message\n");
72 }
73
74 int 
75 ifinfo_main (int argc, char *const argv[])
76 {
77   ImageFile *im;
78   char *in_file;
79   int opt_verbose = 0;
80   int opt_stats = 1;
81   int opt_labels = 1;
82   int opt_debug = 0;
83
84   while (1)
85     {
86       int c = getopt_long (argc, argv, "", my_options, NULL);
87       
88       if (c == -1)
89         break;
90       
91       switch (c)
92         {
93         case O_LABELS:
94           opt_labels = 1;
95           break;
96         case O_STATS:
97           opt_stats = 1;
98           break;
99         case O_NO_LABELS:
100           opt_labels = 0;
101           break;
102         case O_NO_STATS:
103           opt_stats = 0;
104           break;
105         case O_VERBOSE:
106           opt_verbose = 1;
107           break;
108         case O_DEBUG:
109           opt_debug = 0;
110           break;
111         case O_VERSION:
112 #ifdef VERSION
113           fprintf(stdout, "Version %s\n", VERSION);
114 #else
115           fprintf(stderr, "Unknown version number");
116 #endif
117           exit(0);
118         case O_HELP:
119         case '?':
120           ifinfo_usage(argv[0]);
121           return (0);
122         default:
123           ifinfo_usage(argv[0]);
124           return (1);
125         }
126     }
127
128   if (optind + 1 != argc)
129     {
130       ifinfo_usage(argv[0]);
131       return (1);
132     }
133   
134   in_file = argv[optind];
135
136   im = new ImageFile (in_file);
137   im->adf.fileRead ();
138
139   if (opt_labels) 
140     {
141       int nlabels = im->adf.getNumLabels();
142       int i;
143
144       for (i = 0; i < nlabels; i++)
145         {
146             Array2dFileLabel label;
147             string str;
148
149             im->adf.labelRead (label, i);
150             label.getDateString (str);
151
152             if (label.getLabelType() == Array2dFileLabel::L_HISTORY) {
153               cout << "History: " << label.getLabelString() << endl;
154               cout << "  calc time = " << label.getCalcTime() << " secs" << endl;
155               cout << "  Timestamp = " << str << endl;
156             } else if (label.getLabelType() == Array2dFileLabel::L_USER) {
157               cout << "Note: " <<  label.getLabelString() << endl;
158               cout << "  Timestamp = %s" << str << endl;
159           }
160         }
161     }
162
163   if (opt_stats)
164     {
165       double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
166       double mode = 0, mean = 0, stddev = 0;
167       double spread;
168       int hist[256];
169       int ibin, nbin = 256;
170       int max_bin, max_binindex; 
171       double maxbin;
172       int ix, iy;
173
174       maxbin = nbin - 1;
175       ImageFileArray v = im->adf.getArray();
176       int nx = im->adf.nx;
177       int ny = im->adf.ny;
178
179       for (ix = 0; ix < nx; ix++)
180         {
181           for (iy = 0; iy < ny; iy++)
182             {
183               if (v[ix][iy] > maxfound)
184                 maxfound = v[ix][iy];
185               if (v[ix][iy] < minfound)
186                 minfound = v[ix][iy];
187               mean += v[ix][iy];
188             }
189         }
190       spread = maxfound - minfound;
191       if (spread == 0)
192         mode = minfound;
193       else
194         {
195           for (ibin = 0; ibin < nbin; ibin++)
196             hist[ibin] = 0;
197           for (ix = 0; ix < nx; ix++)
198             {
199               for (iy = 0; iy < ny; iy++)
200                 {
201                   int b = (int) ((((v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
202                   hist[b]++;
203                 }
204             }
205           max_binindex = 0;
206           max_bin = -1;
207           for (ibin = 0; ibin < nbin; ibin++)
208             {
209               if (hist[ibin] > max_bin)
210                 {
211                   max_bin = hist[ibin];
212                   max_binindex = ibin;
213                 }
214             }
215           mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
216         }
217
218       mean /= nx * ny;
219       for (ix = 0; ix < nx; ix++)
220         {
221           for (iy = 0; iy < ny; iy++)
222             {
223               double diff = (v[ix][iy] - mean);
224               stddev += diff * diff;
225             }
226         }
227       stddev = sqrt(stddev / (nx * ny));
228       fprintf(stdout,"nx=%d\n", nx);
229       fprintf(stdout,"ny=%d\n", ny);
230       fprintf(stdout,"min=%f\n", minfound);
231       fprintf(stdout,"max=%f\n", maxfound);
232       fprintf(stdout,"mean=%f\n", mean);
233       fprintf(stdout,"mode=%f\n", mode);
234       fprintf(stdout,"stddev=%f\n", stddev);
235     }
236   
237   return (0);
238 }
239
240 #ifndef NO_MAIN
241 int 
242 main (int argc, char *const argv[])
243 {
244   return (ifinfo_main(argc, argv));
245 }
246 #endif