Panorama.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00026 #include "Panorama.h"
00027 
00028 #include "PTScriptParsing.h"
00029 #include "ImageVariableTranslate.h"
00030 #include "StandardImageVariableGroups.h"
00031 #include <panotools/PanoToolsInterface.h>
00032 #include <algorithms/basic/CalculateOverlap.h>
00033 #include <algorithms/basic/LayerStacks.h>
00034 #include <panodata/OptimizerSwitches.h>
00035 
00036 #include <fstream>
00037 #include <typeinfo>
00038 
00039 namespace HuginBase {
00040 
00041 Panorama::Panorama() : dirty(false), m_forceImagesUpdate(false)
00042 {
00043     // init map with ptoptimizer variables.
00044     m_ptoptimizerVarNames.insert("a");
00045     m_ptoptimizerVarNames.insert("b");
00046     m_ptoptimizerVarNames.insert("c");
00047     m_ptoptimizerVarNames.insert("d");
00048     m_ptoptimizerVarNames.insert("e");
00049     m_ptoptimizerVarNames.insert("g");
00050     m_ptoptimizerVarNames.insert("t");
00051     m_ptoptimizerVarNames.insert("v");
00052     m_ptoptimizerVarNames.insert("r");
00053     m_ptoptimizerVarNames.insert("p");
00054     m_ptoptimizerVarNames.insert("y");
00055     m_ptoptimizerVarNames.insert("TrX");
00056     m_ptoptimizerVarNames.insert("TrY");
00057     m_ptoptimizerVarNames.insert("TrZ");
00058     m_ptoptimizerVarNames.insert("Tpy");
00059     m_ptoptimizerVarNames.insert("Tpp");
00060 }
00061 
00062 Panorama::~Panorama()
00063 {
00064     DEBUG_TRACE("dtor");
00065     reset();
00066     DEBUG_TRACE("dtor about to finish");
00067 }
00068 
00069 void Panorama::reset()
00070 {
00071     // delete all images and control points.
00072     state.ctrlPoints.clear();
00073     state.deleteAllImages();
00074     state.options.reset();
00075     state.optvec.clear();
00076     state.optSwitch=0;
00077     state.optPhotoSwitch=0;
00078     state.needsOptimization = false;
00079     AppBase::DocumentData::setDirty(false);
00080     dirty=false;
00081 }
00082 
00083 std::vector<unsigned int> Panorama::getCtrlPointsForImage(unsigned int imgNr) const
00084 {
00085     std::vector<unsigned int> result;
00086     unsigned int i = 0;
00087     for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
00088         if ((it->image1Nr == imgNr) || (it->image2Nr == imgNr)) {
00089             result.push_back(i);
00090         }
00091         i++;
00092     }
00093     return result;
00094 }
00095 
00096 CPointVector Panorama::getCtrlPointsVectorForImage(unsigned int imgNr) const
00097 {
00098     CPointVector result;
00099     for(unsigned int i=0;i<state.ctrlPoints.size();i++)
00100     {
00101         ControlPoint point=state.ctrlPoints[i];
00102         if(point.image1Nr==imgNr)
00103         {
00104             result.push_back(std::make_pair(i,point));
00105         }
00106         else
00107         {
00108             if(point.image2Nr==imgNr)
00109             {
00110                 point.mirror();
00111                 result.push_back(std::make_pair(i,point));
00112             };
00113         };
00114     };
00115     return result;
00116 };
00117 
00118 VariableMapVector Panorama::getVariables() const
00119 {
00120     VariableMapVector map;
00121     for (size_t i = 0; i < state.images.size(); i++)
00122     {
00123         map.push_back(state.images[i]->getVariableMap());
00124     }
00125     return map;
00126 }
00127 
00128 const VariableMap Panorama::getImageVariables(unsigned int imgNr) const
00129 {
00130     assert(imgNr < state.images.size());
00131     return state.images[imgNr]->getVariableMap();
00132 }
00133 
00134 
00135 void Panorama::updateCtrlPointErrors(const UIntSet & imgs, const CPVector & cps)
00136 {
00137     unsigned sc = 0;
00138     unsigned ic = 0;
00139     std::map<unsigned int, unsigned int> script2CPMap;
00140     for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
00141         if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
00142             script2CPMap[sc] = ic;
00143             sc++;
00144         }
00145         ic++;
00146     }
00147 
00148     // need to have same number of control points!
00149     assert(cps.size() == script2CPMap.size());
00150     unsigned i=0;
00151     for (CPVector::const_iterator it = cps.begin(); it != cps.end(); ++it) {
00152         imageChanged(script2CPMap[it->image1Nr]);
00153         imageChanged(script2CPMap[it->image2Nr]);
00154         state.ctrlPoints[script2CPMap[i]].error = it->error;
00155         i++;
00156     }
00157 }
00158 
00159 
00160 void Panorama::updateCtrlPointErrors(const CPVector & cps)
00161 {
00162     assert(cps.size() == state.ctrlPoints.size());
00163     unsigned int nrp = cps.size();
00164     for (unsigned int i = 0; i < nrp ; i++) {
00165         imageChanged(state.ctrlPoints[i].image1Nr);
00166         imageChanged(state.ctrlPoints[i].image2Nr);
00167         state.ctrlPoints[i].error = cps[i].error;
00168     }
00169 }
00170 
00171 void Panorama::updateVariables(const VariableMapVector & vars)
00172 {
00173     assert(vars.size() == state.images.size());
00174     unsigned int i = 0;
00175     for (VariableMapVector::const_iterator it = vars.begin(); it != vars.end(); ++it) {
00176         updateVariables(i, *it);
00177         i++;
00178     }
00179 }
00180 
00181 void Panorama::updateVariables(const UIntSet & imgs, const VariableMapVector & vars)
00182 {
00183     VariableMapVector::const_iterator v_it = vars.begin();
00184     for (UIntSet::const_iterator it = imgs.begin(); it != imgs.end(); ++it) {
00185         assert(*it < state.images.size());
00186         updateVariables(*it, *v_it);
00187         ++v_it;
00188     }
00189 }
00190 
00191 void Panorama::updateVariables(unsigned int imgNr, const VariableMap & var)
00192 {
00193     if (imgNr > state.images.size())
00194         return;
00195     for (VariableMap::const_iterator it = var.begin(); it != var.end() ; ++it) {
00196         updateVariable(imgNr,it->second);
00197     }
00198 }
00199 
00200 void Panorama::updateVariable(unsigned int imgNr, const Variable &var)
00201 {
00202     if (imgNr > state.images.size())
00203         return;
00204     // update a single variable, and everything linked to it.
00205     state.images[imgNr]->setVar(var.getName(), var.getValue());
00206     // call imageChanged for the images affected by this.
00207 #define image_variable( name, type, default_value ) \
00208     if (PTOVariableConverterFor##name::checkApplicability(var.getName())) \
00209         {\
00210             for (std::size_t i = 0; i < getNrOfImages(); i++)\
00211             {\
00212                 if (state.images[imgNr]->name##isLinkedWith(*state.images[i]))\
00213                 {\
00214                     imageChanged(i);\
00215                 }\
00216             }\
00217         }\
00218     else 
00219 #include "image_variables.h"
00220 #undef image_variable
00221     {// this is for the final else.
00222         DEBUG_ERROR("Unknown variable " << var.getName());
00223     }
00224     state.needsOptimization = true;
00225 }
00226 
00227 void Panorama::UpdateFocalLength(UIntSet imgs, double newFocalLength)
00228 {
00229     for(UIntSet::const_iterator it=imgs.begin();it!=imgs.end();++it)
00230     {
00231         state.images[*it]->updateFocalLength(newFocalLength);
00232         imageChanged(*it);
00233     };
00234     //search for images with linked HFOV and mark these also as changed
00235     for(UIntSet::const_iterator it=imgs.begin();it!=imgs.end();++it)
00236     {
00237         SrcPanoImage *img=state.images[*it];
00238         if(state.images[*it]->HFOVisLinked())
00239         {
00240             for(unsigned int j=0;j<getNrOfImages();j++)
00241             {
00242                 if(*it!=j)
00243                 {
00244                     if(state.images[*it]->HFOVisLinkedWith(*img))
00245                     {
00246                         imageChanged(j);
00247                     };
00248                 };
00249             };
00250         };
00251     };
00252 };
00253 
00254 void Panorama::UpdateCropFactor(UIntSet imgs, double newCropFactor)
00255 {
00256     //store list of focal length, so we can keep the focal length unchanged and change hfov instead
00257     std::vector<double> focalLengthVector(getNrOfImages(),0.0);
00258     for(unsigned i=0;i<getNrOfImages();i++)
00259     {
00260         focalLengthVector[i]=state.images[i]->calcFocalLength(state.images[i]->getProjection(),
00261             state.images[i]->getHFOV(),state.images[i]->getCropFactor(),state.images[i]->getSize());
00262     };
00263     for(UIntSet::const_iterator it=imgs.begin();it!=imgs.end();++it)
00264     {
00265         state.images[*it]->updateCropFactor(focalLengthVector[*it],newCropFactor);
00266         imageChanged(*it);
00267     };
00268 };
00269 
00270 // What counts as changed in terms of the image variable links?
00271 // Should I call imageChanged on every image linked to sourceImg and destImg?
00272 // destImg's variable will change value to sourceImg's, so the images linked to
00273 // destImg should be linked.
00275 #define image_variable( name, type, default_value )\
00276 void Panorama::linkImageVariable##name(unsigned int sourceImgNr, unsigned int destImgNr)\
00277 {\
00278     state.images[destImgNr]->link##name(state.images[sourceImgNr]);\
00279     imageChanged(destImgNr);\
00280     imageChanged(sourceImgNr);\
00281     state.needsOptimization = true;\
00282 }
00283 #include "image_variables.h"
00284 #undef image_variable
00285 
00287 #define image_variable( name, type, default_value )\
00288 void Panorama::unlinkImageVariable##name(unsigned int imgNr)\
00289 {\
00290     state.images[imgNr]->unlink##name();\
00291     imageChanged(imgNr);\
00292     state.needsOptimization = true;\
00293 }
00294 #include "image_variables.h"
00295 #undef image_variable
00296 
00297 void Panorama::setOptimizeVector(const OptimizeVector & optvec)
00298 {
00299     DEBUG_ASSERT(optvec.size() == state.images.size());
00300     state.optvec = optvec;
00301 }
00302 
00303 void Panorama::setOptimizerSwitch(const int newSwitch)
00304 {
00305     if(state.optSwitch!=newSwitch)
00306     {
00307         state.optSwitch=newSwitch;
00308     };
00309 };
00310 
00311 void Panorama::setPhotometricOptimizerSwitch(const int newSwitch)
00312 {
00313     if(state.optPhotoSwitch!=newSwitch)
00314     {
00315         state.optPhotoSwitch=newSwitch;
00316     };
00317 };
00318 
00319 unsigned int Panorama::addImage(const SrcPanoImage &img)
00320 {
00321     unsigned int nr = state.images.size();
00322     state.images.push_back(new SrcPanoImage(img));
00323     // create empty optimisation vector
00324     state.optvec.push_back(std::set<std::string>());
00325     imageChanged(nr);
00326     return nr;
00327 }
00328 
00329 void Panorama::removeImage(unsigned int imgNr)
00330 {
00331     DEBUG_DEBUG("Panorama::removeImage(" << imgNr << ")");
00332     assert(imgNr < state.images.size());
00333 
00334     // remove control points
00335     CPVector::iterator it = state.ctrlPoints.begin();
00336     while (it != state.ctrlPoints.end()) {
00337         if ((it->image1Nr == imgNr) || (it->image2Nr == imgNr)) {
00338             // remove point that refernce to imgNr
00339             it = state.ctrlPoints.erase(it);
00340         } else {
00341             // correct point references
00342             if (it->image1Nr > imgNr) it->image1Nr--;
00343             if (it->image2Nr > imgNr) it->image2Nr--;
00344             ++it;
00345         }
00346     }
00347 
00348     DEBUG_TRACE("Remove variables and image from panorama state")
00349     delete state.images[imgNr];
00350     state.images.erase(state.images.begin() + imgNr);
00351     state.optvec.erase(state.optvec.begin() + imgNr);
00352 
00353         // check if reference image has been moved
00354         if (state.options.optimizeReferenceImage >= state.images.size()) {
00355                 state.options.optimizeReferenceImage = 0;
00356         imageChanged(state.options.optimizeReferenceImage);
00357         }
00358 
00359         if (state.options.colorReferenceImage >= state.images.size()) {
00360                 state.options.colorReferenceImage = 0;
00361         imageChanged(state.options.colorReferenceImage);
00362         }
00363 
00364     // change all other (moved) images
00365     DEBUG_TRACE("flag moved images as dirty");
00366     for (unsigned int i=imgNr; i < state.images.size(); i++) {
00367         imageChanged(i);
00368     }
00369     m_forceImagesUpdate = true;
00370 }
00371 
00372 
00373 void Panorama::setImageFilename(unsigned int i, const std::string & fname)
00374 {
00375     DEBUG_ASSERT(i < state.images.size());
00376     state.images[i]->setFilename(fname);
00377     imageChanged(i);
00378     m_forceImagesUpdate = true;
00379 }
00380 
00381 unsigned int Panorama::addCtrlPoint(const ControlPoint & point )
00382 {
00383     unsigned int nr = state.ctrlPoints.size();
00384     state.ctrlPoints.push_back(point);
00385     imageChanged(point.image1Nr);
00386     imageChanged(point.image2Nr);
00387     state.needsOptimization = true;
00388     return nr;
00389 }
00390 
00391 void Panorama::removeCtrlPoint(unsigned int pNr)
00392 {
00393     DEBUG_ASSERT(pNr < state.ctrlPoints.size());
00394     ControlPoint & point = state.ctrlPoints[pNr];
00395     unsigned int i1 = point.image1Nr;
00396     unsigned int i2 = point.image2Nr;
00397     state.ctrlPoints.erase(state.ctrlPoints.begin() + pNr);
00398 
00399     // update line control points
00400     updateLineCtrlPoints();
00401     imageChanged(i1);
00402     imageChanged(i2);
00403     state.needsOptimization = true;
00404 }
00405 
00406 void Panorama::removeDuplicateCtrlPoints()
00407 {
00408     std::set<std::string> listOfCPs;
00409     std::set<unsigned int> duplicateCPs;
00410     for(unsigned int i=0; i<state.ctrlPoints.size();i++)
00411     {
00412         std::string s=state.ctrlPoints[i].getCPString();
00413         std::pair<std::set<std::string>::iterator,bool> it=listOfCPs.insert(s);
00414         if(it.second==false)
00415         {
00416             duplicateCPs.insert(i);
00417         };
00418     }
00419     //now remove duplicate control points, mark affected images as changed
00420     if(!duplicateCPs.empty())
00421     {
00422         for(std::set<unsigned int>::reverse_iterator it=duplicateCPs.rbegin();it!=duplicateCPs.rend();++it)
00423         {
00424             ControlPoint cp=state.ctrlPoints[*it];
00425             imageChanged(cp.image1Nr);
00426             imageChanged(cp.image2Nr);
00427             removeCtrlPoint(*it);
00428         };
00429     };
00430     updateLineCtrlPoints();
00431 }
00432 
00433 
00434 void Panorama::changeControlPoint(unsigned int pNr, const ControlPoint & point)
00435 {
00436     assert(pNr < state.ctrlPoints.size());
00437 
00438     // change notify for all involved images
00439     imageChanged(state.ctrlPoints[pNr].image1Nr);
00440     imageChanged(state.ctrlPoints[pNr].image2Nr);
00441     imageChanged(point.image1Nr);
00442     imageChanged(point.image2Nr);
00443     state.needsOptimization = true;
00444 
00445     state.ctrlPoints[pNr] = point;
00446     updateLineCtrlPoints();
00447 }
00448 
00449 void Panorama::setCtrlPoints(const CPVector & points)
00450 {
00451     for (CPVector::const_iterator it = state.ctrlPoints.begin();
00452          it != state.ctrlPoints.end(); ++it)
00453     {
00454         imageChanged(it->image1Nr);
00455         imageChanged(it->image2Nr);
00456     }
00457 
00458     state.ctrlPoints = points;
00459 
00460     for (CPVector::const_iterator it = state.ctrlPoints.begin();
00461          it != state.ctrlPoints.end(); ++it)
00462     {
00463         imageChanged(it->image1Nr);
00464         imageChanged(it->image2Nr);
00465     }
00466     state.needsOptimization = true;
00467     updateLineCtrlPoints();
00468 }
00469 
00470 // close holes in line control points
00471 void Panorama::updateLineCtrlPoints()
00472 {
00473     // sort all line control points
00474     std::map<int, int> lines;
00475     for (CPVector::const_iterator it = state.ctrlPoints.begin();
00476          it != state.ctrlPoints.end(); ++it)
00477     {
00478         if (it->mode > 2)
00479             lines[it->mode] = 0;
00480     }
00481     int i=3;
00482     for (std::map<int,int >::iterator it = lines.begin(); it != lines.end(); ++it) 
00483     {
00484         (*it).second = i;
00485         i++;
00486     }
00487 
00488     for (CPVector::iterator it = state.ctrlPoints.begin();
00489          it != state.ctrlPoints.end(); ++it)
00490     {
00491         if (it->mode > 2) {
00492             int newmode = lines[it->mode];
00493             if (it->mode != newmode) {
00494                 it->mode = newmode;
00495                 imageChanged(it->image1Nr);
00496                 imageChanged(it->image2Nr);
00497             }
00498         }
00499     }
00500 }
00501 
00502 
00503 void Panorama::printPanoramaScript(std::ostream & o,
00504                                    const OptimizeVector & optvars,
00505                                    const PanoramaOptions & output,
00506                                    const UIntSet & imgs,
00507                                    bool forPTOptimizer,
00508                                    const std::string & stripPrefix) const
00509 {
00510     // set numeric locale to C, for correct number output
00511     char * old_locale = strdup(setlocale(LC_NUMERIC, NULL));
00512     setlocale(LC_NUMERIC,"C");
00513 
00514     if (forPTOptimizer) {
00515         o << "# PTOptimizer script, written by hugin" << std::endl
00516           << std::endl;
00517     } else {
00518         o << "# hugin project file" << std::endl;
00519         o << "#hugin_ptoversion 2" << std::endl;
00520     }
00521     // output options..
00522 
00523     output.printScriptLine(o, forPTOptimizer);
00524 
00525     // map from script img nr -> pano image nr
00526     std::map<unsigned int, unsigned int> imageNrMap;
00527     o << std::endl
00528       << "# image lines" << std::endl;
00529     
00530     // somewhere to store the v lines, which give the variables to be optimised.
00531     std::stringstream vlines;
00532     std::stringstream masklines;
00533     unsigned int ic = 0;
00534     for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
00535          ++imgNrIt)
00536     {
00537         unsigned int imgNr = *imgNrIt;
00538         imageNrMap[imgNr] = ic;
00539         const SrcPanoImage & img = *state.images[imgNr];
00540         VariableMap vars;
00541 
00542         // print special comment line with hugin GUI data
00543         o << "#-hugin ";
00544         if (img.getCropMode() != BaseSrcPanoImage::NO_CROP) {
00545             if (img.getAutoCenterCrop())
00546                 o << " autoCenterCrop=1";
00547         }
00548         o << " cropFactor=" << img.getCropFactor() ;
00549         if (! img.getActive()) {
00550             o << " disabled";
00551         }
00552         o << std::endl;
00553 
00554         o << "i w" << img.getSize().width() << " h" << img.getSize().height()
00555           <<" f" << img.getProjection() << " ";
00556 
00557         // print variables with links
00558 /* Individually do all the variables specified by each SrcPanoImg variable.
00559  * Clear the list after each SrcPanoImg variable.
00560  * If there is any links to previous images, write that in the script.
00561  * If there are no links to previous images, write the value instead.
00562  * Each variable in SrcPanoImg may produce any number of variables in the map,
00563  *      but the linking properties are shared.
00564  * Additionally, when we are writing variables by value which are set to be
00565  * optimised, we should remember them so we can write them later as 'v' lines.
00566  */
00567 #define image_variable( name, type, default_value )\
00568         PTOVariableConverterFor##name::addToVariableMap(state.images[imgNr]->get##name##IV(), vars);\
00569         if (!vars.empty())\
00570         {\
00571             bool linking = false;\
00572             std::size_t link_target;\
00573             if (ic!=0)\
00574             {\
00575                 if (state.images[imgNr]->name##isLinked())\
00576                 {\
00577                     for (link_target = 0; link_target < imgNr; link_target++)\
00578                     {\
00579                         if (set_contains(imgs,link_target) && state.images[imgNr]->name##isLinkedWith(*state.images[link_target]))\
00580                         {\
00581                             linking = true;\
00582                             break;\
00583                         }\
00584                     }\
00585                 }\
00586             }\
00587             for (VariableMap::const_iterator vit = vars.begin();\
00588              vit != vars.end(); ++vit)\
00589             {\
00590                 if (forPTOptimizer && !set_contains(m_ptoptimizerVarNames,vit->first))\
00591                     continue;\
00592                 else if (linking)\
00593                 {\
00594                     o << vit->first << "=" << imageNrMap[link_target] << " ";\
00595                 } else {\
00596                     if (set_contains(optvars[imgNr], vit->first))\
00597                     {\
00598                         vlines << "v " << vit->first << imageNrMap[imgNr] << std::endl;\
00599                     }\
00600                     if (((vit->first == "a" && set_contains(optvars[imgNr], "a") )|| \
00601                                 (vit->first == "b" && set_contains(optvars[imgNr], "b") )|| \
00602                                 (vit->first == "c" && set_contains(optvars[imgNr], "c") )|| \
00603                                 (vit->first == "TrX" && set_contains(optvars[imgNr], "TrX") )|| \
00604                                 (vit->first == "TrY" && set_contains(optvars[imgNr], "TrY") )|| \
00605                                 (vit->first == "TrZ" && set_contains(optvars[imgNr], "TrZ") )|| \
00606                                 (vit->first == "Tpy" && set_contains(optvars[imgNr], "Tpy") )|| \
00607                                 (vit->first == "Tpp" && set_contains(optvars[imgNr], "Tpp") )\
00608                                )\
00609                                && forPTOptimizer && vit->second.getValue() == 0.0) \
00610                     {\
00611                         o << vit->first << 1e-5 << " ";\
00612                     } else if (( (vit->first == "r" && set_contains(optvars[imgNr], "r") ) || \
00613                                  (vit->first == "p" && set_contains(optvars[imgNr], "p") ) || \
00614                                  (vit->first == "y" && set_contains(optvars[imgNr], "y") ) \
00615                                )\
00616                                && forPTOptimizer && fabs(vit->second.getValue()) < 1e-13)\
00617                     {\
00618                         o << vit->first << 0 << " ";\
00619                     } else {\
00620                         vit->second.print(o) << " ";\
00621                     }\
00622                 }\
00623             }\
00624         }\
00625         vars.clear();
00626 #include "image_variables.h"
00627 #undef image_variable
00628 
00629         if (img.getCropMode()!=SrcPanoImage::NO_CROP) {
00630             // print crop parameters
00631             vigra::Rect2D c = img.getCropRect();
00632             o << " S" << c.left() << "," << c.right() << "," << c.top() << "," << c.bottom();
00633         }
00634 
00635         if (!forPTOptimizer) {
00636 
00637             if (img.getVigCorrMode() != SrcPanoImage::VIGCORR_NONE) {
00638                 o << " Vm" << img.getVigCorrMode();
00639             }
00640 
00641             if (img.getFlatfieldFilename().size() > 0) {
00642                 o << " Vf\"" << img.getFlatfieldFilename() << "\"";
00643             }
00644             if (img.getResponseType() > 0) {
00645                 o << " Rt" << img.getResponseType();
00646             }
00647 
00648             if(img.hasMasks())
00649                 img.printMaskLines(masklines,ic);
00650         }
00651 
00652 #if 0
00653 //panotools paramters, currently not used
00654         o << " u" << output.featherWidth
00655           << (img.getMorph() ? " o" : "");
00656 #endif
00657         std::string fname = img.getFilename();
00658         if (stripPrefix.size() > 0) {
00659             // strip prefix from image names.
00660             // check if the prefix is acutally the same
00661             std::string tmp = fname.substr(0,stripPrefix.size());
00662             if (tmp.compare(stripPrefix) == 0) {
00663                 DEBUG_DEBUG("striping " << stripPrefix << " from " << fname);
00664                 fname = fname.erase(0,stripPrefix.size());
00665                 DEBUG_DEBUG("after stripping: " <<  fname);
00666             } else {
00667                 DEBUG_DEBUG(stripPrefix << " does not match " << fname);
00668             }
00669         }
00670         o << " n\"" << fname << "\"" << std::endl;
00671         ic++;
00672     }
00673 
00674     o << std::endl << std::endl
00675       << "# specify variables that should be optimized" << std::endl
00676       << vlines.str()
00677       << "v" << std::endl; // empty v line to work around libpano13 bug.
00678     
00679     o << std::endl << std::endl
00680       << "# control points" << std::endl;
00681     for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
00682                 if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
00683                 o << "c n" << imageNrMap[it->image1Nr]
00684                       << " N" << imageNrMap[it->image2Nr]
00685                           << " x" << it->x1 << " y" << it->y1
00686                   << " X" << it->x2 << " Y" << it->y2
00687               << " t" << it->mode << std::endl;
00688         }
00689     }
00690     o << std::endl;
00691 
00692     if(masklines.str().length()>0)
00693         o << "# masks" << std::endl
00694             << masklines.str()
00695             << std::endl;
00696 
00697     // special line with hugins options.
00698     o << "#hugin_optimizeReferenceImage " << output.optimizeReferenceImage << std::endl;
00699     o << "#hugin_blender ";
00700     switch (output.blendMode) {
00701         case PanoramaOptions::NO_BLEND:
00702             o << "none" << endl;
00703             break;
00704         case PanoramaOptions::PTBLENDER_BLEND:
00705             o << "PTblender" << endl;
00706             break;
00707         case PanoramaOptions::SMARTBLEND_BLEND:
00708             o << "smartblend" << endl;
00709             break;
00710         case PanoramaOptions::PTMASKER_BLEND:
00711             o << "PTmasker" << endl;
00712             break;
00713         case PanoramaOptions::INTERNAL_BLEND:
00714             o << "internal" << endl;
00715             break;
00716         default:
00717         case PanoramaOptions::ENBLEND_BLEND:
00718             o << "enblend" << endl;
00719             break;
00720     }
00721     
00722     o << "#hugin_remapper ";
00723     switch (output.remapper) {
00724         case PanoramaOptions::PTMENDER:
00725             o << "PTmender" << endl;
00726             break;
00727         default:
00728         case PanoramaOptions::NONA:
00729             o << "nona" << endl;
00730             break;
00731     }
00732     
00733     o << "#hugin_enblendOptions " << output.enblendOptions << endl;
00734     o << "#hugin_enfuseOptions " << output.enfuseOptions << endl;
00735     o << "#hugin_hdrmergeOptions " << output.hdrmergeOptions << endl;
00736     o << "#hugin_verdandiOptions " << output.verdandiOptions << endl;
00737 
00738     o << "#hugin_outputLDRBlended " << (output.outputLDRBlended ? "true" : "false") << endl;
00739     o << "#hugin_outputLDRLayers " << (output.outputLDRLayers ? "true" : "false") << endl;
00740     o << "#hugin_outputLDRExposureRemapped " << (output.outputLDRExposureRemapped ? "true" : "false") << endl;
00741     o << "#hugin_outputLDRExposureLayers " << (output.outputLDRExposureLayers ? "true" : "false") << endl;
00742     o << "#hugin_outputLDRExposureBlended " << (output.outputLDRExposureBlended ? "true" : "false") << endl;
00743     o << "#hugin_outputLDRStacks " << (output.outputLDRStacks ? "true" : "false") << endl;
00744     o << "#hugin_outputLDRExposureLayersFused " << (output.outputLDRExposureLayersFused ? "true" : "false") << endl;
00745     o << "#hugin_outputHDRBlended " << (output.outputHDRBlended ? "true" : "false") << endl;
00746     o << "#hugin_outputHDRLayers " << (output.outputHDRLayers ? "true" : "false") << endl;
00747     o << "#hugin_outputHDRStacks " << (output.outputHDRStacks ? "true" : "false") << endl;    
00748 
00749     o << "#hugin_outputLayersCompression " << output.outputLayersCompression << endl;
00750     o << "#hugin_outputImageType " << output.outputImageType << endl;
00751     o << "#hugin_outputImageTypeCompression " << output.outputImageTypeCompression << endl;
00752     o << "#hugin_outputJPEGQuality " << output.quality << endl;
00753     o << "#hugin_outputImageTypeHDR " << output.outputImageTypeHDR << endl;
00754     o << "#hugin_outputImageTypeHDRCompression " << output.outputImageTypeHDRCompression << endl;
00755 
00756     o << "#hugin_outputStacksMinOverlap " << std::setprecision(3) << output.outputStacksMinOverlap << endl;
00757     o << "#hugin_outputLayersExposureDiff " << std::setprecision(2) << output.outputLayersExposureDiff << endl;
00758 
00759     if(optvars==getOptimizeVector())
00760     {
00761         o << "#hugin_optimizerMasterSwitch " << getOptimizerSwitch() << endl;
00762         o << "#hugin_optimizerPhotoMasterSwitch " << getPhotometricOptimizerSwitch() << endl;
00763     }
00764     else
00765     {
00766         o << "#hugin_optimizerMasterSwitch 0" << endl;
00767         o << "#hugin_optimizerPhotoMasterSwitch 0" << endl;
00768     };
00769 
00770     // reset locale
00771     setlocale(LC_NUMERIC,old_locale);
00772     free(old_locale);
00773 }
00774 
00775 
00776 void Panorama::printStitcherScript(std::ostream & o,
00777                                    const PanoramaOptions & target,
00778                                    const UIntSet & imgs) const
00779 {
00780     // set numeric locale to C, for correct number output
00781     char * old_locale = strdup(setlocale(LC_NUMERIC, NULL));
00782     setlocale(LC_NUMERIC,"C");
00783 
00784     o << "# PTStitcher script, written by hugin" << std::endl
00785       << std::endl;
00786     // output options..
00787     target.printScriptLine(o, true);
00788     o << std::endl
00789       << "# output image lines" << std::endl;
00790     for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end(); ++imgNrIt) {
00791         unsigned int imgNr = *imgNrIt;
00792         const SrcPanoImage & img = *state.images[imgNr];
00793 // DGSW FIXME - Unreferenced
00794 //              const Lens & lens = state.lenses[lensNr];
00795         const VariableMap & vars = state.images[imgNr]->getVariableMap();
00796 
00797         o << "o w" << img.getSize().width() << " h" << img.getSize().height()
00798           <<" f" << img.getProjection() << " ";
00799         // print variables, without links
00800         VariableMap::const_iterator vit;
00801         for(vit = vars.begin(); vit != vars.end();  ++vit)
00802         {
00803             if (!set_contains(m_ptoptimizerVarNames,vit->first)) {
00804                 continue;
00805             }
00806             vit->second.print(o) << " ";
00807         }
00808 #if 0
00809 // panotools parameters, currently not used 
00810         o << " u" << img.getFeatureWidth() 
00811           << (img.getMorph() ? " o" : "");
00812 #endif
00813         o << " n\"" << img.getFilename() << "\"";
00814         if (img.getCropMode()!=SrcPanoImage::NO_CROP) {
00815             // print crop parameters
00816             vigra::Rect2D c = img.getCropRect();
00817             o << " S" << c.left() << "," << c.right() << "," << c.top() << "," << c.bottom();
00818         }
00819         o << std::endl;
00820     }
00821     o << std::endl;
00822 
00823     // reset locale
00824     setlocale(LC_NUMERIC,old_locale);
00825     free(old_locale);
00826 
00827 }
00828 
00829 void Panorama::parseOptimizerScript(std::istream & i, const UIntSet & imgs,
00830                                     VariableMapVector & imgVars, CPVector & CPs) const
00831 {
00832     DEBUG_TRACE("");
00833     // set numeric locale to C, for correct number output
00834     char * old_locale = strdup(setlocale(LC_NUMERIC, ""));
00835     setlocale(LC_NUMERIC,"C");
00836 
00837     unsigned int ic=0;
00838     std::map<unsigned int, unsigned int> script2ImgMap;
00839     for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
00840          ++imgNrIt)
00841     {
00842         unsigned int imgNr = *imgNrIt;
00843         script2ImgMap[ic] = imgNr;
00844         ic++;
00845     }
00846     ic = 0;
00847     unsigned int sc = 0;
00848     std::map<unsigned int, unsigned int> script2CPMap;
00849     for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
00850         if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
00851             script2CPMap[sc] = ic;
00852             sc++;
00853         }
00854         ic++;
00855     }
00856 
00857 
00858 
00859     // 0 = read output (image lines), 1 = read control point distances
00860     int state = 0;
00861     std::string line;
00862     unsigned int lineNr = 0;
00863     unsigned int scriptImgCounter = 0;
00864     unsigned int scriptCPCounter = 0;
00865 //    VariableMapVector::iterator varIt = imgVars.begin();
00866 //    CPVector::iterator pointIt = CPs.begin();
00867 
00868 // DGSW FIXME - Unreferenced
00869 //      int pnr=0;
00870 
00871     while (!i.eof()) {
00872         std::getline(i, line);
00873         lineNr++;
00874         switch (state) {
00875         case 0:
00876         {
00877             // we are reading the output lines:
00878             // o f3 r0 p0 y0 v89.2582 a-0.027803 b0.059851 c-0.073115 d10.542470 e16.121145 u10 -buf
00879             if ((line.compare("# Control Points: Distance between desired and fitted Position") == 0 )
00880              || (line.compare("# Control Points: Distance between desired and fitted Position (in Pixels)") == 0 )
00881              || (line.compare("# Control Points: Distance between desired and fitted Position (in \"Pixels\")") == 0 )) {
00882                 
00883                 // switch to reading the control point distance
00884                 if (scriptImgCounter != imgs.size()) {
00885                     DEBUG_ERROR("Read only " << scriptImgCounter << " images from PTOptimizer file");
00886                 }
00887                 DEBUG_DEBUG("Changing state to read control point distances");
00888                 state = 1;
00889                 break;
00890             }
00891             if (line[0] != 'o') continue;
00892             // select variables of the image
00893             VariableMap & var = imgVars[script2ImgMap[scriptImgCounter]];
00894             DEBUG_DEBUG("reading image variables for image:" << scriptImgCounter);
00895             // read position variables
00896             int link;
00897             PTScriptParsing::readVar(map_get(var, "r"), link, line);
00898             DEBUG_ASSERT(link == -1);
00899             PTScriptParsing::readVar(map_get(var, "p"), link, line);
00900             DEBUG_ASSERT(link == -1);
00901             PTScriptParsing::readVar(map_get(var, "y"), link, line);
00902             DEBUG_ASSERT(link == -1);
00903 
00904             DEBUG_DEBUG("yaw: " << map_get(var, "y").getValue()
00905                         << " pitch " << map_get(var, "p").getValue()
00906                         << " roll " << map_get(var, "r").getValue());
00907             // read lens variables
00908 
00909             PTScriptParsing::readVar(map_get(var, "TrX"), link, line);
00910             DEBUG_ASSERT(link == -1);
00911             PTScriptParsing::readVar(map_get(var, "TrY"), link, line);
00912             DEBUG_ASSERT(link == -1);
00913             PTScriptParsing::readVar(map_get(var, "TrZ"), link, line);
00914             DEBUG_ASSERT(link == -1);
00915             PTScriptParsing::readVar(map_get(var, "Tpy"), link, line);
00916             DEBUG_ASSERT(link == -1);
00917             PTScriptParsing::readVar(map_get(var, "Tpp"), link, line);
00918             DEBUG_ASSERT(link == -1);
00919 
00920             DEBUG_DEBUG("X: " << map_get(var, "TrX").getValue()
00921                         << " Y " << map_get(var, "TrY").getValue()
00922                         << " Z " << map_get(var, "TrZ").getValue());
00923             // read lens variables
00924 
00925 
00926             for (const char **c = Lens::variableNames; *c != 0; ++c) {
00927                 Variable & curVar = map_get(var, *c);
00928                 if (!PTScriptParsing::readVar(curVar, link, line)) {
00929                     DEBUG_ERROR("Could not read "<< *c << " at script line " << lineNr);
00930                 }
00931                 // linking in output forbidden
00932                 DEBUG_ASSERT(link == -1);
00933             }
00934             scriptImgCounter++;
00935             break;
00936         }
00937         case 1:
00938         {
00939             // read ctrl point distances:
00940             // # Control Point No 0:  0.428994
00941             if (line[0] == 'C') {
00942 //                DEBUG_DEBUG(CPs.size() << " points, read: " << pnr);
00943                 state = 2;
00944                 break;
00945             }
00946             if (line.find("# Control Point No") != 0) continue;
00947             DEBUG_DEBUG("reading cp dist line: " << line);
00948             std::string::size_type p;
00949             if ((p=line.find(':')) == std::string::npos) assert(0);
00950             p++;
00951             DEBUG_DEBUG("parsing point " << scriptCPCounter << " (idx:" << p << "): " << line.substr(p));
00952             double err = -1;
00953 
00954             hugin_utils::stringToDouble(line.substr(p), err);
00955             CPs[script2CPMap[scriptCPCounter]].error = err;
00956             DEBUG_DEBUG("read CP distance " << err);
00957             scriptCPCounter++;
00958             break;
00959         }
00960         default:
00961             // ignore line..
00962             break;
00963         }
00964     }
00965 
00966     // reset locale
00967     setlocale(LC_NUMERIC,old_locale);
00968     free(old_locale);
00969 }
00970 
00971 void Panorama::changeFinished(bool keepDirty)
00972 {
00973     if (state.images.size() == 0) {
00974         // force an empty update if all images have been
00975         // removed
00976         DEBUG_DEBUG("forcing images update, with no images");
00977         m_forceImagesUpdate = true;
00978     }
00979     // remove change notification for nonexisting images from set.
00980     UIntSet::iterator uB = changedImages.lower_bound(state.images.size());
00981     changedImages.erase(uB,changedImages.end());
00982 
00983     std::stringstream t;
00984     copy(changedImages.begin(), changedImages.end(),
00985          std::ostream_iterator<unsigned int>(t, " "));
00986     DEBUG_TRACE("changed image(s) " << t.str() << " begin");
00987     //force update of crops
00988     if(changedImages.size()>0)
00989     {
00990         for(UIntSet::iterator it=changedImages.begin();it!=changedImages.end();++it)
00991         {
00992             //if the projection was changed, we need to update the crop mode
00993             updateCropMode(*it);
00994             //now center the crop if user requested it
00995             if(state.images[*it]->getAutoCenterCrop())
00996             {
00997                 centerCrop(*it);
00998             };
00999         };
01000     };
01001     //update masks
01002     updateMasks();
01003     updateOptimizeVector();
01004     std::list<PanoramaObserver *>::iterator it;
01005     for(it = observers.begin(); it != observers.end(); ++it) {
01006         DEBUG_TRACE("notifying listener");
01007         if (changedImages.size() > 0 || m_forceImagesUpdate) {
01008             (*it)->panoramaImagesChanged(*this, changedImages);
01009         }
01010         (*it)->panoramaChanged(*this);
01011     }
01012     // reset changed images
01013     changedImages.clear();
01014     m_forceImagesUpdate = false;
01015     if (!keepDirty) {
01016         dirty = true;
01017         AppBase::DocumentData::setDirty(dirty);
01018     }
01019     DEBUG_TRACE("end");
01020 }
01021 
01022 void Panorama::updateMasksForImage(unsigned int imgNr, MaskPolygonVector newMasks)
01023 {
01024     DEBUG_ASSERT(imgNr < state.images.size());
01025     state.images[imgNr]->setMasks(newMasks);
01026     imageChanged(imgNr);
01027     m_forceImagesUpdate = true;
01028 };
01029 
01030 void Panorama::transferMask(MaskPolygon mask,unsigned int imgNr, const UIntSet& targetImgs)
01031 {
01032     if(targetImgs.size()==0)
01033     {
01034         return;
01035     };
01036     MaskPolygon transformedMask=mask;
01037     // clip positive mask to image boundaries or clip region
01038     vigra::Rect2D clipRect=vigra::Rect2D(0,0,state.images[imgNr]->getWidth(),state.images[imgNr]->getHeight());
01039     if(mask.isPositive())
01040     {
01041         //clip to crop region only positive masks
01042         switch(state.images[imgNr]->getCropMode())
01043         {
01044             case BaseSrcPanoImage::CROP_RECTANGLE:
01045                 clipRect=clipRect & state.images[imgNr]->getCropRect();
01046                 if(clipRect.isEmpty())
01047                 {
01048                     return;
01049                 };
01050                 if(clipRect.width()>10 && clipRect.height()>10)
01051                 {
01052                     clipRect.addBorder(-2);
01053                 };
01054                 break;
01055             case BaseSrcPanoImage::CROP_CIRCLE:
01056                 {
01057                     vigra::Rect2D cropRect=state.images[imgNr]->getCropRect();
01058                     hugin_utils::FDiff2D center((cropRect.left()+cropRect.right())/2.0,(cropRect.top()+cropRect.bottom())/2.0);
01059                     double radius=((cropRect.width()<cropRect.height())?cropRect.width():cropRect.height())/2.0;
01060                     if(radius>10)
01061                     {
01062                         radius-=2;
01063                     };
01064                     if(!transformedMask.clipPolygon(center,radius))
01065                     {
01066                         return;
01067                     };
01068                 };
01069                 break;
01070             default:
01071                 if(clipRect.width()>10 && clipRect.height()>10)
01072                 {
01073                     clipRect.addBorder(-2);
01074                 };
01075                 break;
01076         };
01077     };
01078     int origWindingNumber=transformedMask.getTotalWindingNumber();
01079     if(transformedMask.clipPolygon(clipRect))
01080     {
01081         //increase resolution of positive mask to get better transformation
01082         //of vertices, especially for fisheye images
01083         transformedMask.subSample(20);
01084         //transform polygon to panorama space
01085         HuginBase::PTools::Transform trans;
01086         trans.createInvTransform(getImage(imgNr),getOptions());
01087         transformedMask.transformPolygon(trans);
01088         for(UIntSet::const_iterator it=targetImgs.begin();it!=targetImgs.end();++it)
01089         {
01090             if(imgNr==(*it))
01091             {
01092                 continue;
01093             };
01094             MaskPolygon targetMask;
01095             if(state.images[imgNr]->YawisLinkedWith(*(state.images[*it])))
01096             {
01097                 //if yaw is linked, we simply copy the mask
01098                 targetMask=mask;
01099             }
01100             else
01101             {
01102                 targetMask=transformedMask;
01103                 PTools::Transform targetTrans;
01104                 targetTrans.createTransform(getImage(*it),getOptions());
01105                 targetMask.transformPolygon(targetTrans);
01106                 //check if transformation has produced invalid polygon
01107                 if(targetMask.getMaskPolygon().size()<3)
01108                 {
01109                     continue;
01110                 };
01111                 //check if mask was inverted - outside became inside and vice versa
01112                 //if so, invert mask
01113                 int newWindingNumber=targetMask.getTotalWindingNumber();
01114                 targetMask.setInverted(origWindingNumber * newWindingNumber < 0);
01115             };
01116             //now clip polygon to image rectangle, add mask only when polygon is inside image
01117             if(targetMask.clipPolygon(vigra::Rect2D(-maskOffset,-maskOffset,
01118                                       state.images[*it]->getWidth()+maskOffset,state.images[*it]->getHeight()+maskOffset)))
01119             {
01120                 targetMask.setMaskType(MaskPolygon::Mask_negative);
01121                 targetMask.setImgNr(*it);
01122                 state.images[*it]->addActiveMask(targetMask);
01123             };
01124         };
01125     };        
01126 };
01127 
01128 void Panorama::updateMasks(bool convertPosMaskToNeg)
01129 {
01130     // update masks
01131     UIntSet imgWithPosMasks;
01132     for(unsigned int i=0;i<state.images.size();i++)
01133     {
01134         state.images[i]->clearActiveMasks();
01135         if(state.images[i]->hasPositiveMasks())
01136         {
01137             imgWithPosMasks.insert(i);
01138         };
01139     };
01140     CalculateImageOverlap overlap(this);
01141     overlap.limitToImages(imgWithPosMasks);
01142     overlap.calculate(10);
01143     ConstStandardImageVariableGroups variable_groups(*this);
01144     ConstImageVariableGroup & lenses = variable_groups.getLenses();
01145     for(unsigned int i=0;i<state.images.size();i++)
01146     {
01147         if(state.images[i]->hasMasks())
01148         {
01149             MaskPolygonVector masks=state.images[i]->getMasks();
01150             for(unsigned int j=0;j<masks.size();j++)
01151             {
01152                 if(convertPosMaskToNeg)
01153                 {
01154                     //this is used for masking in the cp finder, we are consider
01155                     //all masks as negative masks, because at this moment
01156                     //the final position of the images is not known
01157                     switch(masks[j].getMaskType())
01158                     {
01159                         case MaskPolygon::Mask_negative:
01160                         case MaskPolygon::Mask_positive:
01161                             masks[j].setImgNr(i);
01162                             masks[j].setMaskType(MaskPolygon::Mask_negative);
01163                             state.images[i]->addActiveMask(masks[j]);
01164                             break;
01165                         case MaskPolygon::Mask_Stack_negative:
01166                         case MaskPolygon::Mask_Stack_positive:
01167                             {
01168                                 //copy mask to all images of the same stack
01169                                 UIntSet imgStack;
01170                                 for(unsigned int k=0;k<getNrOfImages();k++)
01171                                 {
01172                                     if(i!=k)
01173                                     {
01174                                         if(state.images[i]->StackisLinkedWith(*(state.images[k])))
01175                                         {
01176                                             imgStack.insert(k);
01177                                         };
01178                                     };
01179                                 };
01180                                 masks[j].setImgNr(i);
01181                                 masks[j].setMaskType(MaskPolygon::Mask_negative);
01182                                 state.images[i]->addActiveMask(masks[j]);
01183                                 transferMask(masks[j],i,imgStack);
01184                             };
01185                             break;
01186                         case MaskPolygon::Mask_negative_lens:
01187                             {
01188                                 unsigned int lensNr=lenses.getPartNumber(i);
01189                                 //copy masks to all image of the same lens
01190                                 for(unsigned int k=0;k<getNrOfImages();k++)
01191                                 {
01192                                     if(lenses.getPartNumber(k)==lensNr)
01193                                     {
01194                                         masks[j].setImgNr(k);
01195                                         masks[j].setMaskType(MaskPolygon::Mask_negative_lens);
01196                                         state.images[k]->addActiveMask(masks[j]);
01197                                     };
01198                                 };
01199                             };
01200                             break;
01201                     };
01202                 }
01203                 else
01204                 {
01205                     switch(masks[j].getMaskType())
01206                     {
01207                         case MaskPolygon::Mask_negative:
01208                             //negative mask, simply copy mask to active mask
01209                             masks[j].setImgNr(i);
01210                             state.images[i]->addActiveMask(masks[j]);
01211                             break;
01212                         case MaskPolygon::Mask_positive:
01213                             //propagate positive mask only if image is active
01214                             if(state.images[i]->getActive())
01215                             {
01216                                 UIntSet overlapImgs=overlap.getOverlapForImage(i);
01217                                 transferMask(masks[j],i,overlapImgs);
01218                             };
01219                             break;
01220                         case MaskPolygon::Mask_Stack_negative:
01221                             {
01222                                 //search all images of the stack
01223                                 UIntSet imgStack;
01224                                 for(unsigned int k=0;k<getNrOfImages();k++)
01225                                 {
01226                                     if(i!=k)
01227                                     {
01228                                         if(state.images[i]->StackisLinkedWith(*(state.images[k])))
01229                                         {
01230                                             imgStack.insert(k);
01231                                         };
01232                                     };
01233                                 };
01234                                 //copy mask also to the image which contains the mask
01235                                 masks[j].setImgNr(i);
01236                                 masks[j].setMaskType(MaskPolygon::Mask_negative);
01237                                 state.images[i]->addActiveMask(masks[j]);
01238                                 transferMask(masks[j],i,imgStack);
01239                             };
01240                             break;
01241                         case MaskPolygon::Mask_Stack_positive:
01242                             {
01243                                 //remove all images from the stack from the set
01244                                 UIntSet imgStack;
01245                                 fill_set(imgStack,0,getNrOfImages()-1);
01246                                 imgStack.erase(i);
01247                                 for(unsigned int k=0;k<getNrOfImages();k++)
01248                                 {
01249                                     if(i!=k)
01250                                     {
01251                                         if(state.images[i]->StackisLinkedWith(*(state.images[k])))
01252                                         {
01253                                             imgStack.erase(k);
01254                                         };
01255                                     };
01256                                 };
01257                                 //only leave overlapping images in set
01258                                 UIntSet imgOverlap=overlap.getOverlapForImage(i);
01259                                 UIntSet imgs;
01260                                 std::set_intersection(imgStack.begin(),imgStack.end(),imgOverlap.begin(),imgOverlap.end(),inserter(imgs,imgs.begin()));
01261                                 //now transfer mask
01262                                 transferMask(masks[j],i,imgs);
01263                             };
01264                             break;
01265                         case MaskPolygon::Mask_negative_lens:
01266                             {
01267                                 unsigned int lensNr=lenses.getPartNumber(i);
01268                                 //copy masks to all image of the same lens
01269                                 for(unsigned int k=0;k<getNrOfImages();k++)
01270                                 {
01271                                     if(lenses.getPartNumber(k)==lensNr)
01272                                     {
01273                                         masks[j].setImgNr(k);
01274                                         masks[j].setMaskType(MaskPolygon::Mask_negative_lens);
01275                                         state.images[k]->addActiveMask(masks[j]);
01276                                     };
01277                                 };
01278                             };
01279                             break;
01280                     };
01281                 };
01282             };
01283         };
01284     };
01285 };
01286 
01287 void Panorama::updateCropMode(unsigned int imgNr)
01288 {
01289     vigra::Rect2D r=state.images[imgNr]->getCropRect();
01290     if(r.isEmpty() || r==vigra::Rect2D(state.images[imgNr]->getSize()))
01291     {
01292         state.images[imgNr]->setCropMode(SrcPanoImage::NO_CROP);
01293     }
01294     else
01295     {
01296         if (state.images[imgNr]->isCircularCrop())
01297         {
01298             state.images[imgNr]->setCropMode(SrcPanoImage::CROP_CIRCLE);
01299         }
01300         else
01301         {
01302             state.images[imgNr]->setCropMode(SrcPanoImage::CROP_RECTANGLE);
01303         };
01304     };
01305 };
01306 
01307 vigra::Rect2D Panorama::centerCropImage(unsigned int imgNr)
01308 {
01309     vigra::Rect2D cropRect;
01310     if(state.images[imgNr]->getCropMode()==SrcPanoImage::NO_CROP)
01311     {
01312         return cropRect;
01313     };
01314     int dx = hugin_utils::roundi(state.images[imgNr]->getRadialDistortionCenterShift().x);
01315     int dy = hugin_utils::roundi(state.images[imgNr]->getRadialDistortionCenterShift().y);
01316     vigra::Point2D center = vigra::Point2D(state.images[imgNr]->getSize().width()/2 + dx, state.images[imgNr]->getSize().height()/2 + dy);
01317 
01318     vigra::Diff2D d(state.images[imgNr]->getCropRect().width() / 2, state.images[imgNr]->getCropRect().height() / 2);
01319     cropRect.setUpperLeft( center - d);
01320     cropRect.setLowerRight( center + d);
01321     return cropRect;
01322 };
01323 
01324 void Panorama::centerCrop(unsigned int imgNr)
01325 {
01326     vigra::Rect2D cropRect;
01327     if(  state.images[imgNr]->getCropMode()!=SrcPanoImage::NO_CROP &&
01328          state.images[imgNr]->getAutoCenterCrop() && 
01329        !(state.images[imgNr]->getCropRect().isEmpty())
01330        )
01331     {
01332         cropRect=centerCropImage(imgNr);
01333         if(!cropRect.isEmpty())
01334         {
01335             state.images[imgNr]->setCropRect(cropRect);
01336             imageChanged(imgNr);
01337         };
01338     };
01339     for (std::size_t i = 0; i < getNrOfImages(); i++)
01340     {
01341         if(i==imgNr)
01342         {
01343             continue;
01344         };
01345         if (state.images[imgNr]->RadialDistortionCenterShiftisLinkedWith(*state.images[i]))
01346         {
01347             if(  state.images[i]->getCropMode()!=SrcPanoImage::NO_CROP &&
01348                  state.images[i]->getAutoCenterCrop() && 
01349                !(state.images[i]->getCropRect().isEmpty())
01350                )
01351             {
01352                 cropRect=centerCropImage(i);
01353                 if(!cropRect.isEmpty())
01354                 {
01355                     state.images[i]->setCropRect(cropRect);
01356                     imageChanged(i);
01357                 };
01358             };
01359         };
01360     };
01361 };
01362 
01363 void UpdateOptVectorSet(std::set<std::string>& imgVar, const std::string& var, const bool opt)
01364 {
01365     if(opt)
01366     {
01367         imgVar.insert(var);
01368     }
01369     else
01370     {
01371         imgVar.erase(var);
01372     };
01373 };
01374 
01375 std::set<size_t> Panorama::getRefImages()
01376 {
01377     unsigned int refImg = getOptions().optimizeReferenceImage;
01378     std::set<size_t> refImgs;
01379     refImgs.insert(refImg);
01380     const HuginBase::SrcPanoImage & refImage = getImage(refImg);
01381     for (size_t imgNr = 0; imgNr < getNrOfImages(); imgNr++)
01382     {
01383         if(imgNr!=refImg)
01384         {
01385             const HuginBase::SrcPanoImage & compImage = getImage(imgNr);
01386             if (refImage.YawisLinkedWith(compImage))
01387             {
01388                 refImgs.insert(imgNr);
01389             };
01390         };
01391     };
01392     return refImgs;
01393 };
01394 
01395 void Panorama::checkRefOptStatus(bool& linkRefImgsYaw, bool& linkRefImgsPitch, bool& linkRefImgsRoll)
01396 {
01397     // count number of vertical/horizontal control points
01398     int nHCP = 0;
01399     int nVCP = 0;
01400     const CPVector & cps = getCtrlPoints();
01401     for (CPVector::const_iterator it = cps.begin(); it != cps.end(); ++it)
01402     {
01403         // control points
01404         if (it->mode == ControlPoint::X)
01405         {
01406             nVCP++;
01407         }
01408         else
01409         {
01410             if (it->mode == ControlPoint::Y)
01411             {
01412                 nHCP++;
01413             }
01414         };
01415     };
01416 
01417     // try to select sensible position optimisation parameters,
01418     // dependent on output projection
01419     linkRefImgsYaw=false;
01420     linkRefImgsPitch=false;
01421     linkRefImgsRoll=false;
01422     switch (getOptions().getProjection())
01423     {
01424         case PanoramaOptions::RECTILINEAR:
01425             linkRefImgsRoll = nVCP + nHCP >= 1;
01426             linkRefImgsYaw = nVCP + nHCP >= 3 && nVCP >= 1 && nHCP >= 1;
01427             linkRefImgsPitch = nVCP + nHCP >= 2;
01428             break;
01429         case PanoramaOptions::CYLINDRICAL:
01430         case PanoramaOptions::EQUIRECTANGULAR:
01431             linkRefImgsPitch =  nHCP + nVCP > 1;
01432             linkRefImgsRoll = nHCP + nVCP >= 1;
01433             break;
01434         default:
01435             break;
01436     };
01437 };
01438 
01439 void Panorama::updateOptimizeVector()
01440 {
01441     if(state.images.size()==0)
01442     {
01443         return;
01444     };
01445     if(state.optSwitch!=0)
01446     {
01447         std::set<size_t> refImgs=getRefImages();
01448         bool linkRefImgsYaw=false;
01449         bool linkRefImgsPitch=false;
01450         bool linkRefImgsRoll=false;
01451         checkRefOptStatus(linkRefImgsYaw, linkRefImgsPitch, linkRefImgsRoll);
01452 
01453         for(size_t i=0;i<getNrOfImages();i++)
01454         {
01455             if(state.optSwitch & OPT_PAIR || state.optSwitch & OPT_POSITION || state.optSwitch & OPT_ALL)
01456             {
01457                 if(set_contains(refImgs,i))
01458                 {
01459                     UpdateOptVectorSet(state.optvec[i],"y",linkRefImgsYaw);
01460                     UpdateOptVectorSet(state.optvec[i],"p",linkRefImgsPitch);
01461                     UpdateOptVectorSet(state.optvec[i],"r",linkRefImgsRoll);
01462                     //don't optimize translation parameters of anchor
01463                     UpdateOptVectorSet(state.optvec[i],"TrX",false);
01464                     UpdateOptVectorSet(state.optvec[i],"TrY",false);
01465                     UpdateOptVectorSet(state.optvec[i],"TrZ",false);
01466                 }
01467                 else
01468                 {
01469                     UpdateOptVectorSet(state.optvec[i],"y",true);
01470                     UpdateOptVectorSet(state.optvec[i],"p",true);
01471                     UpdateOptVectorSet(state.optvec[i],"r",true);
01472                     UpdateOptVectorSet(state.optvec[i],"TrX",(state.optSwitch & OPT_TRANSLATION)>0);
01473                     UpdateOptVectorSet(state.optvec[i],"TrY",(state.optSwitch & OPT_TRANSLATION)>0);
01474                     UpdateOptVectorSet(state.optvec[i],"TrZ",(state.optSwitch & OPT_TRANSLATION)>0);
01475                 };
01476             }
01477             else
01478             {
01479                 UpdateOptVectorSet(state.optvec[i],"y",false);
01480                 UpdateOptVectorSet(state.optvec[i],"p",false);
01481                 UpdateOptVectorSet(state.optvec[i],"r",false);
01482                 UpdateOptVectorSet(state.optvec[i],"Trx",false);
01483                 UpdateOptVectorSet(state.optvec[i],"Try",false);
01484                 UpdateOptVectorSet(state.optvec[i],"Trz",false);
01485             };
01486             UpdateOptVectorSet(state.optvec[i],"v",state.optSwitch & OPT_VIEW || state.optSwitch & OPT_ALL);
01487             UpdateOptVectorSet(state.optvec[i],"a",(state.optSwitch & OPT_ALL)>0);
01488             UpdateOptVectorSet(state.optvec[i],"b",state.optSwitch & OPT_BARREL || state.optSwitch & OPT_ALL);
01489             UpdateOptVectorSet(state.optvec[i],"c",(state.optSwitch & OPT_ALL)>0);
01490             UpdateOptVectorSet(state.optvec[i],"d",(state.optSwitch & OPT_ALL)>0);
01491             UpdateOptVectorSet(state.optvec[i],"e",(state.optSwitch & OPT_ALL)>0);
01492             //shear and translation plane not include in master switches
01493             UpdateOptVectorSet(state.optvec[i],"g",false);
01494             UpdateOptVectorSet(state.optvec[i],"t",false);
01495             UpdateOptVectorSet(state.optvec[i],"Tpy", false);
01496             UpdateOptVectorSet(state.optvec[i],"Tpp", false);
01497         };
01498     };
01499     if(state.optPhotoSwitch!=0)
01500     {
01501         for(size_t i=0;i<getNrOfImages();i++)
01502         {
01503             UpdateOptVectorSet(state.optvec[i],"Eev",state.optPhotoSwitch & OPT_EXPOSURE && i!=state.options.colorReferenceImage);
01504             UpdateOptVectorSet(state.optvec[i],"Er", state.optPhotoSwitch & OPT_WHITEBALANCE && i!=state.options.colorReferenceImage);
01505             UpdateOptVectorSet(state.optvec[i],"Eb", state.optPhotoSwitch & OPT_WHITEBALANCE && i!=state.options.colorReferenceImage);
01506             UpdateOptVectorSet(state.optvec[i],"Vb", (state.optPhotoSwitch & OPT_VIGNETTING)>0);
01507             UpdateOptVectorSet(state.optvec[i],"Vc", (state.optPhotoSwitch & OPT_VIGNETTING)>0);
01508             UpdateOptVectorSet(state.optvec[i],"Vd", (state.optPhotoSwitch & OPT_VIGNETTING)>0);
01509             UpdateOptVectorSet(state.optvec[i],"Vx", (state.optPhotoSwitch & OPT_VIGNETTING_CENTER)>0);
01510             UpdateOptVectorSet(state.optvec[i],"Vy", (state.optPhotoSwitch & OPT_VIGNETTING_CENTER)>0);
01511             UpdateOptVectorSet(state.optvec[i],"Ra", (state.optPhotoSwitch & OPT_RESPONSE)>0);
01512             UpdateOptVectorSet(state.optvec[i],"Rb", (state.optPhotoSwitch & OPT_RESPONSE)>0);
01513             UpdateOptVectorSet(state.optvec[i],"Rc", (state.optPhotoSwitch & OPT_RESPONSE)>0);
01514             UpdateOptVectorSet(state.optvec[i],"Rd", (state.optPhotoSwitch & OPT_RESPONSE)>0);
01515             UpdateOptVectorSet(state.optvec[i],"Re", (state.optPhotoSwitch & OPT_RESPONSE)>0);
01516         };
01517     };
01518 };
01519 
01520 void Panorama::swapImages(unsigned int img1, unsigned int img2)
01521 {
01522     DEBUG_TRACE("swapping images " << img1 << ", " << img2);
01523     DEBUG_ASSERT(img1 < state.images.size());
01524     DEBUG_ASSERT(img2 < state.images.size());
01525 
01526     // first, swap image pointers in the list.
01527     SrcPanoImage * pimg1 = state.images[img1];
01528     state.images[img1] = state.images[img2];
01529     state.images[img2] = pimg1;
01530     
01531     // update control points
01532     for (CPVector::iterator it=state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
01533         int n1 = (*it).image1Nr;
01534         int n2 = (*it).image2Nr;
01535         if ((*it).image1Nr == img1) {
01536             n1 = img2;
01537         } else if ((*it).image1Nr == img2) {
01538             n1 = img1;
01539         }
01540         if ((*it).image2Nr == img1) {
01541             n2 = img2;
01542         } else if ((*it).image2Nr == img2) {
01543             n2 = img1;
01544         }
01545         (*it).image1Nr = n1;
01546         (*it).image2Nr = n2;
01547     }
01548 
01549     // update panorama options
01550     if (state.options.colorReferenceImage == img1) {
01551         state.options.colorReferenceImage = img2;
01552     } else if (state.options.colorReferenceImage == img2) {
01553         state.options.colorReferenceImage = img1;
01554     }
01555     if (state.options.optimizeReferenceImage == img1) {
01556         state.options.optimizeReferenceImage = img2;
01557     } else if (state.options.optimizeReferenceImage == img2) {
01558         state.options.optimizeReferenceImage = img1;
01559     }
01560     imageChanged(img1);
01561     imageChanged(img2);
01562 }
01563 
01564 void Panorama::moveImage(size_t img1, size_t img2)
01565 {
01566     //generate a vector with the translated image numbers
01567     std::vector<size_t> imgList(getNrOfImages(),-1);
01568     for(size_t i=0; i<getNrOfImages(); i++)
01569     {
01570         imgList[i]=i;
01571     };
01572     imgList.erase(imgList.begin()+img1);
01573     if(img2<imgList.size())
01574     {
01575         imgList.insert(imgList.begin()+img2, img1);
01576     }
01577     else
01578     {
01579         imgList.push_back(img1);
01580     };
01581     //generate map for translation of old -> new image numbers
01582     std::map<size_t,size_t> imgMap;
01583     for(size_t i=0; i<imgList.size(); i++)
01584     {
01585         imgMap[imgList[i]]=i;
01586     };
01587     // now generate the new images list
01588     std::vector<SrcPanoImage *> new_images(getNrOfImages());
01589     for(size_t i=0; i<imgList.size(); i++)
01590     {
01591         new_images[i]=state.images[imgList[i]];
01592         if(i!=imgList[i])
01593         {
01594             imageChanged(imgList[i]);
01595         };
01596     };
01597     state.images=new_images;
01598 
01599     // update optimize vector
01600     OptimizeVector newOptVec;
01601     for(size_t i=0; i<state.optvec.size(); i++)
01602     {
01603         newOptVec.push_back(state.optvec[imgList[i]]);
01604     };
01605     state.optvec=newOptVec;
01606 
01607     // update control points
01608     for (CPVector::iterator it=state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it)
01609     {
01610         (*it).image1Nr = imgMap[(*it).image1Nr];
01611         (*it).image2Nr = imgMap[(*it).image2Nr];
01612     }
01613 
01614     // update panorama options
01615     state.options.colorReferenceImage=imgMap[state.options.colorReferenceImage];
01616     state.options.optimizeReferenceImage=imgMap[state.options.optimizeReferenceImage];
01617 };
01618 
01619 bool Panorama::setMementoToCopyOf(const PanoramaDataMemento* memento)
01620 {
01621     if(memento==NULL)
01622         return false;
01623     
01624     const PanoramaMemento* mymemento;
01625         
01626     try {
01627         
01628         mymemento = dynamic_cast<const PanoramaMemento*>(memento);
01629         
01630     } catch (std::bad_cast&) {
01631 //        std::cerr << "Incompatible memento type." << std::endl;
01632         DEBUG_DEBUG("Incompatible memento type.");
01633         return false;
01634     }
01635     
01636     setMemento(PanoramaMemento(*mymemento));
01637     return true;
01638 }
01639 
01640 
01642 void Panorama::setMemento(const PanoramaMemento& memento)
01643 {
01644     DEBUG_TRACE("");
01645     
01646     // remove old content.
01647     reset();
01648     DEBUG_DEBUG("nr of images in memento:" << memento.images.size());
01649     
01650     state = memento;
01651     updateMasks();
01652     unsigned int nNewImages = state.images.size();
01653     DEBUG_DEBUG("nNewImages:" << nNewImages);
01654     
01655     // send changes for all images
01656     for (unsigned int i = 0; i < nNewImages; i++) {
01657         imageChanged(i);
01658     }
01659 }
01660 
01661 PanoramaDataMemento* Panorama::getNewMemento() const
01662 {
01663     return new PanoramaMemento(getMemento());
01664 }
01665 
01666 void Panorama::setOptions(const PanoramaOptions & opt)
01667 {
01668     if (state.options.optimizeReferenceImage != opt.optimizeReferenceImage) {
01669         imageChanged(opt.optimizeReferenceImage);
01670         imageChanged(state.options.optimizeReferenceImage);
01671     }
01672 
01673     if (state.options.colorReferenceImage != opt.colorReferenceImage) {
01674         imageChanged(opt.colorReferenceImage);
01675         imageChanged(state.options.colorReferenceImage);
01676     }
01677 
01678     state.options = opt;
01679 }
01680 
01681 void Panorama::addObserver(PanoramaObserver * o)
01682 {
01683     observers.push_back(o);
01684 }
01685 
01686 bool Panorama::removeObserver(PanoramaObserver * o)
01687 {
01688     size_t oldCount=observers.size();
01689     observers.remove(o);
01690     return observers.size()!=oldCount;
01691 }
01692 
01693 void Panorama::clearObservers()
01694 {
01695     observers.clear();
01696 }
01697 
01698 const bool Panorama::hasPendingChanges() const
01699 {
01700     return !changedImages.empty();
01701 }
01702 
01703 void Panorama::imageChanged(unsigned int imgNr)
01704 {
01705 //    DEBUG_TRACE("adding image " << imgNr);
01706     changedImages.insert(imgNr);
01707     assert(changedImages.find(imgNr) != changedImages.end());
01708 }
01709 
01710 void Panorama::activateImage(unsigned int imgNr, bool active)
01711 {
01712     assert(imgNr < state.images.size());
01713     if (state.images[imgNr]->getActive() != active)
01714     {
01715         state.images[imgNr]->setActive(active);
01716         imageChanged(imgNr);
01717     }
01718 }
01719 
01720 UIntSet Panorama::getActiveImages() const
01721 {
01722         UIntSet activeImgs;
01723 
01724     for (unsigned int i = 0; i < state.images.size(); i++) {
01725         if (state.images[i]->getActive())
01726         {
01727             activeImgs.insert(i);
01728         }
01729     }
01730         return activeImgs;
01731 }
01732 
01733 const std::string Panorama::getICCProfileDesc() const
01734 {
01735     return state.iccProfileDesc;
01736 };
01737 
01738 void Panorama::setICCProfileDesc(const std::string& newDesc)
01739 {
01740     state.iccProfileDesc = newDesc;
01741 };
01742 
01743 //==== internal function for variable management
01744 
01745 SrcPanoImage Panorama::getSrcImage(unsigned imgNr) const
01746 {
01747     DEBUG_ASSERT(imgNr < state.images.size());
01748     return *state.images[imgNr];
01749 }
01750 
01751 void Panorama::setSrcImage(unsigned int imgNr, const SrcPanoImage & img)
01752 {
01753     DEBUG_ASSERT(imgNr < state.images.size());
01754     
01755     /* Copy the variables. We don't assign directly so we can do the changes to
01756      * any linked variables.
01757      */
01758     SrcPanoImage *dest = state.images[imgNr];
01759     #define image_variable( name, type, default_value ) \
01760     dest->set##name (img.get##name());
01761     #include "image_variables.h"
01762     #undef image_variable
01763     
01764     // mark the potentially changed images.
01765 #define image_variable( name, type, default_value ) \
01766     for (std::size_t i = 0; i < getNrOfImages(); i++)\
01767     {\
01768         if(state.images[imgNr]->name##isLinkedWith(*state.images[i]))\
01769         {\
01770             imageChanged(i);\
01771         }\
01772     }
01773 #include "image_variables.h"
01774 #undef image_variable
01775 }
01776 
01777 
01778 Panorama Panorama::duplicate() const
01779 {
01780     Panorama pano(*this);
01781     pano.observers.clear();
01782     return pano;
01783 }
01784 
01785 Panorama Panorama::getSubset(const UIntSet & imgs) const
01786 {
01787     Panorama subset;
01788     // copy data except for listners
01789     
01790     // bits that don't change in the subset.
01791     subset.imgFilePrefix = imgFilePrefix;
01792     subset.dirty = dirty;
01793     subset.state.options = state.options;
01794     subset.state.optSwitch=0;
01795     subset.state.optPhotoSwitch=0;
01796     subset.state.needsOptimization = state.needsOptimization;
01797     subset.changedImages = changedImages;
01798     subset.m_forceImagesUpdate = m_forceImagesUpdate;
01799     subset.m_ptoptimizerVarNames = m_ptoptimizerVarNames;
01800     
01801     // create image number map.
01802     std::map<unsigned int, unsigned int> imageNrMap;
01803 
01804     // copy image information
01805     unsigned int ic = 0;
01806     for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
01807          ++imgNrIt)
01808     {
01809         subset.state.images.push_back(new SrcPanoImage(*state.images[*imgNrIt]));
01810         subset.state.optvec.push_back(state.optvec[*imgNrIt]);
01811         imageNrMap[*imgNrIt] = ic;
01812         ic++;
01813     }
01814     
01815     // recreate links between image variables.
01816     ic = 0;
01817     for (UIntSet::const_iterator i = imgs.begin(); i != imgs.end(); ++i)
01818     {
01819         unsigned int jc = ic + 1;
01820         UIntSet::const_iterator j = i;
01821         for (++j; j != imgs.end(); ++j)
01822         {
01827 #define image_variable( name, type, default_value )\
01828             if (state.images[*i]->name##isLinkedWith(*state.images[*j]))\
01829             {\
01830                 subset.state.images[ic]->link##name(subset.state.images[jc]);\
01831             }
01832 #include "image_variables.h"
01833 #undef image_variable
01834             jc++;
01835         }
01836         ic++;
01837     }
01838 
01839     // select and translate control points.
01840     subset.state.ctrlPoints.clear();
01841     for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
01842         if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
01843             ControlPoint pnt = *it;
01844             pnt.image1Nr = imageNrMap[pnt.image1Nr];
01845             pnt.image2Nr = imageNrMap[pnt.image2Nr];
01846             subset.state.ctrlPoints.push_back(pnt);
01847         }
01848     }
01849 
01850     //update optimizeReferenceImage and colorReferenceImage number
01851     unsigned int newRefImg=0;
01852     std::map<unsigned int, unsigned int>::iterator it=imageNrMap.find(state.options.optimizeReferenceImage);
01853     if(it!=imageNrMap.end())
01854     {
01855         newRefImg=it->second;
01856     };
01857     it=imageNrMap.find(state.options.colorReferenceImage);
01858     subset.state.options.optimizeReferenceImage=newRefImg;
01859     newRefImg=0;
01860     if(it!=imageNrMap.end())
01861     {
01862         newRefImg=it->second;
01863     }
01864     subset.state.options.colorReferenceImage=newRefImg;
01865     return subset;
01866 }
01867 
01868 int FindStackNumberForImage(const std::vector<UIntSet>& imageGroups, const unsigned int imgNr)
01869 {
01870     for (size_t i = 0; i < imageGroups.size(); ++i)
01871     {
01872         if (set_contains(imageGroups[i], imgNr))
01873         {
01874             return i;
01875         };
01876     };
01877     return -1;
01878 };
01879 
01880 PanoramaData* Panorama::getUnlinkedSubset(UIntSetVector& imageGroups) const
01881 {
01882     const CPVector cps = getCtrlPoints();
01883     CPVector newCP;
01884 
01885     // remove all connected images, keep only a single image for each connected stack
01886     imageGroups.clear();
01887     std::vector<bool> visitedImages(getNrOfImages(), false);
01888     for (size_t i = 0; i < getNrOfImages(); ++i)
01889     {
01890         if (visitedImages[i])
01891         {
01892             continue;
01893         };
01894         const SrcPanoImage& img1 = getImage(i);
01895         UIntSet imgs;
01896         imgs.insert(i);
01897         visitedImages[i] = true;
01898         if (img1.YawisLinked())
01899         {
01900             for (size_t j = i + 1; j < getNrOfImages(); ++j)
01901             {
01902                 if (img1.YawisLinkedWith(getImage(j)))
01903                 {
01904                     imgs.insert(j);
01905                     visitedImages[j] = true;
01906                 }
01907             }
01908         };
01909         imageGroups.push_back(imgs);
01910     };
01911     UIntSet singleStackImgs;
01912     for (size_t i = 0; i < imageGroups.size(); ++i)
01913     {
01914         singleStackImgs.insert(*imageGroups[i].begin());
01915     };
01916     // new generate subpano
01917     PanoramaData* subPano = getNewSubset(singleStackImgs);
01918     // translate now reference image
01919     int newRefImage = FindStackNumberForImage(imageGroups, getOptions().optimizeReferenceImage);
01920     if (newRefImage != -1)
01921     {
01922         PanoramaOptions opts = subPano->getOptions();
01923         opts.optimizeReferenceImage = newRefImage;
01924         subPano->setOptions(opts);
01925     };
01926     // remove all vertical and horizontal cp, also remap all cps to new subpano
01927     // all cps from removed images will be mapped to first image of corresponding stack
01928     for (CPVector::const_iterator it = cps.begin(); it != cps.end(); ++it)
01929     {
01930         if (it->mode == ControlPoint::X_Y)
01931         {
01932             ControlPoint cp(*it);
01933             int newImg1 = FindStackNumberForImage(imageGroups, cp.image1Nr);
01934             int newImg2 = FindStackNumberForImage(imageGroups, cp.image2Nr);
01935             if (newImg1 != -1 && newImg2 != -1 && newImg1 != newImg2)
01936             {
01937                 cp.image1Nr = newImg1;
01938                 cp.image2Nr = newImg2;
01939                 newCP.push_back(cp);
01940             };
01941         };
01942     };
01943     subPano->setCtrlPoints(newCP);
01944     return subPano;
01945 }
01946 
01947 void Panorama::mergePanorama(const Panorama &newPano)
01948 {
01949     if(newPano.getNrOfImages()>0)
01950     {
01951         std::vector<unsigned int> new_image_nr(newPano.getNrOfImages());
01952         HuginBase::OptimizeVector optVec=getOptimizeVector();
01953         HuginBase::OptimizeVector optVecNew=newPano.getOptimizeVector();
01954         //add only new images
01955         for(unsigned int i=0;i<newPano.getNrOfImages();i++)
01956         {
01957             std::string filename=newPano.getImage(i).getFilename();
01958             bool found=false;
01959             for(unsigned int j=0;j<getNrOfImages();j++)
01960             {
01961                 if(getImage(j).getFilename()==filename)
01962                 {
01963                     //image is already in panorama, we remember the image nr
01964                     found=true;
01965                     new_image_nr[i]=j;
01966                     // now check if we have to update the masks
01967                     HuginBase::MaskPolygonVector masksOld=getImage(j).getMasks();
01968                     HuginBase::MaskPolygonVector masksNew=newPano.getImage(i).getMasks();
01969                     if(masksNew.size()>0)
01970                     {
01971                         for(unsigned int k=0;k<masksNew.size();k++)
01972                         {
01973                             bool usedMasks=false;
01974                             unsigned int l=0;
01975                             while((!usedMasks) && l<masksOld.size())
01976                             {
01977                                 usedMasks=(masksNew[k]==masksOld[l]);
01978                                 l++;
01979                             };
01980                             if(!usedMasks)
01981                                 masksOld.push_back(masksNew[k]);
01982                         };
01983                         updateMasksForImage(j,masksOld);
01984                     };
01985                     break;
01986                 };
01987             };
01988             if(!found)
01989             {
01990                 //new image found, add it
01991                 new_image_nr[i]=addImage(newPano.getImage(i));
01992                 //copy also optimise vector
01993                 optVec.push_back(optVecNew[i]);
01994             };
01995         };
01996         setOptimizeVector(optVec);
01997         // recreate links between image variables.
01998         for (unsigned int i=0; i<newPano.getNrOfImages(); i++)
01999         {
02000             for(unsigned int j=i+1;j<newPano.getNrOfImages();j++)
02001             {
02002                 const HuginBase::SrcPanoImage &img=newPano.getImage(i);
02003 #define image_variable( name, type, default_value )\
02004                 if(img.name##isLinkedWith(newPano.getImage(j)))\
02005                 {\
02006                     linkImageVariable##name(new_image_nr[i],new_image_nr[j]);\
02007                 };
02008 #include "panodata/image_variables.h"
02009 #undef image_variable
02010             }
02011         }
02012         //now translate cp
02013         CPVector cps=newPano.getCtrlPoints();
02014         const int nextLineCPOffset = getNextCPTypeLineNumber() - 3;
02015         for(unsigned int i=0;i<cps.size();i++)
02016         {
02017             // special treatment of line control points
02018             if (cps[i].mode > 2)
02019             {
02020                 addCtrlPoint(HuginBase::ControlPoint(new_image_nr[cps[i].image1Nr], cps[i].x1, cps[i].y1,
02021                     new_image_nr[cps[i].image2Nr], cps[i].x2, cps[i].y2, cps[i].mode + nextLineCPOffset));
02022             }
02023             else
02024             {
02025                 // normal, horizontal and vertical cp keep their mode
02026                 addCtrlPoint(HuginBase::ControlPoint(new_image_nr[cps[i].image1Nr], cps[i].x1, cps[i].y1,
02027                     new_image_nr[cps[i].image2Nr], cps[i].x2, cps[i].y2, cps[i].mode));
02028             };
02029         };
02030         removeDuplicateCtrlPoints();
02031     };
02032 };
02033 
02034 int Panorama::getNextCPTypeLineNumber() const
02035 {
02036     int t=0;
02037     for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it)
02038     {
02039         t = std::max(t, it->mode);
02040     }
02041     if (t <= 2) {
02042         t=2;
02043     }
02044     return t+1;
02045 }
02046 
02047 
02048 Panorama::ReadWriteError Panorama::readData(std::istream& dataInput, std::string documentType)
02049 {
02050     // [TODO] check the document type, return INCOMPATIBLE_TYPE
02051     
02052     if(!dataInput.good() || dataInput.eof())
02053     {
02054         DEBUG_WARN("Failed to read from dataInput.");
02055         return INVALID_DATA;
02056     }
02057     
02058     PanoramaMemento newPano;
02059     int ptoVersion;
02060     if (newPano.loadPTScript(dataInput, ptoVersion, getFilePrefix())) {
02061         
02062         this->setMemento(newPano);
02063         return SUCCESSFUL;
02064         
02065     } else {
02066         DEBUG_FATAL("Could not parse the data input successfully.");
02067         return PARCER_ERROR;
02068     }
02069 }
02070 
02072 Panorama::ReadWriteError Panorama::writeData(std::ostream& dataOutput, std::string documentType)
02073 {
02074     UIntSet all;
02075     
02076     if (getNrOfImages() > 0)
02077         fill_set(all, 0, getNrOfImages()-1);
02078     
02079     printPanoramaScript(dataOutput, getOptimizeVector(), getOptions(), all, false, getFilePrefix());
02080     
02081     return SUCCESSFUL;
02082 }
02083 
02084 void Panorama::updateWhiteBalance(double redFactor, double blueFactor)
02085 {
02086     UIntSet modified_images;
02087     for(unsigned int i=0;i<getNrOfImages();i++)
02088     {
02089         if(!set_contains(modified_images,i))
02090         {
02091             state.images[i]->setWhiteBalanceRed(redFactor * state.images[i]->getWhiteBalanceRed());
02092             state.images[i]->setWhiteBalanceBlue(blueFactor * state.images[i]->getWhiteBalanceBlue());
02093             modified_images.insert(i);
02094             imageChanged(i);
02095             //check linked images and remember for later
02096             if(state.images[i]->WhiteBalanceRedisLinked())
02097             {
02098                 if(i+1<getNrOfImages())
02099                 {
02100                     for(unsigned int j=i+1;j<getNrOfImages();j++)
02101                     {
02102                         if(state.images[i]->WhiteBalanceRedisLinkedWith(*(state.images[j])))
02103                         {
02104                             modified_images.insert(j);
02105                             imageChanged(j);
02106                         };
02107                     };
02108                 };
02109             };
02110         };
02111     };
02112 };
02113 
02114 const double Panorama::getMaxExposureDifference() const
02115 {
02116     if (state.images.empty())
02117     {
02118         return 0;
02119     }
02120     double minEv = 1000;
02121     double maxEv = -1000;
02122     for (size_t i = 0; i < state.images.size(); i++)
02123     {
02124         const double ev = state.images[i]->getExposureValue();
02125         minEv = std::min(minEv, ev);
02126         maxEv = std::max(maxEv, ev);
02127     };
02128     return maxEv - minEv;
02129 };
02130 
02131 const bool Panorama::hasPossibleStacks() const
02132 {
02133     if (state.images.empty())
02134     {
02135         return false;
02136     }
02137     // this algorithm is based on panostart by Bruno Postle
02138     // bracketed pano has at least a dynamic range of 1.2 ev values (corresponds to bracket with +-2/3)
02139     if (getMaxExposureDifference()<1.2)
02140     {
02141         return false;
02142     }
02143     //now get all exposure layers
02144     UIntSet allImg;
02145     fill_set(allImg, 0, state.images.size() - 1);
02146     UIntSetVector evValues = getExposureLayers(*this, allImg, 0.3);
02147     //if there is only one unique exposure value then there are no stacks
02148     if (evValues.size()<2)
02149     {
02150         return false;
02151     }
02152     //if number of unique exposure values is equal the number of images then there are no stacks
02153     if (evValues.size() == state.images.size())
02154     {
02155         return false;
02156     }
02157     //if number of images is not a multiple of number of unique exposure values
02158     //then the stacks are incomplete, skipping
02159     if (state.images.size() % evValues.size() != 0)
02160     {
02161         return false;
02162     }
02163     //check if exposure value is repeated with step size of bracket size
02164     if (set_contains(evValues[0], evValues.size()))
02165     {
02166         return true;
02167     }
02168     else
02169     {
02170         return false;
02171     };
02172 };
02173 
02175 void Panorama::linkPossibleStacks(bool linkPosition)
02176 {
02177     // we need at least 2 images
02178     if (state.images.size()<=1)
02179     {
02180         return;
02181     };
02182     // unlink all existing stacks
02183     for (size_t imgNr = 0; imgNr < state.images.size(); imgNr++)
02184     {
02185         if (state.images[imgNr]->YawisLinked())
02186         {
02187             unlinkImageVariableYaw(imgNr);
02188             unlinkImageVariablePitch(imgNr);
02189             unlinkImageVariableRoll(imgNr);
02190             unlinkImageVariableX(imgNr);
02191             unlinkImageVariableY(imgNr);
02192             unlinkImageVariableZ(imgNr);
02193             unlinkImageVariableTranslationPlaneYaw(imgNr);
02194             unlinkImageVariableTranslationPlanePitch(imgNr);
02195         };
02196         if (state.images[imgNr]->StackisLinked())
02197         {
02198             unlinkImageVariableStack(imgNr);
02199         };
02200     };
02201     // now link all possible stacks
02202     UIntSet allImg;
02203     fill_set(allImg, 0, state.images.size() - 1);
02204     UIntSetVector evValues = getExposureLayers(*this, allImg, 0.3);
02205     if (evValues.empty())
02206     {
02207         return;
02208     };
02209     unsigned int imgNr = 0;
02210     for (size_t i = 1; i<state.images.size(); i++)
02211     {
02212         if (set_contains(evValues[0], i))
02213         {
02214             imgNr = i;
02215         }
02216         else
02217         {
02218             linkImageVariableStack(imgNr, i);
02219             if (linkPosition)
02220             {
02221                 linkImageVariableYaw(imgNr, i);
02222                 linkImageVariablePitch(imgNr, i);
02223                 linkImageVariableRoll(imgNr, i);
02224                 linkImageVariableX(imgNr, i);
02225                 linkImageVariableY(imgNr, i);
02226                 linkImageVariableZ(imgNr, i);
02227                 linkImageVariableTranslationPlaneYaw(imgNr, i);
02228                 linkImageVariableTranslationPlanePitch(imgNr, i);
02229             };
02230         };
02231     };
02232 };
02233 
02234 PanoramaMemento::PanoramaMemento(const PanoramaMemento & data)
02235 {
02236     // Use the assignment operator to get the work done: see the next function.
02237     *this = data;
02238 }
02239 
02240 PanoramaMemento & PanoramaMemento::operator=(const PanoramaMemento & data)
02241 {
02242     // Copy the PanoramaMemento.
02243     
02244     // Don't do anything in the case of self assignment. This is important as we
02245     // are about to delete the image information.
02246     if (&data == this)
02247     {
02248         return *this;
02249     }
02250     
02251     // Remove any images we currently had.
02252     deleteAllImages();
02253     // copy image variables
02254     for (std::vector<SrcPanoImage *>::const_iterator it = data.images.begin();
02255          it != data.images.end(); ++it)
02256     {
02257         images.push_back(new SrcPanoImage(*(*it)));
02258     }
02259     // Copies of SrcPanoImage's variables aren't linked, so we have to create
02260     // new links in the same pattern.
02266     std::size_t num_imgs = images.size();
02267     for (std::size_t i = 0; i < num_imgs; i++)
02268     {
02269         // copy this image's links.
02270         // links to lower numbered images will have already been spotted, since
02271         // they are bi-directional.
02272         for (std::size_t j = i + 1; j < num_imgs; j++)
02273         {
02274 #define image_variable( name, type, default_value )\
02275             if (data.images[i]->name##isLinkedWith(*data.images[j]))\
02276             {\
02277                 images[i]->link##name(images[j]);\
02278             }
02279 #include "image_variables.h"
02280 #undef image_variable
02281         }
02282     }
02283     
02284     ctrlPoints = data.ctrlPoints;
02285     iccProfileDesc = data.iccProfileDesc;
02286     
02287     options = data.options;
02288     
02289     optSwitch = data.optSwitch;
02290     optPhotoSwitch = data.optPhotoSwitch;
02291     optvec = data.optvec;
02292 
02293     needsOptimization = data.needsOptimization;
02294     
02295     return *this;
02296 }
02297 
02298 PanoramaMemento::~PanoramaMemento()
02299 {
02300     deleteAllImages();
02301 }
02302 
02303 void PanoramaMemento::deleteAllImages()
02304 {
02305     // delete all the images pointed to by the images vector.
02306     for (std::vector<SrcPanoImage *>::iterator it = images.begin();
02307          it != images.end(); ++it)
02308     {
02309         delete *it;
02310     }
02311     // now clear the pointers themselves.
02312     images.clear();
02313 }
02314 
02315 bool PanoramaMemento::loadPTScript(std::istream &i, int & ptoVersion, const std::string &prefix)
02316 {
02317     DEBUG_TRACE("");
02318     // set numeric locale to C, for correct number output
02319     char * p = setlocale(LC_NUMERIC,NULL);
02320     char * old_locale = strdup(p);
02321     setlocale(LC_NUMERIC,"C");
02322     std::string line;
02323 
02324     // vector with the different information lines about images
02325     std::vector<PTScriptParsing::ImgInfo> oImgInfo;
02326     std::vector<PTScriptParsing::ImgInfo> iImgInfo;
02327     // strange comment information.
02328     std::vector<PTScriptParsing::ImgInfo> cImgInfo;
02329     // hugin additional information
02330     std::vector<PTScriptParsing::ImgInfo> huginImgInfo;
02331     // vector with readed masks
02332     MaskPolygonVector ImgMasks;
02333     CPVector loadedCp;
02334 
02335     // indicate lines that should be skipped for whatever reason
02336     bool skipNextLine = false;
02337 
02338     bool PTGUIScriptFile = false;
02339     int PTGUIScriptVersion = 0;
02340     // PTGui lens line detected
02341     int ctrlPointsImgNrOffset = 0;
02342     bool PTGUILensLine = false;
02343 
02344     bool PTGUILensLoaded = false;
02345     PTScriptParsing::ImgInfo PTGUILens;
02346 
02347     // set new options to some sensible default.
02348     options.reset();
02349     options.tiff_saveROI = false;
02350 
02351     ptoVersion = 1;
02352 
02353     bool firstOptVecParse = true;
02354     unsigned int lineNr = 0;    
02355     while (i.good()) {
02356         std::getline(i, line);
02357         lineNr++;
02358         DEBUG_DEBUG(lineNr << ": " << line);
02359         if (skipNextLine) {
02360             skipNextLine = false;
02361             continue;
02362         }
02363         //skip emtpy lines
02364         if(line.empty())
02365             continue;
02366         // check for a known line
02367         switch(line[0]) {
02368         case 'p':
02369         {
02370             DEBUG_DEBUG("p line: " << line);
02371             int i;
02372             if (PTScriptParsing::getIntParam(i, line, "f"))
02373                 options.setProjection( (PanoramaOptions::ProjectionFormat) i );
02374             unsigned int w;
02375             if (PTScriptParsing::getIntParam(w, line, "w"))
02376                 options.setWidth(w);
02377             double v;
02378             if (PTScriptParsing::getDoubleParam(v, line, "v"))
02379                 options.setHFOV(v, false);
02380             int height;
02381             if (PTScriptParsing::getIntParam(height, line, "h"))
02382                 options.setHeight(height);
02383 
02384             double newE;
02385             if (PTScriptParsing::getDoubleParam(newE, line, "E"))
02386                 options.outputExposureValue = newE;
02387             int ar=0;
02388             if (PTScriptParsing::getIntParam(ar, line, "R"))
02389                 options.outputMode = (PanoramaOptions::OutputMode) ar;
02390 
02391             std::string format;
02392             if (PTScriptParsing::getPTParam(format, line, "T"))
02393                 options.outputPixelType = format;
02394 
02395             if (PTScriptParsing::getPTParam(format, line, "S")) {
02396                 int left, right, top, bottom;
02397                 int n = sscanf(format.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
02398                 if (n == 4) {
02399                     options.setROI(vigra::Rect2D(left, top, right, bottom));
02400                 } else {
02401                     DEBUG_WARN("Could not parse crop string: " << format);
02402                 }
02403             }
02404 
02405             // parse projection parameters
02406             if (PTScriptParsing::getPTParam(format, line, "P")) {
02407                 char * tstr = strdup(format.c_str());
02408                 std::vector<double> projParam;
02409                 char * b = strtok(tstr, " \"");
02410                 if (b != NULL) {
02411                     while (b != NULL) {
02412                     double tempDbl;
02413                     if (sscanf(b, "%lf", &tempDbl) == 1) {
02414                         projParam.push_back(tempDbl);
02415                         b = strtok(NULL, " \"");
02416                     }
02417                     }
02418                 }
02419                 free(tstr);
02420                 // only set projection parameters, if the have the right size.
02421                 if (projParam.size() == options.getProjectionParameters().size()) {
02422                     options.setProjectionParameters(projParam);
02423                 }
02424             }
02425 
02426             // this is fragile.. hope nobody adds additional whitespace
02427             // and other arguments than q...
02428             // n"JPEG q80"
02429             if (PTScriptParsing::getPTParam(format, line, "n")) {
02430                 int t = format.find(' ');
02431                 options.outputFormat = options.getFormatFromName(format.substr(0,t));
02432 
02433                 // parse output format options.
02434                 switch (options.outputFormat)
02435                 {
02436                     case PanoramaOptions::JPEG:
02437                     case PanoramaOptions::JPEG_m:
02438                     {
02439                         // "parse" jpg quality
02440                         int q;
02441                         if (PTScriptParsing::getIntParam(q, format, "q")) {
02442                         options.quality = (int) q;
02443                         }
02444                     }
02445                     break;
02446                     case PanoramaOptions::TIFF_m:
02447                     {
02448                         int coordImgs = 0;
02449                         if (PTScriptParsing::getIntParam(coordImgs, format, "p"))
02450                         if (coordImgs)
02451                             options.saveCoordImgs = true;
02452                     }
02453                     case PanoramaOptions::TIFF:
02454                     case PanoramaOptions::TIFF_mask:
02455                     case PanoramaOptions::TIFF_multilayer:
02456                     case PanoramaOptions::TIFF_multilayer_mask:
02457                     {
02458                         // parse tiff compression mode
02459                         std::string comp;
02460                         if (PTScriptParsing::getPTParam(comp, format, "c:")) {
02461                             if (comp == "NONE" || comp == "LZW" ||
02462                                 comp == "PACKBITS" || comp == "DEFLATE")
02463                             {
02464                                 options.tiffCompression = comp;
02465                             } else {
02466                                 DEBUG_WARN("No valid tiff compression found");
02467                             }
02468                         }
02469                         // read tiff roi
02470                         if (PTScriptParsing::getPTParam(comp, format, "r:")) {
02471                             if (comp == "CROP") {
02472                                 options.tiff_saveROI = true;
02473                             } else {
02474                                 options.tiff_saveROI = false;
02475                             }
02476                         }
02477                     }
02478                 break;
02479                 default:
02480                 break;
02481                 }
02482             }
02483 
02484             int cRefImg = 0;
02485             if (PTScriptParsing::getIntParam(cRefImg, line, "k")) {
02486                 options.colorCorrection = PanoramaOptions::BRIGHTNESS_COLOR;
02487             } else if (PTScriptParsing::getIntParam(cRefImg, line,"b")) {
02488                 options.colorCorrection = PanoramaOptions::BRIGHTNESS;
02489             } else if (PTScriptParsing::getIntParam(cRefImg, line,"d")) {
02490                 options.colorCorrection = PanoramaOptions::COLOR;
02491             } else {
02492                 options.colorCorrection = PanoramaOptions::NONE;
02493             }
02494             options.colorReferenceImage=cRefImg;
02495             break;
02496 
02497         }
02498         case 'm':
02499         {
02500             DEBUG_DEBUG("m line: " << line);
02501             // parse misc options
02502             int i;
02503             if (PTScriptParsing::getIntParam(i, line, "i"))
02504                 options.interpolator = (vigra_ext::Interpolator) i;
02505             (void)PTScriptParsing::getDoubleParam(options.gamma, line, "g");
02506 
02507             if (PTScriptParsing::getIntParam(i, line, "f")) {
02508                 switch(i) {
02509                 case 0:
02510                     options.remapAcceleration = PanoramaOptions::MAX_SPEEDUP;
02511                     break;
02512                 case 1:
02513                     options.remapAcceleration = PanoramaOptions::MEDIUM_SPEEDUP;
02514                     break;
02515                 default:
02516                     options.remapAcceleration = PanoramaOptions::NO_SPEEDUP;
02517                     break;
02518                 }
02519             } else {
02520                 options.remapAcceleration = PanoramaOptions::NO_SPEEDUP;
02521             }
02522 
02523             break;
02524         }
02525         case 'v':
02526         {
02527             DEBUG_DEBUG("v line: " << line);
02528             if (!PTGUIScriptFile) {
02529                 if (firstOptVecParse) {
02530                     int nImg = std::max(iImgInfo.size(), oImgInfo.size());
02531                     DEBUG_DEBUG("nImg: " << nImg);
02532                     optvec = OptimizeVector(nImg);
02533                     firstOptVecParse = false;
02534                 }
02535                 std::stringstream optstream;
02536                 optstream << line.substr(1);
02537                 std::string var;
02538                 while (!(optstream >> std::ws).eof()) {
02539                     optstream >> var;
02540                     if (var.length() == 1) {
02541                         // special case for PTGUI
02542                         var += "0";
02543                     }
02544                     // find first numerical character
02545                     std::string::size_type np = var.find_first_of("0123456789");
02546                     if (np == std::string::npos) {
02547                         // invalid, continue
02548                         continue;
02549                     }
02550                     std::string name=var.substr(0,np);
02551                     unsigned int nr;
02552                     if (!hugin_utils::stringToUInt(var.substr(np), nr))
02553                     {
02554                         // invalid, continue
02555                         continue;
02556                     };
02557                     DEBUG_ASSERT(nr < optvec.size());
02558                     if(nr < optvec.size())
02559                     {
02560                         optvec[nr].insert(name);
02561                         DEBUG_DEBUG("parsing opt: >" << var << "< : var:" << name << " image:" << nr);
02562                     };
02563                 }
02564             }
02565             break;
02566         }
02567         case 'c':
02568         {
02569             DEBUG_DEBUG("c line: " << line);
02570             int t;
02571             // read control points
02572             ControlPoint point;
02573             // TODO - should verify that line syntax is correct
02574             PTScriptParsing::getIntParam(point.image1Nr, line, "n");
02575             point.image1Nr += ctrlPointsImgNrOffset;
02576             PTScriptParsing::getIntParam(point.image2Nr, line, "N");
02577             point.image2Nr += ctrlPointsImgNrOffset;
02578             PTScriptParsing::getDoubleParam(point.x1, line, "x");
02579             PTScriptParsing::getDoubleParam(point.x2, line, "X");
02580             PTScriptParsing::getDoubleParam(point.y1, line, "y");
02581             PTScriptParsing::getDoubleParam(point.y2, line, "Y");
02582             if (!PTScriptParsing::getIntParam(t, line, "t")){
02583                 t = 0;
02584             }
02585 
02586             point.mode = t;
02587             loadedCp.push_back(point);
02588             break;
02589         }
02590 
02591         // handle the complicated part.. the image & lens settings.
02592         // treat i and o lines the same.. however, o lines have priority
02593         // over i lines.(i lines often do not contain link information!)
02594         case 'i':
02595         {
02596             if (PTGUILensLine) {
02597                 PTGUILensLine = false;
02598                 PTGUILensLoaded = true;
02599                 PTGUILens.parse(line);
02600             } else {
02601                 iImgInfo.push_back(PTScriptParsing::ImgInfo(line));
02602             }
02603             break;
02604         }
02605         case 'o':
02606         {
02607             if (PTGUILensLine) {
02608                 PTGUILensLine = false;
02609                 PTGUILensLoaded = true;
02610                 PTGUILens.parse(line);
02611             } else {
02612                 oImgInfo.push_back(PTScriptParsing::ImgInfo(line));
02613             }
02614             break;
02615         }
02616 
02617         case 'k':
02618         {
02619             unsigned int param;
02620             if (PTScriptParsing::getIntParam(param, line, "i"))
02621             {
02622                 MaskPolygon newPolygon;
02623                 newPolygon.setImgNr(param);
02624                 if (PTScriptParsing::getIntParam(param, line, "t"))
02625                     newPolygon.setMaskType((HuginBase::MaskPolygon::MaskType)param);
02626                 std::string format;
02627                 if (PTScriptParsing::getPTParam(format, line, "p"))
02628                 {
02629                     if(newPolygon.parsePolygonString(format))
02630                         ImgMasks.push_back(newPolygon);
02631                 };
02632             };
02633             break;
02634         }
02635 
02636         case '#':
02637         {
02638             // parse special comments...
02639             if (line.substr(0,20) == std::string("# ptGui project file")) {
02640                 PTGUIScriptFile = true;
02641             }
02642             if (line.substr(0,12) == "#-dummyimage") {
02643                 PTGUILensLine = true;
02644             }
02645             if (PTGUIScriptFile) {
02646                 // parse special PTGUI stuff.
02647                 if (sscanf(line.c_str(), "#-fileversion %d", &PTGUIScriptVersion) > 0) {
02648                     DEBUG_DEBUG("Detected PTGUI script version: " << PTGUIScriptVersion);
02649                     switch (PTGUIScriptVersion) {
02650                         case 0:
02651                             break;
02652                         case 1:
02653                             break;
02654                         case 2:
02655                             break;
02656                         case 3:
02657                             break;
02658                         case 4:
02659                             break;
02660                         case 5:
02661                             break;
02662                         case 6:
02663                             break;
02664                         case 7:
02665                             break;
02666                         default:
02667                             ctrlPointsImgNrOffset = -1;
02668                             // latest known version is 8
02669                             break;
02670                     }
02671                 }
02672             }
02673 
02674             if (line.substr(0,8) == "#-hugin ") {
02675                 // read hugin image line
02676                 PTScriptParsing::ImgInfo info;
02677                 info.autoCenterCrop = (line.find("autoCenterCrop=1") != std::string::npos);
02678                 size_t pos = line.find("cropFactor=");
02679                 if (pos > 0 && pos < line.length()) {
02680                     double cropFactor=1;
02681                     const char * s = line.c_str() + pos;
02682                     sscanf(s,"cropFactor=%lf", & cropFactor);
02683                     if(cropFactor<0.01 || cropFactor > 100)
02684                         cropFactor=1;
02685                     info.cropFactor = cropFactor;
02686                 }
02687                 pos = line.find("disabled");
02688                 if (pos > 0 && pos < line.length()) {
02689                     info.enabled = false;
02690                 }
02691                 huginImgInfo.push_back(info);
02692             }
02693 
02694             // PTGui and PTAssember project files:
02695             // #-imgfile 960 1280 "D:\data\bruno\074-098\087.jpg"
02696             if (line.substr(0,10) == "#-imgfile ") {
02697 
02698                 // arghhh. I like string processing without regexps.
02699                 int b = line.find_first_not_of(" ",9);
02700                 int e = line.find_first_of(" ",b);
02701                 DEBUG_DEBUG(" width:" << line.substr(b, e - b) << ":")
02702                 int nextWidth;
02703                 if (!hugin_utils::stringToInt(line.substr(b, e - b), nextWidth))
02704                 {
02705                     continue;
02706                 };
02707                 DEBUG_DEBUG("next width " << nextWidth);
02708                 b = line.find_first_not_of(" ",e);
02709                 e = line.find_first_of(" ",b);
02710                 DEBUG_DEBUG(" height:" << line.substr(b, e - b) << ":")
02711                 int nextHeight;
02712                 if (!hugin_utils::stringToInt(line.substr(b, e - b), nextHeight))
02713                 {
02714                     continue;
02715                 };
02716                 DEBUG_DEBUG("next height " << nextHeight);
02717 
02718                 std::string nextFilename;
02719                 try {
02720                     b = line.find_first_not_of(" \"",e);
02721                     e = line.find_first_of("\"",b);
02722                     nextFilename = line.substr(b,e-b);
02723                 } catch (std::out_of_range& e) {
02724                     DEBUG_ERROR("ERROR PARSING INPUT FILE" << e.what( ));
02725                     return false;
02726                 }
02727                 DEBUG_DEBUG("next filename " << nextFilename);
02728 
02729                 PTScriptParsing::ImgInfo info;
02730                 info.width  = nextWidth;
02731                 info.height = nextHeight;
02732                 info.filename = nextFilename;
02733                 cImgInfo.push_back(info);
02734             }
02735 
02736 
02737             // parse hugin properties
02738             if (line.substr(0,7) == "#hugin_") {
02739                 std::istringstream is(line);
02740                 std::string var,value;
02741                 is >> var >> value;
02742                 if (!is.fail()) {
02743                     if (var == "#hugin_ptoversion") {
02744                         ptoVersion = atoi(value.c_str());
02745                     }
02746 
02747                     if (var == "#hugin_optimizeReferenceImage") {
02748                         options.optimizeReferenceImage = atoi(value.c_str());
02749                     } else if (var == "#hugin_remapper") {
02750                         if (value == "nona") {
02751                             options.remapper = PanoramaOptions::NONA;
02752                         } else if (value == "PTmender") {
02753                             options.remapper = PanoramaOptions::PTMENDER;
02754                         }
02755                     } else if (var == "#hugin_blender") {
02756                         if (value == "none") {
02757                             options.blendMode = PanoramaOptions::NO_BLEND;
02758                         } else if (value == "PTblender") {
02759                             options.blendMode = PanoramaOptions::PTBLENDER_BLEND;
02760                         } else if (value == "enblend") {
02761                             options.blendMode = PanoramaOptions::ENBLEND_BLEND;
02762                         } else if (value == "PTmasker") {
02763                             options.blendMode = PanoramaOptions::PTMASKER_BLEND;
02764                         } else if (value == "smartblend") {
02765                             options.blendMode = PanoramaOptions::SMARTBLEND_BLEND;
02766                         } else if (value == "internal") {
02767                             options.blendMode = PanoramaOptions::INTERNAL_BLEND;
02768                         }
02769 
02770                     } else if (var == "#hugin_enblendOptions") {
02771                         options.enblendOptions = value;
02772                         while (!is.eof()) {
02773                             is >> value;
02774                             if (value.length() > 0) {
02775                                 options.enblendOptions += " ";
02776                                 options.enblendOptions += value;
02777                             }
02778                         }
02779                     } else if (var == "#hugin_enfuseOptions") {
02780                         options.enfuseOptions = value;
02781                         while (!is.eof()) {
02782                             is >> value;
02783                             if (value.length() > 0) {
02784                                 options.enfuseOptions += " ";
02785                                 options.enfuseOptions += value;
02786                             }
02787                         }
02788                     } else if (var == "#hugin_hdrmergeOptions") {
02789                         options.hdrmergeOptions = value;
02790                         while (!is.eof()) {
02791                             is >> value;
02792                             if (value.length() > 0) {
02793                                 options.hdrmergeOptions += " ";
02794                                 options.hdrmergeOptions += value;
02795                             }
02796                         }
02797                     } else if (var == "#hugin_verdandiOptions") {
02798                         options.verdandiOptions = value;
02799                         while (!is.eof()) {
02800                             is >> value;
02801                             if (value.length() > 0) {
02802                                 options.verdandiOptions += " ";
02803                                 options.verdandiOptions += value;
02804                             }
02805                         }
02806 
02807                     } else if (var == "#hugin_outputLDRBlended") {
02808                         options.outputLDRBlended = (value == "true");
02809                     } else if (var == "#hugin_outputLDRLayers") {
02810                         options.outputLDRLayers = (value == "true");
02811                     } else if (var == "#hugin_outputLDRExposureRemapped") {
02812                         options.outputLDRExposureRemapped = (value == "true");
02813                     } else if (var == "#hugin_outputLDRExposureLayers") {
02814                         options.outputLDRExposureLayers = (value == "true");
02815                     } else if (var == "#hugin_outputLDRExposureBlended") {
02816                         options.outputLDRExposureBlended = (value == "true");
02817                     } else if (var == "#hugin_outputLDRExposureLayersFused") {
02818                         options.outputLDRExposureLayersFused = (value == "true");
02819                     } else if (var == "#hugin_outputLDRStacks") {
02820                         options.outputLDRStacks = (value == "true");
02821                     } else if (var == "#hugin_outputHDRBlended") {
02822                         options.outputHDRBlended = (value == "true");
02823                     } else if (var == "#hugin_outputHDRLayers") {
02824                         options.outputHDRLayers = (value == "true");
02825                     } else if (var == "#hugin_outputHDRStacks") {
02826                         options.outputHDRStacks = (value == "true");
02827 
02828                     } else if (var == "#hugin_outputStacksMinOverlap") {
02829                         double val=atof(value.c_str());
02830                         if(val>0 && val <= 1)
02831                         {
02832                             options.outputStacksMinOverlap = val;
02833                         };
02834                         if (val < 0)
02835                         {
02836                             options.outputStacksMinOverlap = -1;
02837                         };
02838                     } else if (var == "#hugin_outputLayersExposureDiff") {
02839                         double val=atof(value.c_str());
02840                         if(val>0.01)
02841                         {
02842                             options.outputLayersExposureDiff = val;
02843                         }
02844 
02845                     } else if (var == "#hugin_outputLayersCompression") {
02846                         options.outputLayersCompression = value;
02847                     } else if (var == "#hugin_outputImageType") {
02848                         options.outputImageType = value;
02849                     } else if (var == "#hugin_outputImageTypeCompression") {
02850                         options.outputImageTypeCompression = value;
02851                     } else if (var == "#hugin_outputJPEGQuality") {
02852                         options.quality = atoi(value.c_str());
02853                     } else if (var == "#hugin_outputImageTypeHDR") {
02854                         options.outputImageTypeHDR = value;
02855                     } else if (var == "#hugin_outputImageTypeHDRCompression") {
02856                         options.outputImageTypeHDRCompression = value;
02857                     } else if (var == "#hugin_optimizerMasterSwitch") {
02858                         optSwitch = atoi(value.c_str());
02859                     } else if (var == "#hugin_optimizerPhotoMasterSwitch") {
02860                         optPhotoSwitch = atoi(value.c_str());
02861                     };
02862 
02863                 }
02864             }
02865             break;
02866         }
02867 
02868         } // case
02869     }
02870 
02871     // assemble images from the information read before..
02872 
02875 #if 0
02876     // handle PTGUI special case
02877     if (PTGUILensLoaded) {
02878         // create lens with dummy info
02879         Lens l;
02880         for (const char **v = Lens::variableNames; *v != 0; v++) {
02881             map_get(l.variables, *v).setValue(PTGUILens.vars[*v]);
02882         }
02883         l.setImageSize(vigra::Size2D(PTGUILens.width, PTGUILens.height));
02884         l.setCropFactor(1);
02885         l.setProjection((Lens::LensProjectionFormat) PTGUILens.f);
02886         lenses.push_back(l);
02887     }
02888 #endif
02889 
02890 /*
02891     // ugly hack to load PTGui script files
02892     if (ptGUIDummyImage) {
02893         DEBUG_DEBUG("loading default PTGUI line: " << line);
02894             Lens l;
02895             // skip ptgui's dummy image
02896             // load parameters into default lens...
02897             for (LensVarMap::iterator it = l.variables.begin();
02898              it != l.variables.end();
02899              ++it)
02900             {
02901                 DEBUG_DEBUG("reading default lens variable " << it->first);
02902                 int link;
02903                 bool ok = readVar(it->second, link, line);
02904                 DEBUG_ASSERT(ok);
02905                 DEBUG_ASSERT(link == -1);
02906             }
02907             lenses.push_back(l);
02908 
02909             ptGUIDummyImage = false;
02910             break;
02911         }
02912 */
02913 
02914     // merge image info from the 3 different lines...
02915     // i lines are the main reference.
02916 
02917     int nImgs = iImgInfo.size();
02918     int nOLines = oImgInfo.size();
02919     int nCLines = cImgInfo.size();
02920 
02921     if (nImgs < nOLines) {
02922         // no, or less i lines found. scrap i lines.
02923         DEBUG_DEBUG("throwing away " << nImgs << " i lines");
02924         iImgInfo = oImgInfo;
02925         nImgs = nOLines;
02926     }
02927     if (nOLines < nImgs) {
02928         oImgInfo = iImgInfo;
02929     }
02930 
02931     // merge o lines and i lines into i lines.
02932     for (int i=0; i < nImgs; i++) {
02933 
02934         // move parameters from o lines -> i (only if it isn't given in the
02935         // i lines. or it is linked on the o lines)
02936 
02937         // ordinary variables
02938         for (const char ** v = PTScriptParsing::ImgInfo::varnames; *v; v++) {
02939 
02940             if ((iImgInfo[i].links[*v] == -2 && oImgInfo[i].links[*v] != -2) || (iImgInfo[i].links[*v] == -1 && oImgInfo[i].links[*v] >=0)) {
02941                 DEBUG_DEBUG(*v << ": o -> i");
02942                 iImgInfo[i].vars[*v] = oImgInfo[i].vars[*v];
02943                 iImgInfo[i].links[*v] = oImgInfo[i].links[*v];
02944             }
02945         }
02946 
02947         if (iImgInfo[i].filename == "" && oImgInfo[i].filename != "") {
02948             DEBUG_DEBUG("filename: o -> i");
02949             iImgInfo[i].filename = oImgInfo[i].filename;
02950         }
02951 
02952         if (iImgInfo[i].crop.isEmpty() && !oImgInfo[i].crop.isEmpty()) {
02953             DEBUG_DEBUG("crop: o -> i");
02954             iImgInfo[i].crop = oImgInfo[i].crop;
02955         }
02956 
02957         if (iImgInfo[i].width <= 0 && oImgInfo[i].width > 0) {
02958             DEBUG_DEBUG("width: o -> i");
02959             iImgInfo[i].width = oImgInfo[i].width;
02960         }
02961 
02962         if (iImgInfo[i].height <= 0 && oImgInfo[i].height > 0) {
02963             DEBUG_DEBUG("height: o -> i");
02964             iImgInfo[i].height = oImgInfo[i].height;
02965         }
02966 
02967         if (iImgInfo[i].f < 0 && oImgInfo[i].f > 0) {
02968             DEBUG_DEBUG("f: o -> i");
02969             iImgInfo[i].f = oImgInfo[i].f;
02970         }
02971 
02972         if (nCLines == nImgs) {
02973             // img file & size in clines
02974             if (cImgInfo[i].filename != "" && cImgInfo[i].width > 0) {
02975                 DEBUG_DEBUG("filename, width, height: c -> i");
02976                 iImgInfo[i].filename = cImgInfo[i].filename;
02977                 iImgInfo[i].width = cImgInfo[i].width;
02978                 iImgInfo[i].height = cImgInfo[i].height;
02979             }
02980         }
02981         if (huginImgInfo.size() == (size_t)nImgs) {
02982             iImgInfo[i].cropFactor = huginImgInfo[i].cropFactor;
02983             iImgInfo[i].autoCenterCrop = huginImgInfo[i].autoCenterCrop;
02984             iImgInfo[i].enabled= huginImgInfo[i].enabled;
02985         }
02986     }
02987 
02988     // create images.
02989     for (int i=0; i < nImgs; i++) {
02990 
02991         DEBUG_DEBUG("i line: " << i);
02992         // read the variables
02993         VariableMap vars;
02994         int link = -2;
02995         fillVariableMap(vars);
02996 
02997         for (const char ** v = PTScriptParsing::ImgInfo::varnames; *v != 0; v++) {
02998             std::string name(*v);
02999             double val = iImgInfo[i].vars[*v];
03000             map_get(vars,name).setValue(val);
03001             if (iImgInfo[i].links[*v] >= 0) {
03002                 link = iImgInfo[i].links[*v];
03003             }
03004         }
03005         
03006         std::string file = iImgInfo[i].filename;
03007         // add prefix if only a relative path.
03008 #ifdef _WIN32
03009         bool absPath = ( (file[1]==':' && file[2]=='\\') || (file[1]==':' && file[2]=='/') || (file[0] == '\\' && file[1] == '\\'));
03010 #else
03011         bool absPath = file[0] == '/';
03012 #endif
03013         if (!absPath) {
03014             file.insert(0, prefix);
03015         }
03016         DEBUG_DEBUG("filename: " << file);
03017         
03018         // Make a new SrcPanoImage in this list for expressing this one.
03019         SrcPanoImage * new_img_p = new SrcPanoImage();
03020         images.push_back(new_img_p);
03021         // and make a reference to it so we dont keep using images.back(),
03022         SrcPanoImage & new_img = *new_img_p;
03023         new_img.setFilename(file);
03024         new_img.setSize(vigra::Size2D(iImgInfo[i].width, iImgInfo[i].height));
03025         new_img.checkImageSizeKnown();
03026         
03027         // Panotools Script variables for the current SrcPanoImage variable.
03028         // We just need the names.
03029         VariableMap vars_for_name;
03030         
03031         // A dummy SrcPanoImage to use to fill vars_for_name.
03032         SrcPanoImage name_src;
03033         
03034 /* Set image variables in new_img using the PanoTools Script names, with
03035  * linking where specified in the file.
03036  * 
03037  * We create a list of PanoTools script variable names for each variable in
03038  * SrcPanoImg (vars_for_name). It may have multiple variables or none at all.
03039  * If a link is found between any of the variables inside it we can link them up
03040  * in new_img.
03041  * 
03042  * The Panorama Tools script format makes it possible to link parts of the same
03043  * SrcPanoImage variable differently (e.g. you can link the horizontal component
03044  * of shearing to a different target to the vertical component, not link the
03045  * vertical component at all). SrcPanoImage cannot handle this, so we end up
03046  * linking all components of the same variable to anything specified in the PTO
03047  * script for one of the components: most often we specify the same link
03048  * multiple times.
03049  */
03053 #define RESET_LOCALE setlocale(LC_NUMERIC,old_locale); free(old_locale);
03054 #define image_variable( name, type, default_value )\
03055         PTOVariableConverterFor##name::addToVariableMap(name_src.get##name##IV(), vars_for_name);\
03056         for (VariableMap::iterator vit = vars_for_name.begin();\
03057              vit != vars_for_name.end(); vit++)\
03058         {\
03059             if (link >= 0 && iImgInfo[i].links[vit->first] >= 0)\
03060             {\
03061                 if (   !(PTGUILensLoaded && link == 0)\
03062                     && (int) images.size() < link && (!PTGUILensLoaded))\
03063                 {\
03064                     DEBUG_ERROR("variables must be linked to an image with a lower number" << endl\
03065                                 << "number links: " << link << " images: " << images.size() << endl\
03066                                 << "error on line " << lineNr << ":" << endl\
03067                                 << line);\
03068                     RESET_LOCALE\
03069                     return false;\
03070                 }\
03071                 DEBUG_DEBUG("anchored to image " << iImgInfo[i].links[vit->first]);\
03072                 new_img.link##name(images[iImgInfo[i].links[vit->first]]);\
03073             } else {\
03074                 double val = map_get(vars, vit->first).getValue();\
03075                 new_img.setVar(vit->first, val);\
03076             }\
03077         }\
03078         vars_for_name.clear();
03079 #include "image_variables.h"
03080 #undef image_variable
03081         new_img.setProjection((SrcPanoImage::Projection) iImgInfo[i].f);
03082 
03083         // check, if stacks are correctly linked
03084 #define check_stack_link(name) \
03085         if(!new_img.YawisLinked() && new_img.name##isLinked())\
03086         {\
03087             new_img.unlink##name();\
03088         };\
03089         if(new_img.YawisLinked() && !new_img.name##isLinked())\
03090         {\
03091             for(size_t j=0; j<i; j++)\
03092             {\
03093                 if(new_img.YawisLinkedWith(*images[j]))\
03094                 {\
03095                     new_img.link##name(images[j]);\
03096                     break;\
03097                 };\
03098             };\
03099         }
03100         check_stack_link(Pitch);
03101         check_stack_link(Roll);
03102         check_stack_link(X);
03103         check_stack_link(Y);
03104         check_stack_link(Z);
03105         check_stack_link(TranslationPlaneYaw);
03106         check_stack_link(TranslationPlanePitch);
03107 #undef check_stack_link
03108 
03109 #if 0
03110         new_img.setFeatherWidth((unsigned int) iImgInfo[i].blend_radius);
03111 #endif
03112         
03113         // is this right?
03114         new_img.setCropFactor(iImgInfo[i].cropFactor);
03115         new_img.setVigCorrMode(iImgInfo[i].vigcorrMode);
03116         new_img.setFlatfieldFilename(iImgInfo[i].flatfieldname);
03117         new_img.setResponseType((SrcPanoImage::ResponseType)iImgInfo[i].responseType);
03118         new_img.setAutoCenterCrop(iImgInfo[i].autoCenterCrop);
03119         new_img.setActive(iImgInfo[i].enabled);
03120         
03121         if (!iImgInfo[i].crop.isEmpty()) {
03122             if (new_img.isCircularCrop())
03123             {
03124                 new_img.setCropMode(SrcPanoImage::CROP_CIRCLE);
03125             } else {
03126                 new_img.setCropMode(SrcPanoImage::CROP_RECTANGLE);
03127             }
03128             new_img.setCropRect(iImgInfo[i].crop);
03129         }
03130 
03131         //now fill the mask
03132         for(unsigned int j=0;j<ImgMasks.size();j++)
03133             if(ImgMasks[j].getImgNr()==i)
03134                 //now clip mask to image size + offset
03135                 if(ImgMasks[j].clipPolygon(vigra::Rect2D(-0.9*maskOffset,-0.9*maskOffset,
03136                     new_img.getWidth()+0.9*maskOffset,new_img.getHeight()+0.9*maskOffset)))
03137                 {
03138                     new_img.addMask(ImgMasks[j]);
03139                 };
03140     }
03141     
03142     // if we haven't found a v line in the project file
03143     if (optvec.size() != images.size()) {
03144         optvec = OptimizeVector(images.size());
03145     }
03146 
03147     if (!loadedCp.empty())
03148     {
03149         // check if control points are linked with existing images
03150         const size_t nrImg = images.size();
03151         for (CPVector::const_iterator it = loadedCp.begin(); it != loadedCp.end(); ++it)
03152         {
03153             HuginBase::ControlPoint cp = *it;
03154             if (cp.image1Nr < nrImg && cp.image2Nr < nrImg)
03155             {
03156                 ctrlPoints.push_back(cp);
03157             };
03158         };
03159         if (loadedCp.size() != ctrlPoints.size())
03160         {
03161             std::cout << "WARNING: Project file contains control points that are connected with" << std::endl
03162                 << "  non existing images. Ignoring these control points." << std::endl;
03163         };
03164     };
03165 
03166     // reset locale
03167     setlocale(LC_NUMERIC,old_locale);
03168     free(old_locale);
03169 
03170     return true;
03171 }
03172 
03173 } // namespace

Generated on 23 May 2016 for Hugintrunk by  doxygen 1.4.7