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