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