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