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