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