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