r72: Initial C++ versions
[ctsim.git] / src / rs2if.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: rs2if.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: rs2if.cpp,v $
7 **  Revision 1.1  2000/06/07 02:29:05  kevin
8 **  Initial C++ versions
9 **
10 **  Revision 1.3  2000/05/16 04:33:59  kevin
11 **  Improved option processing
12 **
13 **  Revision 1.2  2000/05/08 20:02:32  kevin
14 **  ANSI C changes
15 **
16 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
17 **  Initial CVS import for first public release
18 **
19 **
20 **
21 **  This program is free software; you can redistribute it and/or modify
22 **  it under the terms of the GNU General Public License (version 2) as
23 **  published by the Free Software Foundation.
24 **
25 **  This program is distributed in the hope that it will be useful,
26 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
27 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 **  GNU General Public License for more details.
29 **
30 **  You should have received a copy of the GNU General Public License
31 **  along with this program; if not, write to the Free Software
32 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 ******************************************************************************/
34
35 /* FILE
36  *   rs2if.c                    Convert Raysum to image
37  *
38  * DATE
39  *   Apr 1999
40  */
41
42 #include "ct.h"
43
44
45 enum { O_VERBOSE, O_HELP, O_VERSION };
46
47 static struct option my_options[] =
48 {
49   {"verbose", 0, 0, O_VERBOSE},
50   {"help", 0, 0, O_HELP},
51   {"version", 0, 0, O_VERSION},
52   {0, 0, 0, 0}
53 };
54
55
56 void 
57 rs2if_usage (const char *program)
58 {
59   fprintf(stdout, "usage: %s in-rs-file out-if-file [OPTIONS]\n", kbasename(program));
60   fprintf(stdout, "This program converts a raysum file to a IF file\n");
61   fprintf(stdout, "\n");
62   fprintf(stdout, "   --verbose   Verbose mode\n");
63   fprintf(stdout, "   --version   Print version\n");
64   fprintf(stdout, "   --help      Print this help message\n");
65 }
66
67           
68
69 int 
70 rs2if_main (const int argc, char *const argv[])
71 {
72   ImageFile *im;
73   RAYSUM *rs;
74   DETARRAY *detarray;
75   char *rs_name, *im_name;
76   int ix, iy;
77   int opt_v = 0;
78   extern int optind;
79   
80   while (1)
81     {
82       int c = getopt_long (argc, argv, "", my_options, NULL);
83       if (c == -1)
84         break;
85       
86       switch (c)
87         {
88         case O_VERBOSE:
89           opt_v = 1;
90           break;
91         case O_VERSION:
92 #ifdef VERSION
93           fprintf(stdout, "Version %s\n", VERSION);
94 #else
95           fprintf(stderr, "Unknown version number");
96 #endif
97           exit(0);
98         case O_HELP:
99         case '?':
100           rs2if_usage(argv[0]);
101           return (0);
102         default:
103           rs2if_usage(argv[0]);
104           return (1);
105         }
106     }
107   
108   if (argc - optind != 2) {
109     rs2if_usage(argv[0]);
110     return (1);
111   }
112
113   rs_name = argv[optind];
114   im_name = argv[optind + 1];
115
116   if (file_exists(rs_name) == FALSE) {
117     fprintf (stderr, "Raysum file %s does not exist\n", rs_name);
118     return (1);
119   }
120   
121   rs = raysum_open (rs_name);
122
123   if (opt_v)
124     {
125       printf ("Number of detectors: %d\n", rs->ndet);
126       printf ("    Number of views: %d\n", rs->nview);
127       printf ("             Remark: %s\n", rs->remark);
128     }
129   
130   im = new ImageFile (im_name, rs->ndet, rs->nview);
131   
132   detarray = detarray_alloc (rs->ndet);
133   ImageFileArray v = im->adf.getArray();
134
135   for (iy = 0; iy < rs->nview; iy++)
136     {
137       detarray_read (rs, detarray, iy);
138       for (ix = 0; ix < rs->ndet; ix++)
139         {
140           v[ix][iy] = detarray->detval[ix];
141         }
142     }
143   detarray_free (detarray);
144   raysum_close (rs);
145
146   im->adf.fileCreate ();
147   im->adf.arrayDataWrite ();
148   im->adf.labelAdd (Array2dFileLabel::L_HISTORY, rs->remark, rs->calctime);
149   im->adf.labelAdd (Array2dFileLabel::L_HISTORY, "Conversion from .rs to .idf");
150   im->adf.fileClose ();
151   
152   return(0);
153 }
154
155
156 #ifndef NO_MAIN
157 int 
158 main (const int argc, char *const argv[])
159 {
160   return (rs2if_main(argc, argv));
161 }
162 #endif