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             if (!hugin_utils::stringToInt(val.substr(1), link))
00171             {
00172                 return false;
00173             };
00174         } else {
00175             link = -1;
00176             if (!hugin_utils::stringToDouble(val, value)) {
00177                 return false;
00178             }
00179         }
00180     } else {
00181         return false;
00182     }
00183     return true;
00184 }
00185 
00186 bool readVar(Variable & var, int & link, const std::string & line)
00187 {
00188     std::string val;
00189     if (getPTParam(val,line, var.getName())) {
00190         DEBUG_ASSERT(line.size() > 0);
00191         DEBUG_DEBUG(var.getName() << ":" <<val);
00192         if (val[0] == '=') {
00193             if (!hugin_utils::stringToInt(val.substr(1), link))
00194             {
00195                 return false;
00196             };
00197         } else {
00198             link = -1;
00199             double dest = 0;
00200             if (!hugin_utils::stringToDouble(val, dest)) {
00201                 return false;
00202             }
00203             var.setValue(dest);
00204         }
00205     } else {
00206         return false;
00207     }
00208     return true;
00209 }
00210 
00211 
00212 
00213 
00214 
00215 // cannot use Lens::variableNames here, because r,p,v,j need to be included
00217 const char * ImgInfo::varnames[] = {"v", "a","b","c", "d","e", "g","t", "r","p","y","j","TrX", "TrY", "TrZ", "Tpy", "Tpp",
00218                                     "Va", "Vb", "Vc", "Vd",  "Vx", "Vy",
00219                                     "Eev", "Er",  "Eb",
00220                                     "Ra", "Rb", "Rc", "Rd", "Re",  0};
00221 
00222 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,
00223                                    1.0, 0.0, 0.0, 0.0,   0.0, 0.0, 
00224                                    0.0, 1.0,  1.0, 
00225                                    0.0, 0.0, 0.0, 0.0, 0.0};
00226 
00227     
00228 void ImgInfo:: init()
00229 {
00230     // blend_radius = 0;
00231     width = -1;
00232     height = -1;
00233     f = -2;
00234     vigcorrMode = 0;  // default to no correction
00235                       // is transformed to correction by division later
00236     responseType = 0; // default to EMOR
00237     for (const char ** v = varnames; *v != 0; v++) {
00238         vars[*v] = 0;
00239         links[*v] = -2;
00240     }
00241     autoCenterCrop = true;
00242     cropFactor = 1;
00243     enabled = true;
00244 }
00245 
00246 
00247 void ImgInfo::parse(const std::string & line)
00248 {
00249     double * val = defaultValues;
00250     for (const char ** v = varnames; *v; v++, val++) {
00251         vars[*v] = *val;
00252         links[*v] = -1;
00253         std::string name;
00254         name = *v;
00255         getPTDoubleParam(vars[*v], links[*v], line, name);
00256     }
00257     
00258     // getIntParam(blend_radius, line, "u");
00259     
00260     // read lens type and hfov
00261     getIntParam(f, line, "f");
00262     
00263     getPTParam(filename,line,"n");
00264     getIntParam(width, line, "w");
00265     getIntParam(height, line, "h");
00266     
00267     getIntParam(vigcorrMode, line, "Vm");
00268     // HACK: force Va1, for all images that use the a polynomial vig correction mode.
00269     // reset to vignetting correction by division.
00270     if (vigcorrMode != 5) {
00271         vigcorrMode = 5;
00272         vars["Va"] = 1.0;
00273         vars["Vb"] = 0.0;
00274         vars["Vc"] = 0.0;
00275         vars["Vd"] = 0.0;
00276     }
00277     
00278     getIntParam(responseType, line, "Rt");
00279     getPTParam(flatfieldname,line,"Vf");
00280     
00281     std::string crop_str;
00282     if ( getPTParam(crop_str, line, "C") ) {
00283         int left, right, top, bottom;
00284         int n = sscanf(crop_str.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
00285         if (n == 4) {
00286             crop = vigra::Rect2D(left, top, right, bottom);
00287         } else {
00288             DEBUG_WARN("Could not parse crop string: " << crop_str);
00289         }
00290     }
00291     if ( getPTParam(crop_str, line, "S") ) {
00292         int left, right, top, bottom;
00293         int n = sscanf(crop_str.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
00294         if (n == 4) {
00295             crop = vigra::Rect2D(left, top, right, bottom);
00296         } else {
00297             DEBUG_WARN("Could not parse crop string: " << crop_str);
00298         }
00299     }
00300     
00301     
00302 }
00303 
00304 
00305 
00306 }
00307 } // namespace

Generated on 31 Jul 2015 for Hugintrunk by  doxygen 1.4.7