r70: added imagefiles
[ctsim.git] / src / sdf-1.c
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: sdf-1.c,v 1.6 2000/05/24 22:50:04 kevin Exp $
6 **  $Log: sdf-1.c,v $
7 **  Revision 1.6  2000/05/24 22:50:04  kevin
8 **  Added support for new SGP library
9 **
10 **  Revision 1.5  2000/05/16 04:33:59  kevin
11 **  Improved option processing
12 **
13 **  Revision 1.4  2000/05/09 14:52:27  kevin
14 **  added sqr and sqrt functions
15 **
16 **  Revision 1.3  2000/05/08 20:02:32  kevin
17 **  ANSI C changes
18 **
19 **  Revision 1.2  2000/05/03 08:49:50  kevin
20 **  Code cleanup
21 **
22 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
23 **  Initial CVS import for first public release
24 **
25 **
26 **
27 **  This program is free software; you can redistribute it and/or modify
28 **  it under the terms of the GNU General Public License (version 2) as
29 **  published by the Free Software Foundation.
30 **
31 **  This program is distributed in the hope that it will be useful,
32 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
33 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34 **  GNU General Public License for more details.
35 **
36 **  You should have received a copy of the GNU General Public License
37 **  along with this program; if not, write to the Free Software
38 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39 ******************************************************************************/
40
41 /* FILE
42  *   sdf-1.c             Filter a single SDF file
43  */
44
45 #include "ct.h"
46
47 enum {O_LOG, O_EXP, O_SQRT, O_SQR, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
48
49 static struct option my_options[] =
50 {
51   {"invert", 0, 0, O_INVERT},
52   {"verbose", 0, 0, O_VERBOSE},
53   {"log", 0, 0, O_LOG},
54   {"exp", 0, 0, O_EXP},
55   {"sqr", 0, 0, O_SQR},
56   {"sqrt", 0, 0, O_SQRT},
57   {"help", 0, 0, O_HELP},
58   {"version", 0, 0, O_VERSION},
59   {0, 0, 0, 0}
60 };
61
62 void 
63 sdf1_usage (const char *program)
64 {
65   fprintf(stdout, "sdf1_usage: %s infile outfile [OPTIONS]\n", kbasename(program));
66   fprintf(stdout, "Generate a SDF2D file from a SDF2D file\n");
67   fprintf(stdout, "\n");
68   fprintf(stdout, "     --invert   Invert image\n");
69   fprintf(stdout, "     --log      Natural logrithm of image\n");
70   fprintf(stdout, "     --exp      Natural exponential of image\n");
71   fprintf(stdout, "     --sqr      Square of image\n");
72   fprintf(stdout, "     --sqrt     Square root of image\n");
73   fprintf(stdout, "     --verbose  Verbose modem\n");
74   fprintf(stdout, "     --version  Print version\n");
75   fprintf(stdout, "     --help     Print this help message\n");
76 }
77
78 int 
79 sdf1_main (int argc, char *const argv[])
80 {
81   IMAGE *im_in;
82   IMAGE *im_out;
83   char *in_file;
84   char *out_file;
85   int opt_verbose = 0;
86   int opt_invert = 0;
87   int opt_log = 0;
88   int opt_exp = 0;
89   int opt_sqr = 0;
90   int opt_sqrt = 0;
91
92   while (1)
93     {
94       int c = getopt_long (argc, argv, "", my_options, NULL);
95       
96       if (c == -1)
97         break;
98       
99       switch (c)
100         {
101         case O_INVERT:
102           opt_invert = 1;
103           break;
104         case O_LOG:
105           opt_log = 1;
106           break;
107         case O_SQR:
108           opt_sqr = 1;
109           break;
110         case O_SQRT:
111           opt_sqrt = 1;
112           break;
113         case O_EXP:
114           opt_exp = 1;
115           break;
116         case O_VERBOSE:
117           opt_verbose = 1;
118           break;
119         case O_VERSION:
120 #ifdef VERSION
121           fprintf(stdout, "Version %s\n", VERSION);
122 #else
123           fprintf(stderr, "Unknown version number");
124 #endif
125           exit(0);
126         case O_HELP:
127         case '?':
128           sdf1_usage(argv[0]);
129           return (0);
130         default:
131           sdf1_usage(argv[0]);
132           return (1);
133         }
134     }
135
136   if (optind + 2 != argc)
137     {
138       sdf1_usage(argv[0]);
139       return (1);
140     }
141   
142   in_file = argv[optind];
143   out_file = argv[optind + 1];
144
145
146   if (opt_invert || opt_log || opt_exp || opt_sqr || opt_sqrt) {
147     int ix, iy;
148
149     im_in = image_load (in_file);
150     im_out = image_create (out_file, im_in->nx, im_in->ny);
151
152     if (opt_invert) {
153       for (ix = 0; ix < im_in->nx; ix++)
154         for (iy = 0; iy < im_in->ny; iy++)
155           im_out->v[ix][iy] = - im_in->v[ix][iy];
156     }
157     if (opt_log) {
158       for (ix = 0; ix < im_in->nx; ix++)
159         for (iy = 0; iy < im_in->ny; iy++)
160           im_out->v[ix][iy] = log (im_in->v[ix][iy]);
161     }
162     if (opt_exp) {
163       for (ix = 0; ix < im_in->nx; ix++)
164         for (iy = 0; iy < im_in->ny; iy++)
165           im_out->v[ix][iy] = exp (im_in->v[ix][iy]);
166     }
167     if (opt_sqr) {
168       for (ix = 0; ix < im_in->nx; ix++)
169         for (iy = 0; iy < im_in->ny; iy++)
170           im_out->v[ix][iy] = im_in->v[ix][iy] * im_in->v[ix][iy];
171     }
172     if (opt_sqrt) {
173       for (ix = 0; ix < im_in->nx; ix++)
174         for (iy = 0; iy < im_in->ny; iy++)
175           im_out->v[ix][iy] = sqrt (im_in->v[ix][iy]);
176     }
177
178     image_save(im_out);
179   }
180
181   return (0);
182 }
183
184 #ifndef NO_MAIN
185 int 
186 main (int argc, char *const argv[])
187 {
188   return (sdf1_main(argc, argv));
189 }
190 #endif
191