r96: *** empty log message ***
[ctsim.git] / src / rs2if.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          rs2if.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: rs2if.cpp,v 1.6 2000/06/15 19:07:10 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  *   rs2if.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 rs2if_usage (const char *program)
51 {
52   fprintf(stdout, "usage: %s in-rs-file out-if-file [OPTIONS]\n", fileBasename(program));
53   fprintf(stdout, "This program converts a raysum file to a IF file\n");
54   fprintf(stdout, "\n");
55   fprintf(stdout, "   --verbose   Verbose mode\n");
56   fprintf(stdout, "   --version   Print version\n");
57   fprintf(stdout, "   --help      Print this help message\n");
58 }
59
60           
61
62 int 
63 rs2if_main (const int argc, char *const argv[])
64 {
65   ImageFile *im;
66   RAYSUM *rs;
67   DETARRAY *detarray;
68   char *rs_name, *im_name;
69   int ix, iy;
70   int opt_v = 0;
71   extern int optind;
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           opt_v = 1;
83           break;
84         case O_VERSION:
85 #ifdef VERSION
86           fprintf(stdout, "Version %s\n", VERSION);
87 #else
88           fprintf(stderr, "Unknown version number");
89 #endif
90           exit(0);
91         case O_HELP:
92         case '?':
93           rs2if_usage(argv[0]);
94           return (0);
95         default:
96           rs2if_usage(argv[0]);
97           return (1);
98         }
99     }
100   
101   if (argc - optind != 2) {
102     rs2if_usage(argv[0]);
103     return (1);
104   }
105
106   rs_name = argv[optind];
107   im_name = argv[optind + 1];
108
109   if (file_exists(rs_name) == FALSE) {
110     fprintf (stderr, "Raysum file %s does not exist\n", rs_name);
111     return (1);
112   }
113   
114   rs = raysum_open (rs_name);
115
116   if (opt_v)
117     {
118       printf ("Number of detectors: %d\n", rs->ndet);
119       printf ("    Number of views: %d\n", rs->nview);
120       printf ("             Remark: %s\n", rs->remark.c_str());
121     }
122   
123   im = new ImageFile (im_name, rs->ndet, rs->nview);
124   
125   detarray = detarray_alloc (rs->ndet);
126   ImageFileArray v = im->getArray();
127
128   for (iy = 0; iy < rs->nview; iy++)
129     {
130       detarray_read (rs, detarray, iy);
131       for (ix = 0; ix < rs->ndet; ix++)
132         {
133           v[ix][iy] = detarray->detval[ix];
134         }
135     }
136   detarray_free (detarray);
137   raysum_close (rs);
138
139   im->fileCreate ();
140   im->arrayDataWrite ();
141   im->labelAdd (Array2dFileLabel::L_HISTORY, rs->remark.c_str(), rs->calctime);
142   im->labelAdd (Array2dFileLabel::L_HISTORY, "Conversion from .rs to .idf");
143   im->fileClose ();
144   
145   return(0);
146 }
147
148
149 #ifndef NO_MAIN
150 int 
151 main (const int argc, char *const argv[])
152 {
153   return (rs2if_main(argc, argv));
154 }
155 #endif