Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PTScriptParsing.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
25 #include "PTScriptParsing.h"
26 
27 #include <hugin_utils/utils.h>
28 #include <stdio.h>
29 
30 
31 namespace HuginBase {
32 namespace PTScriptParsing {
33 
34 
35 
37 bool getPTParam(std::string & output, const std::string & line, const std::string & parameter)
38 {
39  int len = line.size();
40  for (int i=1; i < len; i++) {
41  if (line[i-1] == ' ' && line[i] != ' ') {
42  // beginning of a parameter
43  std::string par = line.substr(i, parameter.length());
44  if (par == parameter) {
45  // found, skip to value
46  i = i+parameter.length();
47  if ( i >= len ) {
48  // parameter without value, not valid.
49  output = "";
50  return true;
51  }
52  if (line[i]== '"') {
53  i++;
54  if ( i >= len ) {
55  return false;
56  }
57  // this is a string parameter, skip to next "
58  size_t end = line.find('"', i);
59  if (end == std::string::npos) {
60  // unclosed string found
61  return false;
62  }
63  output = line.substr(i,end-i);
64  return true;
65  } else {
66  // ordinary parameter, skip to next space
67  size_t end = line.find_first_of(" \t\n", i);
68  output = line.substr(i, end-i);
69  return true;
70  }
71  } else {
72  // this is another parameter, skip it
73  i++;
74  if ( i >= len ) {
75  return false;
76  }
77  if (line[i]== '"') {
78  i++;
79  // this is a string parameter, skip to next "
80  size_t end = line.find('"', i);
81  if (end == std::string::npos) {
82  // unclosed string found
83  return false;
84  }
85  i = end;
86  if ( i >= len ) {
87  return false;
88  }
89  } else {
90  // this is an ordinary parameter, skip to next space
91  size_t end = line.find_first_of(" \t\n", i);
92  if (end == std::string::npos) {
93  // not found, last parameter
94  return false;
95  }
96  i = end;
97  }
98  }
99  }
100  }
101  return false;
102 }
103 
104 #if 0
105 bool getPTParam(std::string & output, const std::string & line, const std::string & parameter)
106 {
107  std::string::size_type p;
108  if ((p=line.find(std::string(" ") + parameter)) == std::string::npos) {
109  DEBUG_INFO("could not find param " << parameter
110  << " in line: " << line);
111  return false;
112  }
113  p += parameter.length() + 1;
114  std::string::size_type p2 = line.find(' ',p);
115  output = line.substr(p, p2-p);
116  // DEBUG_DEBUG("string idex: " << p <<"," << p2 << " string: \"" << output << "\"");
117  return true;
118 }
119 
120 bool getPTStringParam(std::string & output, const std::string & line, const std::string & parameter)
121 {
122  std::string::size_type p;
123  if ((p=line.find(std::string(" ") + parameter + "\"")) == std::string::npos) {
124  DEBUG_INFO("could not find string param " << parameter
125  << " in line: " << line);
126  return false;
127  }
128  p += parameter.length() + 2;
129  std::string::size_type e = line.find("\"",p);
130  DEBUG_DEBUG("p:" << p << " e:" << e);
131  output = line.substr(p,e-p);
132  DEBUG_DEBUG("output: ##" << output << "##");
133  return true;
134 }
135 
136 bool getPTStringParamColon(std::string & output, const std::string & line, const std::string & parameter)
137 {
138  std::string::size_type p;
139  if ((p=line.find(std::string(" ") + parameter + ":")) == std::string::npos) {
140  DEBUG_INFO("could not find string param " << parameter
141  << " in line: " << line);
142  return false;
143  }
144  p += parameter.length() + 2;
145  std::string::size_type e = line.find(" ",p);
146  DEBUG_DEBUG("p:" << p << " e:" << e);
147  output = line.substr(p,e-p);
148  DEBUG_DEBUG("output: ##" << output << "##");
149  return true;
150 }
151 #endif
152 
153 bool getDoubleParam(double & d, const std::string & line, const std::string & name)
154 {
155  std::string s;
156  if (!getPTParam(s, line, name)) {
157  return false;
158  }
159  return hugin_utils::stringToDouble(s, d);
160 }
161 
162 bool getPTDoubleParam(double & value, int & link,
163  const std::string & line, const std::string & var)
164 {
165  std::string val;
166  if (getPTParam(val,line, var)) {
167  DEBUG_ASSERT(!line.empty());
168  DEBUG_DEBUG(var << ":" <<val);
169  if (val[0] == '=') {
170  if (!hugin_utils::stringToInt(val.substr(1), link))
171  {
172  return false;
173  };
174  } else {
175  link = -1;
176  if (!hugin_utils::stringToDouble(val, value)) {
177  return false;
178  }
179  }
180  } else {
181  return false;
182  }
183  return true;
184 }
185 
186 bool readVar(Variable & var, int & link, const std::string & line)
187 {
188  std::string val;
189  if (getPTParam(val,line, var.getName())) {
190  DEBUG_ASSERT(!line.empty());
191  DEBUG_DEBUG(var.getName() << ":" <<val);
192  if (val[0] == '=') {
193  if (!hugin_utils::stringToInt(val.substr(1), link))
194  {
195  return false;
196  };
197  } else {
198  link = -1;
199  double dest = 0;
200  if (!hugin_utils::stringToDouble(val, dest)) {
201  return false;
202  }
203  var.setValue(dest);
204  }
205  } else {
206  return false;
207  }
208  return true;
209 }
210 
211 
212 
213 
214 
215 // cannot use Lens::variableNames here, because r,p,v,j need to be included
217 const char * ImgInfo::varnames[] = {"v", "a","b","c", "d","e", "g","t", "r","p","y","j","TrX", "TrY", "TrZ", "Tpy", "Tpp",
218  "Va", "Vb", "Vc", "Vd", "Vx", "Vy",
219  "Eev", "Er", "Eb",
220  "Ra", "Rb", "Rc", "Rd", "Re", 0};
221 
222 double ImgInfo::defaultValues[] = {51.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
223  1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
224  0.0, 1.0, 1.0,
225  0.0, 0.0, 0.0, 0.0, 0.0};
226 
227 
229 {
230  // blend_radius = 0;
231  width = -1;
232  height = -1;
233  f = -2;
234  vigcorrMode = 0; // default to no correction
235  // is transformed to correction by division later
236  responseType = 0; // default to EMOR
237  for (const char ** v = varnames; *v != 0; v++) {
238  vars[*v] = 0;
239  links[*v] = -2;
240  }
241  autoCenterCrop = true;
242  cropFactor = 1;
243  enabled = true;
244 }
245 
246 
247 void ImgInfo::parse(const std::string & line)
248 {
249  double * val = defaultValues;
250  for (const char ** v = varnames; *v; v++, val++) {
251  vars[*v] = *val;
252  links[*v] = -1;
253  std::string name;
254  name = *v;
255  getPTDoubleParam(vars[*v], links[*v], line, name);
256  }
257 
258  // getIntParam(blend_radius, line, "u");
259 
260  // read lens type and hfov
261  getIntParam(f, line, "f");
262 
263  getPTParam(filename,line,"n");
264  getIntParam(width, line, "w");
265  getIntParam(height, line, "h");
266 
267  getIntParam(vigcorrMode, line, "Vm");
268  // HACK: force Va1, for all images that use the a polynomial vig correction mode.
269  // reset to vignetting correction by division.
270  if (vigcorrMode != 5) {
271  vigcorrMode = 5;
272  vars["Va"] = 1.0;
273  vars["Vb"] = 0.0;
274  vars["Vc"] = 0.0;
275  vars["Vd"] = 0.0;
276  }
277 
278  getIntParam(responseType, line, "Rt");
279  getPTParam(flatfieldname,line,"Vf");
280 
281  std::string crop_str;
282  if ( getPTParam(crop_str, line, "C") ) {
283  int left, right, top, bottom;
284  int n = sscanf(crop_str.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
285  if (n == 4) {
286  crop = vigra::Rect2D(left, top, right, bottom);
287  } else {
288  DEBUG_WARN("Could not parse crop string: " << crop_str);
289  }
290  }
291  if ( getPTParam(crop_str, line, "S") ) {
292  int left, right, top, bottom;
293  int n = sscanf(crop_str.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
294  if (n == 4) {
295  crop = vigra::Rect2D(left, top, right, bottom);
296  } else {
297  DEBUG_WARN("Could not parse crop string: " << crop_str);
298  }
299  }
300 
301 
302 }
303 
304 
305 
306 }
307 } // namespace
#define DEBUG_INFO(msg)
Definition: utils.h:69
bool readVar(Variable &var, int &link, const std::string &line)
const std::string & getName() const
bool getIntParam(T &value, const std::string &line, const std::string &name)
a variable has a value and a name.
#define DEBUG_ASSERT(cond)
Definition: utils.h:80
static char * line
Definition: svm.cpp:2784
bool getPTParam(std::string &output, const std::string &line, const std::string &parameter)
helper functions for parsing of a script line
std::map< std::string, double > vars
#define DEBUG_WARN(msg)
Definition: utils.h:74
bool stringToInt(const std::string &s, int &val)
convert string to integer value, returns true, if sucessful
Definition: utils.cpp:264
bool stringToDouble(const STR &str_, double &dest)
convert a string to a double, ignore localisation.
Definition: utils.h:114
std::map< std::string, int > links
#define DEBUG_DEBUG(msg)
Definition: utils.h:68
bool getDoubleParam(double &d, const std::string &line, const std::string &name)
bool getPTDoubleParam(double &value, int &link, const std::string &line, const std::string &var)
void setValue(double v)
void parse(const std::string &line)