r79: *** empty log message ***
[ctsim.git] / src / sdf-2.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: sdf-2.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 **  $Log: sdf-2.cpp,v $
7 **  Revision 1.1  2000/06/07 02:29:05  kevin
8 **  Initial C++ versions
9 **
10 **  Revision 1.5  2000/05/24 22:50:04  kevin
11 **  Added support for new SGP library
12 **
13 **  Revision 1.4  2000/05/16 04:33:59  kevin
14 **  Improved option processing
15 **
16 **  Revision 1.3  2000/05/11 01:06:30  kevin
17 **  Changed sprintf to snprintf
18 **
19 **  Revision 1.2  2000/05/08 20:02:32  kevin
20 **  ANSI C changes
21 **
22 **  Revision 1.1.1.1  2000/04/28 13:02:44  kevin
23 **  Initial CVS import for first public release
24 **
25 **
26 **
27 **  This program is free software; you can redistribute it and/or modify
28 **  it under the terms of the GNU General Public License (version 2) as
29 **  published by the Free Software Foundation.
30 **
31 **  This program is distributed in the hope that it will be useful,
32 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
33 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34 **  GNU General Public License for more details.
35 **
36 **  You should have received a copy of the GNU General Public License
37 **  along with this program; if not, write to the Free Software
38 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39 ******************************************************************************/
40
41 /* FILE
42  *   sdf-2.c         Generate a SDF file from two input SDF files
43  */
44
45 #include "ct.h"
46
47 enum {O_ADD, O_SUB, O_MUL, O_COMP, O_VERBOSE, O_HELP, O_VERSION};
48
49 static struct option my_options[] =
50 {
51   {"add", 0, 0, O_ADD},
52   {"sub", 0, 0, O_SUB},
53   {"mul", 0, 0, O_MUL},
54   {"comp", 0, 0, O_COMP},
55   {"verbose", 0, 0, O_VERBOSE},
56   {"help", 0, 0, O_HELP},
57   {"version", 0, 0, O_VERSION},
58   {0, 0, 0, 0}
59 };
60
61 void 
62 sdf2_usage (const char *program)
63 {
64   fprintf(stdout, "sdf2_usage: %s infile1 infile2 outfile [OPTIONS]\n", kbasename(program));
65   fprintf(stdout, "Generate an SDF2D file from two input SDF2D files\n");
66   fprintf(stdout, "\n");
67   fprintf(stdout, "     infile1    Name of first input SDF file\n");
68   fprintf(stdout, "     infile2    Name of second input SDF file\n");
69   fprintf(stdout, "     outfile    Name of output SDF file\n");
70   fprintf(stdout, "     --add      Add images\n");
71   fprintf(stdout, "     --sub      Subtract image 2 from image 1\n");
72   fprintf(stdout, "     --mul      Multiply images\n");
73   fprintf(stdout, "     --comp     Compare images\n");
74   fprintf(stdout, "     --verbose  Verbose modem\n");
75   fprintf(stdout, "     --version  Print version\n");
76   fprintf(stdout, "     --help     Print this help message\n");
77 }
78
79 int 
80 sdf2_main (int argc, char *const argv[])
81 {
82   IMAGE *im_in1;
83   IMAGE *im_in2;
84   IMAGE *im_out;
85   char *in_file1;
86   char *in_file2;
87   char *out_file;
88   int opt_verbose = 0;
89   int opt_add = 0;
90   int opt_sub = 0;
91   int opt_mul = 0;
92   int opt_comp = 0;
93
94   while (1)
95     {
96       int c = getopt_long (argc, argv, "", my_options, NULL);
97       
98       if (c == -1)
99         break;
100       
101       switch (c)
102         {
103         case O_ADD:
104           opt_add = 1;
105           break;
106         case O_SUB :
107           opt_sub = 1;
108           break;
109         case O_MUL:
110           opt_mul = 1;
111           break;
112         case O_COMP:
113           opt_comp = 1;
114           break;
115         case O_VERBOSE:
116           opt_verbose = 1;
117           break;
118         case O_VERSION:
119 #ifdef VERSION
120           fprintf(stdout, "Version %s\n", VERSION);
121 #else
122           fprintf(stderr, "Unknown version number");
123 #endif
124           exit(0);
125         case O_HELP:
126         case '?':
127           sdf2_usage(argv[0]);
128           return (0);
129         default:
130           sdf2_usage(argv[0]);
131           return (1);
132         }
133     }
134
135   if (optind + 3 != argc)
136     {
137       sdf2_usage(argv[0]);
138       return (1);
139     }
140   
141   in_file1 = argv[optind];
142   in_file2 = argv[optind + 1];
143   out_file = argv[optind + 2];
144
145   im_in1 = image_load (in_file1);
146   im_in2 = image_load (in_file2);
147   if (im_in1 == NULL || im_in2 == NULL) {
148       fprintf(stderr, "Error reading an image");
149       return (1);
150   }
151
152   if (im_in1->nx != im_in2->nx || im_in1->ny != im_in2->ny) {
153     fprintf(stderr, "Error: Size of image 1 (%d,%d) and image 2 (%d,%d) do not match\n",
154             im_in1->nx, im_in1->ny, im_in2->nx, im_in2->ny);
155     return(1);
156   }
157   if (im_in1->nx < 0 || im_in1->ny < 0) {
158       fprintf(stderr, "Error: Size of image < 0");
159       return(1);
160   }
161
162   im_out = image_create (out_file, im_in1->nx, im_in1->ny);
163
164   if (opt_add) {
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_sub) {
175     int ix, iy;
176     for (ix = 0; ix < im_in1->nx; ix++)
177       {
178         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
179         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
180         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
181         for (iy = 0; iy < im_in1->ny; iy++)
182           *out++ = *in1++ - *in2++;
183       }
184   } else if (opt_mul) {
185     int ix, iy;
186     for (ix = 0; ix < im_in1->nx; ix++)
187       {
188         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
189         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
190         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
191         for (iy = 0; iy < im_in1->ny; iy++)
192           *out++ = *in1++ * *in2++;
193       }
194   } else if (opt_comp) {
195     int ix, iy;
196     double abs_error = 0.;
197     for (ix = 0; ix < im_in1->nx; ix++)
198       {
199         IMAGE_ELEM_VAL *in1 = &im_in1->v[ix][0];
200         IMAGE_ELEM_VAL *in2 = &im_in2->v[ix][0];
201         IMAGE_ELEM_VAL *out = &im_out->v[ix][0];
202         for (iy = 0; iy < im_in1->ny; iy++)
203           {
204             double diff = *in1++ - *in2++;
205             *out++ = diff;
206             abs_error += fabs(diff);
207           }
208       }
209     abs_error /= (im_in1->nx * im_in1->ny);
210     fprintf(stdout, "Average Error = %f\n", abs_error);
211   }
212
213   image_save(im_out);
214
215   return (0);
216 }
217
218 #ifndef NO_MAIN
219 int 
220 main (int argc, char *const argv[])
221 {
222   return (sdf2_main(argc, argv));
223 }
224 #endif
225