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