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