PTScriptParsing.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00025 #include "PTScriptParsing.h"
00026 
00027 #include <hugin_utils/utils.h>
00028 #include <stdio.h>
00029 
00030 
00031 namespace HuginBase {
00032 namespace PTScriptParsing {
00033 
00034 
00035 
00037 bool getPTParam(std::string & output, const std::string & line, const std::string & parameter)
00038 {
00039     int len = line.size();
00040     for (int i=1; i < len; i++) {
00041         if (line[i-1] == ' ' && line[i] != ' ') {
00042             // beginning of a parameter
00043             std::string par = line.substr(i, parameter.length());
00044             if (par == parameter) {
00045                 // found, skip to value
00046                 i = i+parameter.length();
00047                 if ( i >= len ) {
00048                     // parameter without value, not valid.
00049                     output = "";
00050                     return true;
00051                 }
00052                 if (line[i]== '"') {
00053                     i++;
00054                     if ( i >= len ) {
00055                         return false;
00056                     }
00057                     // this is a string parameter, skip to next "
00058                     size_t end = line.find('"', i);
00059                     if (end == std::string::npos) {
00060                         // unclosed string found
00061                         return false;
00062                     }
00063                     output = line.substr(i,end-i);
00064                     return true;
00065                 } else {
00066                     // ordinary parameter, skip to next space
00067                     size_t end = line.find_first_of(" \t\n", i);
00068                     output = line.substr(i, end-i);
00069                     return true;
00070                 }
00071             } else {
00072                 // this is another parameter, skip it
00073                 i++;
00074                 if ( i >= len ) {
00075                     return false;
00076                 }
00077                 if (line[i]== '"') {
00078                     i++;
00079                     // this is a string parameter, skip to next "
00080                     size_t end = line.find('"', i);
00081                     if (end == std::string::npos) {
00082                         // unclosed string found
00083                         return false;
00084                     }
00085                     i = end;
00086                     if ( i >= len ) {
00087                         return false;
00088                     }
00089                 } else {
00090                     // this is an ordinary parameter, skip to next space
00091                     size_t end = line.find_first_of(" \t\n", i);
00092                     if (end == std::string::npos) {
00093                         // not found, last parameter
00094                         return false;
00095                     }
00096                     i = end;
00097                 }
00098             }
00099         }
00100     }
00101     return false;
00102 }
00103 
00104 #if 0
00105 bool getPTParam(std::string & output, const std::string & line, const std::string & parameter)
00106 {
00107     std::string::size_type p;
00108     if ((p=line.find(std::string(" ") + parameter)) == std::string::npos) {
00109         DEBUG_INFO("could not find param " << parameter
00110                    << " in line: " << line);
00111         return false;
00112     }
00113     p += parameter.length() + 1;
00114     std::string::size_type p2 = line.find(' ',p);
00115     output = line.substr(p, p2-p);
00116     //    DEBUG_DEBUG("string idex: " << p <<"," << p2 << "  string: \"" << output << "\"");
00117     return true;
00118 }
00119 
00120 bool getPTStringParam(std::string & output, const std::string & line, const std::string & parameter)
00121 {
00122     std::string::size_type p;
00123     if ((p=line.find(std::string(" ") + parameter + "\"")) == std::string::npos) {
00124         DEBUG_INFO("could not find string param " << parameter
00125                    << " in line: " << line);
00126         return false;
00127     }
00128     p += parameter.length() + 2;
00129     std::string::size_type e = line.find("\"",p);
00130     DEBUG_DEBUG("p:" << p << " e:" << e);
00131     output = line.substr(p,e-p);
00132     DEBUG_DEBUG("output: ##" << output << "##");
00133     return true;
00134 }
00135 
00136 bool getPTStringParamColon(std::string & output, const std::string & line, const std::string & parameter)
00137 {
00138     std::string::size_type p;
00139     if ((p=line.find(std::string(" ") + parameter + ":")) == std::string::npos) {
00140         DEBUG_INFO("could not find string param " << parameter
00141                    << " in line: " << line);
00142         return false;
00143     }
00144     p += parameter.length() + 2;
00145     std::string::size_type e = line.find(" ",p);
00146     DEBUG_DEBUG("p:" << p << " e:" << e);
00147     output = line.substr(p,e-p);
00148     DEBUG_DEBUG("output: ##" << output << "##");
00149     return true;
00150 }
00151 #endif
00152 
00153 bool getDoubleParam(double & d, const std::string & line, const std::string & name)
00154 {
00155     std::string s;
00156     if (!getPTParam(s, line, name)) {
00157         return false;
00158     }
00159     return hugin_utils::stringToDouble(s, d);
00160 }
00161 
00162 bool getPTDoubleParam(double & value, int & link,
00163                           const std::string & line, const std::string & var)
00164 {
00165     std::string val;
00166     if (getPTParam(val,line, var)) {
00167         DEBUG_ASSERT(line.size() > 0);
00168         DEBUG_DEBUG(var << ":" <<val);
00169         if (val[0] == '=') {
00170             link = hugin_utils::lexical_cast<int>(val.substr(1));
00171         } else {
00172             link = -1;
00173             if (!hugin_utils::stringToDouble(val, value)) {
00174                 return false;
00175             }
00176         }
00177     } else {
00178         return false;
00179     }
00180     return true;
00181 }
00182 
00183 bool readVar(Variable & var, int & link, const std::string & line)
00184 {
00185     std::string val;
00186     if (getPTParam(val,line, var.getName())) {
00187         DEBUG_ASSERT(line.size() > 0);
00188         DEBUG_DEBUG(var.getName() << ":" <<val);
00189         if (val[0] == '=') {
00190             link = hugin_utils::lexical_cast<int>(val.substr(1));
00191         } else {
00192             link = -1;
00193             double dest = 0;
00194             if (!hugin_utils::stringToDouble(val, dest)) {
00195                 return false;
00196             }
00197             var.setValue(dest);
00198         }
00199     } else {
00200         return false;
00201     }
00202     return true;
00203 }
00204 
00205 
00206 
00207 
00208 
00209 // cannot use Lens::variableNames here, because r,p,v,j need to be included
00211 const char * ImgInfo::varnames[] = {"v", "a","b","c", "d","e", "g","t", "r","p","y","j","TrX", "TrY", "TrZ", "Tpy", "Tpp",
00212                                     "Va", "Vb", "Vc", "Vd",  "Vx", "Vy",
00213                                     "Eev", "Er",  "Eb",
00214                                     "Ra", "Rb", "Rc", "Rd", "Re",  0};
00215 
00216 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,
00217                                    1.0, 0.0, 0.0, 0.0,   0.0, 0.0, 
00218                                    0.0, 1.0,  1.0, 
00219                                    0.0, 0.0, 0.0, 0.0, 0.0};
00220 
00221     
00222 void ImgInfo:: init()
00223 {
00224     // blend_radius = 0;
00225     width = -1;
00226     height = -1;
00227     f = -2;
00228     vigcorrMode = 0;  // default to no correction
00229                       // is transformed to correction by division later
00230     responseType = 0; // default to EMOR
00231     for (const char ** v = varnames; *v != 0; v++) {
00232         vars[*v] = 0;
00233         links[*v] = -2;
00234     }
00235     autoCenterCrop = true;
00236     cropFactor = 1;
00237     enabled = true;
00238 }
00239 
00240 
00241 void ImgInfo::parse(const std::string & line)
00242 {
00243     double * val = defaultValues;
00244     for (const char ** v = varnames; *v; v++, val++) {
00245         vars[*v] = *val;
00246         links[*v] = -1;
00247         std::string name;
00248         name = *v;
00249         getPTDoubleParam(vars[*v], links[*v], line, name);
00250     }
00251     
00252     // getIntParam(blend_radius, line, "u");
00253     
00254     // read lens type and hfov
00255     getIntParam(f, line, "f");
00256     
00257     getPTParam(filename,line,"n");
00258     getIntParam(width, line, "w");
00259     getIntParam(height, line, "h");
00260     
00261     getIntParam(vigcorrMode, line, "Vm");
00262     // HACK: force Va1, for all images that use the a polynomial vig correction mode.
00263     // reset to vignetting correction by division.
00264     if (vigcorrMode != 5) {
00265         vigcorrMode = 5;
00266         vars["Va"] = 1.0;
00267         vars["Vb"] = 0.0;
00268         vars["Vc"] = 0.0;
00269         vars["Vd"] = 0.0;
00270     }
00271     
00272     getIntParam(responseType, line, "Rt");
00273     getPTParam(flatfieldname,line,"Vf");
00274     
00275     std::string crop_str;
00276     if ( getPTParam(crop_str, line, "C") ) {
00277         int left, right, top, bottom;
00278         int n = sscanf(crop_str.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
00279         if (n == 4) {
00280             crop = vigra::Rect2D(left, top, right, bottom);
00281         } else {
00282             DEBUG_WARN("Could not parse crop string: " << crop_str);
00283         }
00284     }
00285     if ( getPTParam(crop_str, line, "S") ) {
00286         int left, right, top, bottom;
00287         int n = sscanf(crop_str.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
00288         if (n == 4) {
00289             crop = vigra::Rect2D(left, top, right, bottom);
00290         } else {
00291             DEBUG_WARN("Could not parse crop string: " << crop_str);
00292         }
00293     }
00294     
00295     
00296 }
00297 
00298 
00299 
00300 }
00301 } // namespace

Generated on Fri Apr 18 01:25:40 2014 for Hugintrunk by  doxygen 1.3.9.1