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