First version of ImageScaler Widget
[mussa.git] / mussa.cxx
1 //  This file is part of the Mussa source distribution.
2 //  http://mussa.caltech.edu/
3 //  Contact author: Tristan  De Buysscher, tristan@caltech.edu
4
5 // This program and all associated source code files are Copyright (C) 2005
6 // the California Institute of Technology, Pasadena, CA, 91125 USA.  It is
7 // under the GNU Public License; please see the included LICENSE.txt
8 // file for more information, or contact Tristan directly.
9
10
11 #include "gui/ConnWindow.hh"
12 #include "alg/mussa_class.hh"
13 #include "mussa_exceptions.hh"
14 #include <iostream>
15 using namespace std;
16
17 char
18 parse_args(int argc, char **argv, string *a_file_path, int *window, 
19            int *threshold, enum Mussa::analysis_modes *ana_mode, float *ent_thres);
20
21
22 int main(int argc, char **argv) 
23 {
24   Mussa an_analysis;
25   string a_file_path;
26   char * picked_file;
27   int window, threshold;
28   float ent_thres;
29   char run_mode;
30   enum Mussa::analysis_modes ana_mode;
31   int x_max=1000;
32   int y_max=500;
33   string err_msg;
34
35
36   err_msg = "";
37
38   // yeah, its horrible to be passing these by reference, but I just don't see
39   // any other way without making parse_args part of MussaClass, which just
40   // seems wrong (args are after all, an io type thing, and the gui only mode
41   // will have it own way as well...
42   run_mode = parse_args(argc, argv, &a_file_path, &window, &threshold,
43                         &ana_mode, &ent_thres);
44
45   cout << "mussa: run mode = " << run_mode;
46   cout << "\tfile_path = "<< a_file_path << endl;
47   cout << "mussa: ent_thres = " << ent_thres << endl;
48
49   // if no error from parse args (run_mode = 'e', ie error), run in proper mode
50   if (run_mode != 'e')
51   {
52     if ((run_mode == 'f') || (run_mode == 'n'))
53     {
54       err_msg = an_analysis.load_mupa_file(a_file_path);
55       an_analysis.analyze(window, threshold, ana_mode, ent_thres);
56       //an_overlord.do_analysis();
57     }
58
59     if (run_mode == 'v')
60       err_msg = an_analysis.load(a_file_path);
61       //an_overlord.get_analysis();
62
63     // no longer needed, but still semi-useful reality check...
64     if (run_mode == 'g')
65     {
66       cout << "GTV - All Gui, All the Time\n";
67     }
68
69     if (err_msg == "")
70     {
71       if  ((run_mode == 'f') || (run_mode == 'v') || (run_mode == 'g'))
72       {
73         //an_overlord.spawnConnView(1000,500);
74         ConnWindow *a_conn_win = new ConnWindow(x_max, y_max, "Mussa");
75
76         // we have an analysis already if in these 2 modes
77         if  ((run_mode == 'f') || (run_mode == 'v'))
78           a_conn_win->add_ana(&an_analysis);
79
80         Fl::run();
81       }
82     }
83     else
84       cout << err_msg << endl;
85   }
86 }
87
88
89 // minimal arg reading function, not very robust to errors
90 char
91 parse_args(int argc, char **argv, string *a_file_path, int *window, 
92            int *threshold, enum Mussa::analysis_modes *ana_mode, float *ent_thres)
93 {
94   int i, else_i;
95   string an_arg;
96   char run_mode;
97
98   // initialize these to 0 as flag if they are not changed
99   *window = 0;
100   *threshold = 0;
101   *ent_thres = 0.0;
102   run_mode = 'e'; //error default if no run mode set implicitly or explicitly
103   *ana_mode = Mussa::TransitiveNway; // default to transitivie analyses mode
104
105   // no args means gui only mode
106   if (argc == 1)
107     run_mode = 'g';
108   else
109   {
110     else_i = 0;
111     i = 1;
112     while (i < argc)
113     {
114       an_arg = * ++argv;
115       i++;
116
117       // see what alternate mode the user wants to run in
118       if (an_arg == "-m")
119       {
120         an_arg = * ++argv;
121         i++;
122         if ( (an_arg == "v") || (an_arg == "n") ) //only 2 valid modes so far
123           run_mode = an_arg[0];
124         else
125         {
126           cout << "Error: \'" << an_arg;
127           cout << "\' is not a valid analysis mode for -a argument" << endl; 
128           throw cmdline_error("not valid -a argument");
129         }
130         *a_file_path = * ++argv;
131         i++;
132       }
133       // alternate analyses modes
134       else if (an_arg == "-a")
135       {
136         an_arg = * ++argv;
137         i++;
138         // t = transitive, r = radial, e = entropy
139         if (an_arg == "t") *ana_mode = Mussa::TransitiveNway;
140         else if (an_arg == "r") *ana_mode = Mussa::RadialNway;
141         else if (an_arg == "e") *ana_mode = Mussa::EntropyNway;
142         else
143         {
144           cout << "Error: \'" << an_arg;
145           cout << "\' is not a valid run mode for -m argument" << endl; 
146           throw cmdline_error("bad argument -m");
147         }
148       }
149       else if (an_arg == "-w") // alternate window size arg
150       {
151         *window = atoi(* ++argv);
152         i++;
153       }
154       else if (an_arg == "-t") // alternate threshold arg
155       {
156         *threshold = atoi(* ++argv);
157         i++;
158       }
159       else if (an_arg == "-et") // alternate entropy threshold arg
160       {
161         *ent_thres = atof(* ++argv);
162         i++;
163       }
164       else
165       {
166         if (else_i == 0)
167         {
168           *a_file_path = an_arg;
169           run_mode = 'f';
170           else_i++;
171         }
172         else
173         {
174           //cout << "Error, unknown arg: \'" << an_arg << "\'" << endl;
175           cout << "Error, too many filenames: \'" << an_arg << "\'" << endl;
176           run_mode == 'e';
177         }
178       }
179     }
180   }
181   return run_mode;
182 }
183
184
185 /*
186       cout << "fee\n";
187       cout << "fie\n";
188       cout << "foe\n";
189       cout << "fum\n";
190 */