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