f6c8da7f789f530d23fea79dfe02111aa119689d
[ctsim.git] / src / ifinfo.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          ifinfo.cpp
5 **   Purpose:       Display information about an image file
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  April 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: ifinfo.cpp,v 1.10 2000/06/25 17:32:24 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 /* FILE
29  *   ifinfo.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 ifinfo_usage (const char *program)
51 {
52   cout << "usage: " << fileBasename(program) << " image1 [image2] [OPTIONS]" << endl;
53   cout << "Imagefile information" << endl;
54   cout << endl;
55   cout << "     infile       Name of input IF file" << endl;
56   cout << "     --display    Display image" << endl;
57   cout << "     --labels     Print image labels (default)" << endl;
58   cout << "     --no-labels  Do not print image labels" << endl;
59   cout << "     --stats      Print image statistics (default)" << endl;
60   cout << "     --no-stats   Do not print image statistics" << endl;
61   cout << "     --debug      Debug mode" << endl;
62   cout << "     --verbose    Verbose mode" << endl;
63   cout << "     --version    Print version" << endl;
64   cout << "     --help       Print this help message" << endl;
65 }
66
67 int 
68 ifinfo_main (int argc, char *const argv[])
69 {
70   ImageFile *im = NULL;
71   ImageFile* im2 = NULL;
72   string in_file;
73   string in2_file;
74   int opt_verbose = 0;
75   int opt_stats = 1;
76   int opt_labels = 1;
77   int opt_debug = 0;
78
79   while (1)
80     {
81       int c = getopt_long (argc, argv, "", my_options, NULL);
82       
83       if (c == -1)
84         break;
85       
86       switch (c)
87         {
88         case O_LABELS:
89           opt_labels = 1;
90           break;
91         case O_STATS:
92           opt_stats = 1;
93           break;
94         case O_NO_LABELS:
95           opt_labels = 0;
96           break;
97         case O_NO_STATS:
98           opt_stats = 0;
99           break;
100         case O_VERBOSE:
101           opt_verbose = 1;
102           break;
103         case O_DEBUG:
104           opt_debug = 0;
105           break;
106         case O_VERSION:
107 #ifdef VERSION
108           cout << "Version " <<  VERSION << endl;
109 #else
110           cout << "Unknown version number" << endl;
111 #endif
112           return (0);
113         case O_HELP:
114         case '?':
115           ifinfo_usage(argv[0]);
116           return (0);
117         default:
118           ifinfo_usage(argv[0]);
119           return (1);
120         }
121     }
122
123   if (optind + 2 == argc) {
124     in2_file = argv [optind+1];
125   } else if (optind + 1 != argc) {
126     ifinfo_usage (argv[0]);
127     return (1);
128   }
129   
130   in_file = argv[optind];
131
132   im = new ImageFile (in_file.c_str());
133   if (! im->fileRead ()) {
134     sys_error (ERR_WARNING, "Unable to read file %s", in_file.c_str());
135     return (1);
136   }
137   if (in2_file != "") {
138     im2 = new ImageFile(in2_file.c_str());
139     if (! im2->fileRead ()) {
140       sys_error (ERR_WARNING, "Unable to read file %s", in2_file.c_str());
141       return (1);
142     }
143   }
144
145   if (opt_stats)
146     cout << "Image size: (" << im->nx() << "," << im->ny() << ")" << endl << endl;
147
148   if (opt_labels) 
149     im->printLabels (cout);
150
151   if (opt_stats) {
152     if (im2)
153       cout << "Image 1" << endl;
154     
155     im->printStatistics (cout);
156
157     if (im2) {
158       cout << endl;
159
160       if (opt_labels)
161         im2->printLabels(cout);
162
163       cout << "Image 2" << endl;
164       im2->printStatistics (cout);
165       cout << endl;
166       
167       im->printComparativeStatistics (*im2, cout);
168     }
169   }
170   
171   return (0);
172 }
173
174 #ifndef NO_MAIN
175 int 
176 main (int argc, char *const argv[])
177 {
178   int retval = 1;
179
180   try {
181     retval = ifinfo_main(argc, argv);
182   } catch (exception e) {
183     cerr << "Exception: " << e.what() << endl;
184   } catch (...) {
185     cerr << "Unknown exception" << endl;
186   }
187
188   return (retval);
189 }
190 #endif