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