r56: Improved option processing
[ctsim.git] / src / sdf-2.c
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: sdf-2.c,v 1.4 2000/05/16 04:33:59 kevin Exp $
6 **  $Log: sdf-2.c,v $
7 **  Revision 1.4  2000/05/16 04:33:59  kevin
8 **  Improved option processing
9 **
10 **  Revision 1.3  2000/05/11 01:06:30  kevin
11 **  Changed sprintf to snprintf
12 **
13 **  Revision 1.2  2000/05/08 20:02:32  kevin
14 **  ANSI C changes
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
35 /* FILE
36  *   sdf-2.c         Generate a SDF file from two input SDF files
37  */
38
39 #include "ct.h"
40
41 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
42
43 static struct option my_options[] =
44 {
45   {"add", 0, 0, O_ADD},
46   {"sub", 0, 0, O_SUB},
47   {"mul", 0, 0, O_MUL},
48   {"comp", 0, 0, O_COMP},
49   {"verbose", 0, 0, O_VERBOSE},
50   {"help", 0, 0, O_HELP},
51   {"version", 0, 0, O_VERSION},
52   {0, 0, 0, 0}
53 };
54
55 void 
56 sdf2_usage (const char *program)
57 {
58   fprintf(stdout, "sdf2_usage: %s infile1 infile2 outfile [OPTIONS]\n", kbasename(program));
59   fprintf(stdout, "Generate an SDF2D file from two input SDF2D files\n");
60   fprintf(stdout, "\n");
61   fprintf(stdout, "     infile1    Name of first input SDF file\n");
62   fprintf(stdout, "     infile2    Name of second input SDF file\n");
63   fprintf(stdout, "     outfile    Name of output SDF file\n");
64   fprintf(stdout, "     --add      Add images\n");
65   fprintf(stdout, "     --sub      Subtract image 2 from image 1\n");
66   fprintf(stdout, "     --mul      Multiply images\n");
67   fprintf(stdout, "     --comp     Compare images\n");
68   fprintf(stdout, "     --verbose  Verbose modem\n");
69   fprintf(stdout, "     --version  Print version\n");
70   fprintf(stdout, "     --help     Print this help message\n");
71 }
72
73 int 
74 sdf2_main (int argc, char *const argv[])
75 {
76   IMAGE *im_in1;
77   IMAGE *im_in2;
78   IMAGE *im_out;
79   char *in_file1;
80   char *in_file2;
81   char *out_file;
82   int opt_verbose = 0;
83   int opt_add = 0;
84   int opt_sub = 0;
85   int opt_mul = 0;
86   int opt_comp = 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_ADD:
98           opt_add = 1;
99           break;
100         case O_SUB :
101           opt_sub = 1;
102           break;
103         case O_MUL:
104           opt_mul = 1;
105           break;
106         case O_COMP:
107           opt_comp = 1;
108           break;
109         case O_VERBOSE:
110           opt_verbose = 1;
111           break;
112         case O_VERSION:
113 #ifdef VERSION
114           fprintf(stdout, "Version %s\n", VERSION);
115 #else
116           fprintf(stderr, "Unknown version number");
117 #endif
118           exit(0);
119         case O_HELP:
120         case '?':
121           sdf2_usage(argv[0]);
122           return (0);
123         default:
124           sdf2_usage(argv[0]);
125           return (1);
126         }
127     }
128
129   if (optind + 3 != argc)
130     {
131       sdf2_usage(argv[0]);
132       return (1);
133     }
134   
135   in_file1 = argv[optind];
136   in_file2 = argv[optind + 1];
137   out_file = argv[optind + 2];
138
139   im_in1 = image_load (in_file1);
140   im_in2 = image_load (in_file2);
141   if (im_in1->nx != im_in2->nx || im_in1->ny != im_in2->ny) {
142     fprintf(stderr, "Size of image 1 (%d,%d) and image 2 (%d,%d) do not match\n",
143             im_in1->nx, im_in1->ny, im_in2->nx, im_in2->ny);
144     exit(1);
145   }
146   im_out = image_create (out_file, im_in1->nx, im_in1->ny);
147
148   if (opt_add) {
149     int ix, iy;
150     for (ix = 0; ix < im_in1->nx; ix++)
151       {
152         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
153         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
154         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
155         for (iy = 0; iy < im_in1->ny; iy++)
156           *out++ = *in1++ + *in2++;
157       }
158   } else if (opt_sub) {
159     int ix, iy;
160     for (ix = 0; ix < im_in1->nx; ix++)
161       {
162         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
163         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
164         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
165         for (iy = 0; iy < im_in1->ny; iy++)
166           *out++ = *in1++ - *in2++;
167       }
168   } else if (opt_mul) {
169     int ix, iy;
170     for (ix = 0; ix < im_in1->nx; ix++)
171       {
172         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
173         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
174         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
175         for (iy = 0; iy < im_in1->ny; iy++)
176           *out++ = *in1++ * *in2++;
177       }
178   } else if (opt_comp) {
179     int ix, iy;
180     double abs_error = 0.;
181     for (ix = 0; ix < im_in1->nx; ix++)
182       {
183         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
184         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
185         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
186         for (iy = 0; iy < im_in1->ny; iy++)
187           {
188             double diff = *in1++ - *in2++;
189             *out++ = diff;
190             abs_error += fabs(diff);
191           }
192       }
193     abs_error /= (im_in1->nx * im_in1->ny);
194     fprintf(stdout, "Average Error = %f\n", abs_error);
195   }
196
197   image_save(im_out);
198
199   return (0);
200 }
201
202 #ifndef NO_MAIN
203 int 
204 main (int argc, char *const argv[])
205 {
206   return (sdf2_main(argc, argv));
207 }
208 #endif
209