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