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