r41: ANSI C changes
[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.3 2000/05/08 20:02:32 kevin Exp $
6 **  $Log: sdf-1.c,v $
7 **  Revision 1.3  2000/05/08 20:02:32  kevin
8 **  ANSI C changes
9 **
10 **  Revision 1.2  2000/05/03 08:49:50  kevin
11 **  Code cleanup
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 /* FILE
32  *   sdf-1.c             Filter a single SDF file
33  */
34
35 #include "ct.h"
36
37 enum {O_LOG, O_EXP, O_INVERT, O_VERBOSE, O_HELP, O_VERSION};
38
39 static struct option my_options[] =
40 {
41   {"invert", 0, 0, O_INVERT},
42   {"verbose", 0, 0, O_VERBOSE},
43   {"log", 0, 0, O_LOG},
44   {"exp", 0, 0, O_EXP},
45   {"help", 0, 0, O_HELP},
46   {"version", 0, 0, O_VERSION},
47   {0, 0, 0, 0}
48 };
49
50 void 
51 usage (const char *program)
52 {
53   fprintf(stdout, "usage: %s infile outfile [OPTIONS]\n", kbasename(program));
54   fprintf(stdout, "Generate a SDF2D file from a SDF2D file\n");
55   fprintf(stdout, "\n");
56   fprintf(stdout, "     --invert   Invert image\n");
57   fprintf(stdout, "     --log      Natural logrithm of image\n");
58   fprintf(stdout, "     --exp      Natural exponential of image\n");
59   fprintf(stdout, "     --verbose  Verbose modem\n");
60   fprintf(stdout, "     --version  Print version\n");
61   fprintf(stdout, "     --help     Print this help message\n");
62   exit(1);
63 }
64
65 int 
66 main (int argc, char *const argv[])
67 {
68   IMAGE *im_in;
69   IMAGE *im_out;
70   char *in_file;
71   char *out_file;
72   int opt_verbose = 0;
73   int opt_invert = 0;
74   int opt_log = 0;
75   int opt_exp = 0;
76
77   while (1)
78     {
79       int c = getopt_long (argc, argv, "", my_options, NULL);
80       
81       if (c == -1)
82         break;
83       
84       switch (c)
85         {
86         case O_INVERT:
87           opt_invert = 1;
88           break;
89         case O_LOG:
90           opt_log = 1;
91           break;
92         case O_EXP:
93           opt_exp = 1;
94           break;
95         case O_VERBOSE:
96           opt_verbose = 1;
97           break;
98         case O_VERSION:
99 #ifdef VERSION
100           fprintf(stdout, "Version %s\n", VERSION);
101 #else
102           fprintf(stderr, "Unknown version number");
103 #endif
104           exit(0);
105         case O_HELP:
106         case '?':
107           usage(argv[0]);
108           exit(0);
109         default:
110           usage(argv[0]);
111           exit(1);
112         }
113     }
114
115   if (optind + 2 != argc)
116     {
117       usage(argv[0]);
118       exit(1);
119     }
120   
121   in_file = argv[optind];
122   out_file = argv[optind + 1];
123
124
125   if (opt_invert || opt_log || opt_exp) {
126     int ix, iy;
127
128     im_in = image_load (in_file);
129     im_out = image_create (out_file, im_in->nx, im_in->ny);
130
131     if (opt_invert) {
132       for (ix = 0; ix < im_in->nx; ix++)
133         for (iy = 0; iy < im_in->ny; iy++)
134           im_out->v[ix][iy] = - im_in->v[ix][iy];
135     }
136     if (opt_log) {
137       for (ix = 0; ix < im_in->nx; ix++)
138         for (iy = 0; iy < im_in->ny; iy++)
139           im_out->v[ix][iy] = log (im_in->v[ix][iy]);
140     }
141     if (opt_exp) {
142       for (ix = 0; ix < im_in->nx; ix++)
143         for (iy = 0; iy < im_in->ny; iy++)
144           im_out->v[ix][iy] = exp (im_in->v[ix][iy]);
145     }
146
147     image_save(im_out);
148   }
149
150   return (0);
151 }