26c537620002eb36e81a9bc14148e1c60ccaeae5
[ctsim.git] / src / 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.3 2000/06/19 17:58:13 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
37
38 enum { O_VERBOSE, O_HELP, O_VERSION };
39
40 static struct option my_options[] =
41 {
42   {"verbose", 0, 0, O_VERBOSE},
43   {"help", 0, 0, O_HELP},
44   {"version", 0, 0, O_VERSION},
45   {0, 0, 0, 0}
46 };
47
48
49 void 
50 pj2if_usage (const char *program)
51 {
52   cout << "usage: " << fileBasename(program) << " in-proj-file out-if-file [OPTIONS]" << endl;
53   cout << "Converts a projection file to a IF file" << endl;
54   cout << endl;
55   cout << "   --verbose   Verbose mode" << endl;
56   cout << "   --version   Print version" << endl;
57   cout << "   --help      Print this help message" << endl;
58 }
59
60           
61
62 int 
63 pj2if_main (const int argc, char *const argv[])
64 {
65   char *pj_name, *im_name;
66   int ix, iy;
67   bool opt_verbose = false;
68   extern int optind;
69   
70   while (1)
71     {
72       int c = getopt_long (argc, argv, "", my_options, NULL);
73       if (c == -1)
74         break;
75       
76       switch (c)
77         {
78         case O_VERBOSE:
79           opt_verbose = true;
80           break;
81         case O_VERSION:
82 #ifdef VERSION
83           cout << "Version " << VERSION << endl;
84 #else
85           cout << "Unknown version number" << endl;
86 #endif
87           return (0);
88         case O_HELP:
89         case '?':
90           pj2if_usage(argv[0]);
91           return (0);
92         default:
93           pj2if_usage(argv[0]);
94           return (1);
95         }
96     }
97   
98   if (argc - optind != 2) {
99     pj2if_usage(argv[0]);
100     return (1);
101   }
102
103   pj_name = argv[optind];
104   im_name = argv[optind + 1];
105
106   Projections pj;
107   if (! pj.read (pj_name)) {
108     sys_error (ERR_SEVERE, "Can not open projection file %s", pj_name);
109     return (1);
110   }
111
112   if (opt_verbose)
113       pj.printScanInfo();
114   
115   ImageFile im (im_name, pj.nDet(), pj.nView());
116   
117   ImageFileArray v = im.getArray();
118
119   for (iy = 0; iy < pj.nView(); iy++)
120     {
121       DetectorArray& detarray = pj.getDetectorArray (iy);
122       const DetectorValue* detval = detarray.detValues();
123       for (ix = 0; ix < pj.nDet(); ix++)
124         {
125           v[ix][iy] = detval[ix];
126         }
127     }
128
129   im.fileCreate ();
130   im.arrayDataWrite ();
131   im.labelAdd (Array2dFileLabel::L_HISTORY, pj.remark(), pj.calcTime());
132   im.labelAdd (Array2dFileLabel::L_HISTORY, "Conversion from .pj to .if");
133   im.fileClose ();
134   
135   return(0);
136 }
137
138
139 #ifndef NO_MAIN
140 int 
141 main (const int argc, char *const argv[])
142 {
143   int retval = 1;
144
145   try {
146     retval = pj2if_main(argc, argv);
147   } catch (exception e) {
148     cerr << "Exception: " << e.what() << endl;
149   } catch (...) {
150     cerr << "Unknown exception" << endl;
151   }
152
153   return (retval);
154 }
155 #endif