r79: *** empty log message ***
[ctsim.git] / src / sdfinfo.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: sdfinfo.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: sdfinfo.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  *   sdfinfo.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 sdfinfo_usage (const char *program)
58 {
59   fprintf(stdout, "sdfinfo_usage: %s infile [OPTIONS]\n", kbasename(program));
60   fprintf(stdout, "Analyze an SDF2D file\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 sdfinfo_main (int argc, char *const argv[])
76 {
77   IMAGE *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           sdfinfo_usage(argv[0]);
121           return (0);
122         default:
123           sdfinfo_usage(argv[0]);
124           return (1);
125         }
126     }
127
128   if (optind + 1 != argc)
129     {
130       sdfinfo_usage(argv[0]);
131       return (1);
132     }
133   
134   in_file = argv[optind];
135
136   im = image_load (in_file);
137
138   if (opt_labels) 
139     {
140       int nlabels = im->dfp_2d->dfp->num_label;
141       int i;
142
143       for (i = 0; i < nlabels; i++)
144         {
145           SDF_BLK blk;
146           if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
147             break;
148           if (blk.lab.label_type == LT_HISTORY) {
149             fprintf(stdout, "History: %s\n", blk.lab.label_str);
150             fprintf(stdout, "  calc time = %.2f secs\n", blk.lab.calc_time);
151             fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
152           } else if (blk.lab.label_type == LT_TITLE) {
153             fprintf(stdout, "Title: %s\n", blk.lab.label_str);
154             fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
155           } else if (blk.lab.label_type == LT_NOTE) {
156             fprintf(stdout, "Note: %s\n", blk.lab.label_str);
157             fprintf(stdout, "  Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
158           }
159         }
160     }
161
162   if (opt_stats)
163     {
164       double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
165       double mode = 0, mean = 0, stddev = 0;
166       double spread;
167       int hist[256];
168       int ibin, nbin = 256;
169       int max_bin, max_binindex; 
170       double maxbin;
171       int ix, iy;
172
173       maxbin = nbin - 1;
174       for (ix = 0; ix < im->nx; ix++)
175         {
176           for (iy = 0; iy < im->ny; iy++)
177             {
178               if (im->v[ix][iy] > maxfound)
179                 maxfound = im->v[ix][iy];
180               if (im->v[ix][iy] < minfound)
181                 minfound = im->v[ix][iy];
182               mean += im->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 < im->nx; ix++)
193             {
194               for (iy = 0; iy < im->ny; iy++)
195                 {
196                   int b = (int) ((((im->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 /= im->nx * im->ny;
214       for (ix = 0; ix < im->nx; ix++)
215         {
216           for (iy = 0; iy < im->ny; iy++)
217             {
218               double diff = (im->v[ix][iy] - mean);
219               stddev += diff * diff;
220             }
221         }
222       stddev = sqrt(stddev / (im->nx * im->ny));
223       fprintf(stdout,"nx=%d\n", im->nx);
224       fprintf(stdout,"ny=%d\n", im->ny);
225       fprintf(stdout,"min=%f\n", minfound);
226       fprintf(stdout,"max=%f\n", maxfound);
227       fprintf(stdout,"mean=%f\n", mean);
228       fprintf(stdout,"mode=%f\n", mode);
229       fprintf(stdout,"stddev=%f\n", stddev);
230     }
231   
232   return (0);
233 }
234
235 #ifndef NO_MAIN
236 int 
237 main (int argc, char *const argv[])
238 {
239   return (sdfinfo_main(argc, argv));
240 }
241 #endif