r254: Added binary output of projection file elements
[ctsim.git] / tools / pjinfo.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          pjinfo.cpp
5 **   Purpose:       Convert an projection data file to 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: pjinfo.cpp,v 1.2 2000/12/16 02:31:00 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  *   pjinfo.c                   Convert Raysum to image
30  *
31  * DATE
32  *   August 2000
33  */
34
35 #include "ct.h"
36 #include "timer.h"
37
38
39 enum { O_BINARYHEADER, O_BINARYVIEWS, O_STARTVIEW, O_ENDVIEW, O_DUMP, O_HELP, O_VERSION };
40
41 static struct option my_options[] =
42 {
43   {"binaryheader", 0, 0, O_BINARYHEADER},
44   {"binaryviews", 0, 0, O_BINARYVIEWS},
45   {"startview", 1, 0, O_STARTVIEW},
46   {"endview", 1, 0, O_ENDVIEW},
47   {"dump", 0, 0, O_DUMP},
48   {"help", 0, 0, O_HELP},
49   {"version", 0, 0, O_VERSION},
50   {0, 0, 0, 0}
51 };
52
53 static const char* g_szIdStr = "$Id: pjinfo.cpp,v 1.2 2000/12/16 02:31:00 kevin Exp $";
54
55 void 
56 pjinfo_usage (const char *program)
57 {
58   cout << "usage: " << fileBasename(program) << " proj-file [OPTIONS]" << endl;
59   cout << "Display projection file information" << endl;
60   cout << "\n";
61   cout << "   --binaryheader  Dump binary header data\n";
62   cout << "   --binaryviews   Dump binary view data\n";
63   cout << "   --startview n   Beginning view number to display (default=0)\n";
64   cout << "   --endview n     Ending view number to display (default=last view)\n";
65   cout << "   --dump          Print all scan data ASCII format\n";
66   cout << "   --version       Print version" << endl;
67   cout << "   --help          Print this help message" << endl;
68 }
69
70           
71
72 int 
73 pjinfo_main (const int argc, char *const argv[])
74 {
75   string pj_name;
76   bool optDump = false;
77   bool optBinaryHeader = false;
78   bool optBinaryViews = false;
79   int optStartView = 0;
80   int optEndView = -1;  // tells copyViewData to use default last view
81   extern int optind;
82
83   while (1)
84     {
85       char *endptr, *endstr;
86       int c = getopt_long (argc, argv, "", my_options, NULL);
87       if (c == -1)
88         break;
89       
90       switch (c)
91         {
92         case O_DUMP:
93           optDump = true;
94           break;
95         case O_BINARYHEADER:
96            optBinaryHeader = true;
97            break;
98         case O_BINARYVIEWS:
99            optBinaryViews = true;
100            break;
101         case O_STARTVIEW:
102           optStartView = strtol(optarg, &endptr, 10);
103           endstr = optarg + strlen(optarg);
104           if (endptr != endstr) {
105             cerr << "Error setting --startview to %s" << optarg << endl;
106             pjinfo_usage(argv[0]);
107             return (1);
108           }
109           break;
110         case O_ENDVIEW:
111           optEndView = strtol(optarg, &endptr, 10);
112           endstr = optarg + strlen(optarg);
113           if (endptr != endstr) {
114             cerr << "Error setting --endview to %s" << optarg << endl;
115             pjinfo_usage(argv[0]);
116             return (1);
117           }
118           break;
119         case O_VERSION:
120 #ifdef VERSION
121           cout << "Version " << VERSION << endl << g_szIdStr << endl;
122 #else
123           cout << "Unknown version number" << endl;
124 #endif
125           return (0);
126         case O_HELP:
127         case '?':
128           pjinfo_usage(argv[0]);
129           return (0);
130         default:
131           pjinfo_usage(argv[0]);
132           return (1);
133         }
134     }
135   
136   if (argc - optind != 1) {
137     pjinfo_usage(argv[0]);
138     return (1);
139   }
140
141   pj_name = argv[optind];
142
143   if (optBinaryHeader)
144     Projections::copyHeader (pj_name, cout);
145   else if (optBinaryViews)
146     Projections::copyViewData (pj_name, cout, optStartView, optEndView);
147   else {
148     Projections pj;
149     if (! pj.read (pj_name)) {
150       sys_error (ERR_SEVERE, "Can not open projection file %s", pj_name.c_str());
151       return (1);
152     }
153
154     if (optDump) {
155       pj.printProjectionData (optStartView, optEndView);
156     } else {
157       ostringstream os;
158       pj.printScanInfo (os);
159       cout << os.str();
160     }
161   }
162   
163   return(0);
164 }
165
166
167 #ifndef NO_MAIN
168 int 
169 main (const int argc, char *const argv[])
170 {
171   int retval = 1;
172
173   try {
174     retval = pjinfo_main(argc, argv);
175   } catch (exception e) {
176     cerr << "Exception: " << e.what() << endl;
177   } catch (...) {
178     cerr << "Unknown exception" << endl;
179   }
180
181   return (retval);
182 }
183 #endif