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