r186: *** empty log message ***
[ctsim.git] / tools / pj2if.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          pj2if.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: pj2if.cpp,v 1.4 2000/08/31 08:38:58 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  *   pj2if.c                    Convert Raysum to image
30  *
31  * DATE
32  *   Apr 1999
33  */
34
35 #include "ct.h"
36 #include "timer.h"
37
38
39 enum { O_VERBOSE, O_DUMP, O_HELP, O_VERSION };
40
41 static struct option my_options[] =
42 {
43   {"verbose", 0, 0, O_VERBOSE},
44   {"dump", 0, 0, O_DUMP},
45   {"help", 0, 0, O_HELP},
46   {"version", 0, 0, O_VERSION},
47   {0, 0, 0, 0}
48 };
49
50 static const char* g_szIdStr = "$Id: pj2if.cpp,v 1.4 2000/08/31 08:38:58 kevin Exp $";
51
52 void 
53 pj2if_usage (const char *program)
54 {
55   cout << "usage: " << fileBasename(program) << " in-proj-file out-if-file [OPTIONS]" << endl;
56   cout << "Converts a projection file to a IF file" << endl;
57   cout << endl;
58   cout << "   --verbose   Verbose mode" << endl;
59   cout << "   --dump      Dump all scan data" << endl;
60   cout << "   --version   Print version" << endl;
61   cout << "   --help      Print this help message" << endl;
62 }
63
64           
65
66 int 
67 pj2if_main (const int argc, char *const argv[])
68 {
69   char *pj_name, *im_name;
70   bool optVerbose = false;
71   bool optDump = false;
72   extern int optind;
73   Timer timerProgram;
74
75   while (1)
76     {
77       int c = getopt_long (argc, argv, "", my_options, NULL);
78       if (c == -1)
79         break;
80       
81       switch (c)
82         {
83         case O_VERBOSE:
84           optVerbose = true;
85           break;
86         case O_DUMP:
87           optDump = true;
88           break;
89         case O_VERSION:
90 #ifdef VERSION
91           cout << "Version " << VERSION << endl << g_szIdStr << endl;
92 #else
93           cout << "Unknown version number" << endl;
94 #endif
95           return (0);
96         case O_HELP:
97         case '?':
98           pj2if_usage(argv[0]);
99           return (0);
100         default:
101           pj2if_usage(argv[0]);
102           return (1);
103         }
104     }
105   
106   if (argc - optind != 2) {
107     pj2if_usage(argv[0]);
108     return (1);
109   }
110
111   pj_name = argv[optind];
112   im_name = argv[optind + 1];
113
114   Projections pj;
115   if (! pj.read (pj_name)) {
116     sys_error (ERR_SEVERE, "Can not open projection file %s", pj_name);
117     return (1);
118   }
119
120   if (optDump)
121     pj.printProjectionData();
122   else if (optVerbose) {
123     ostringstream os;
124     pj.printScanInfo (os);
125     cout << os.str();
126   }
127   
128   ImageFile im (pj.nDet(), pj.nView());
129   
130   ImageFileArray v = im.getArray();
131
132   for (int iy = 0; iy < pj.nView(); iy++) {
133     const DetectorArray& detarray = pj.getDetectorArray (iy);
134     const DetectorValue* detval = detarray.detValues();
135     for (int ix = 0; ix < pj.nDet(); ix++) {
136       v[ix][iy] = detval[ix];
137     }
138   }
139
140   im.labelAdd (pj.getLabel());
141   im.labelAdd (Array2dFileLabel::L_HISTORY, "Conversion from .pj to .if", timerProgram.timerEnd());
142   im.fileWrite (im_name);
143   
144   return(0);
145 }
146
147
148 #ifndef NO_MAIN
149 int 
150 main (const int argc, char *const argv[])
151 {
152   int retval = 1;
153
154   try {
155     retval = pj2if_main(argc, argv);
156   } catch (exception e) {
157     cerr << "Exception: " << e.what() << endl;
158   } catch (...) {
159     cerr << "Unknown exception" << endl;
160   }
161
162   return (retval);
163 }
164 #endif