r79: *** empty log message ***
[ctsim.git] / src / rs2sdf.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: rs2sdf.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: rs2sdf.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  *   rs2sdf.c                   Convert Raysum to image
37  *
38  * DATE
39  *   Apr 1999
40  */
41
42 #include "ct.h"
43
44 enum { O_VERBOSE, O_HELP, O_VERSION };
45
46 static struct option my_options[] =
47 {
48   {"verbose", 0, 0, O_VERBOSE},
49   {"help", 0, 0, O_HELP},
50   {"version", 0, 0, O_VERSION},
51   {0, 0, 0, 0}
52 };
53
54
55 void 
56 rs2sdf_usage (const char *program)
57 {
58   fprintf(stdout, "rs2sdf_usage: %s in-rs-file out-sdf-file [OPTIONS]\n", kbasename(program));
59   fprintf(stdout, "This program converts a raysum file to a SDF2D file\n");
60   fprintf(stdout, "\n");
61   fprintf(stdout, "   --verbose   Verbose mode\n");
62   fprintf(stdout, "   --version   Print version\n");
63   fprintf(stdout, "   --help      Print this help message\n");
64 }
65
66           
67
68 int 
69 rs2sdf_main (const int argc, char *const argv[])
70 {
71   IMAGE *im;
72   RAYSUM *rs;
73   DETARRAY *detarray;
74   char *rs_name, *im_name;
75   int ix, iy;
76   int opt_v = 0;
77   extern int optind;
78   
79   while (1)
80     {
81       int c = getopt_long (argc, argv, "", my_options, NULL);
82       if (c == -1)
83         break;
84       
85       switch (c)
86         {
87         case O_VERBOSE:
88           opt_v = 1;
89           break;
90         case O_VERSION:
91 #ifdef VERSION
92           fprintf(stdout, "Version %s\n", VERSION);
93 #else
94           fprintf(stderr, "Unknown version number");
95 #endif
96           exit(0);
97         case O_HELP:
98         case '?':
99           rs2sdf_usage(argv[0]);
100           return (0);
101         default:
102           rs2sdf_usage(argv[0]);
103           return (1);
104         }
105     }
106   
107   if (argc - optind != 2) {
108     rs2sdf_usage(argv[0]);
109     return (1);
110   }
111
112   rs_name = argv[optind];
113   im_name = argv[optind + 1];
114
115   if (file_exists(rs_name) == FALSE) {
116     fprintf (stderr, "Raysum file %s does not exist\n", rs_name);
117     return (1);
118   }
119   
120   rs = raysum_open (rs_name);
121
122   if (opt_v)
123     {
124       printf ("Number of detectors: %d\n", rs->ndet);
125       printf ("    Number of views: %d\n", rs->nview);
126       printf ("             Remark: %s\n", rs->remark);
127     }
128   
129   im = image_create (im_name, rs->ndet, rs->nview);
130   
131   sdf_add_label (LT_HISTORY, rs->remark, rs->calctime, im->dfp_2d->dfp);
132   sdf_add_empty_label (im->dfp_2d->dfp);
133
134   detarray = detarray_alloc (rs->ndet);
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           im->v[ix][iy] = detarray->detval[ix];
141         }
142     }
143   detarray_free (detarray);
144   
145   raysum_close (rs);
146   image_save (im);
147   
148   return(0);
149 }
150
151 #ifndef NO_MAIN
152 int 
153 main (const int argc, char *const argv[])
154 {
155   return (rs2sdf_main(argc, argv));
156 }
157 #endif