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