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