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