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