hugin_lensdb.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00012 /*  This program is free software; you can redistribute it and/or
00013  *  modify it under the terms of the GNU General Public
00014  *  License as published by the Free Software Foundation; either
00015  *  version 2 of the License, or (at your option) any later version.
00016  *
00017  *  This software is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  *  General Public License for more details.
00021  *
00022  *  You should have received a copy of the GNU General Public
00023  *  License along with this software. If not, see
00024  *  <http://www.gnu.org/licenses/>.
00025  *
00026  */
00027 
00028 #include <iostream>
00029 #include <string>
00030 #include <fstream>
00031 #include "hugin_config.h"
00032 #ifdef HAVE_STD_FILESYSTEM
00033 #include <filesystem>
00034 namespace fs = std::tr2::sys;
00035 #else
00036 #define BOOST_FILESYSTEM_VERSION 3
00037 #include <boost/filesystem.hpp>
00038 namespace fs = boost::filesystem;
00039 #endif
00040 #include <getopt.h>
00041 #include <panodata/Panorama.h>
00042 #include <hugin_utils/stl_utils.h>
00043 #include <lensdb/LensDB.h>
00044 #include <panodata/StandardImageVariableGroups.h>
00045 #include <hugin_base/panotools/PanoToolsUtils.h>
00046 
00047 typedef std::vector<fs::path> pathVec;
00048 
00049 
00050 template <class iteratorType>
00051 bool iterateFileSystem(std::string src, pathVec& projectFiles)
00052 {
00053     try
00054     {
00055         for(iteratorType it(src); it != iteratorType(); it++)
00056         {
00057             std::string ext=hugin_utils::toupper(it->path().extension().string());
00058             if(ext==".PTO")
00059             {
00060                 projectFiles.push_back(*it);
00061             };
00062         }
00063     }
00064     catch(fs::filesystem_error& e)
00065     {
00066         std::cout << e.what() << std::endl;
00067         return false;
00068     }
00069     return true;
00070 };
00071 
00072 void FindPTOFiles(pathVec& projectFiles, std::string src, bool recursive)
00073 {
00074     if(recursive)
00075     {
00076         iterateFileSystem<fs::recursive_directory_iterator>(src, projectFiles);
00077     }
00078     else
00079     {
00080         iterateFileSystem<fs::directory_iterator>(src, projectFiles);
00081     };
00082 };
00083 
00084 bool CheckProjectFile(const fs::path filename)
00085 {
00086     // open project file
00087     HuginBase::Panorama pano;
00088     std::string input = filename.string();
00089     std::ifstream prjfile(input.c_str());
00090     if (!prjfile.good())
00091     {
00092         std::cerr << "ERROR: Could not open script: " << filename.string() << endl;
00093         return false;
00094     }
00095     std::string inputPathPrefix = hugin_utils::getPathPrefix(input);
00096     pano.setFilePrefix(inputPathPrefix);
00097     AppBase::DocumentData::ReadWriteError err = pano.readData(prjfile);
00098     if (err != AppBase::DocumentData::SUCCESSFUL)
00099     {
00100         std::cerr << "ERROR: error while parsing panos tool script: " << input << std::endl
00101                   << "DocumentData::ReadWriteError code: " << err << std::endl;
00102         return false;
00103     };
00104     prjfile.close();
00105     if (pano.getNrOfImages() == 0)
00106     {
00107         return false;
00108     };
00109     std::cout << "Checking " << filename.string();
00110     HuginBase::StandardImageVariableGroups lenses(pano);
00111     if (lenses.getLenses().getNumberOfParts()==1)
00112     {
00113         // read the EXIF data
00114         for (size_t i = 0; i < pano.getNrOfImages(); ++i)
00115         {
00116             HuginBase::SrcPanoImage img = pano.getSrcImage(i);
00117             if (!img.readEXIF())
00118             {
00119                 std::cout << " Ignored (File missing or missing metadata)" << std::endl;
00120                 return false;
00121             }
00122             pano.setSrcImage(i, img);
00123         };
00124         // update cp errors
00125         HuginBase::PTools::calcCtrlPointErrors(pano);
00126         // now save in database
00127         if (HuginBase::LensDB::SaveLensDataFromPano(pano))
00128         {
00129             std::cout << " Saved." << std::endl;
00130             return true;
00131         }
00132         else
00133         {
00134             std::cout << " Ignored." << std::endl;
00135             return false;
00136         }
00137     };
00138     std::cout << " Ignored (More than one lens)." << std::endl;
00139     return false;
00140 };
00141 
00142 static void usage(const char* name)
00143 {
00144     std::cout << name << ": tool for lens database maintenance" << std::endl
00145               << name << " version " << hugin_utils::GetHuginVersion() << std::endl
00146               << std::endl
00147               << "Usage:  hugin_lensdb [--recursive] --populate BASEPATH " << std::endl
00148               << "             Populate database with information from all pto files" << std::endl
00149               << "             in given BASEPATH" << std::endl
00150               << "             With --recursive switch all subfolders will also be" << std::endl
00151               << "             searched." << std::endl
00152               << "        hugin_lensdb --compress" << std::endl
00153               << "             Compresses the database by replacing single values" << std::endl
00154               << "             with averaged values." << std::endl
00155               << "        hugin_lensdb --remove-lens=LENS" << std::endl
00156               << "             Removes given lens from the database." << std::endl
00157               << "        hugin_lensdb --remove-camera=MAKER|MODEL" << std::endl
00158               << "             Removes given camera from the database." << std::endl
00159               << "        hugin_lensdb --export-database=FILENAME" << std::endl
00160               << "             Export data from database to external file." << std::endl
00161               << "        hugin_lensdb --import-from-file=FILENAME" << std::endl
00162               << "             Import data from external file." << std::endl
00163               << std::endl;
00164 };
00165 
00166 int main(int argc, char* argv[])
00167 {
00168     // parse arguments
00169     const char* optstring = "crph";
00170     enum
00171     {
00172         REMOVE_LENS=1000,
00173         REMOVE_CAM=1001,
00174         EXPORT_DB=1002,
00175         IMPORT_DB=1003,
00176     };
00177 
00178     static struct option longOptions[] =
00179     {
00180         { "compress", no_argument, NULL, 'c' },
00181         { "recursive", no_argument, NULL, 'r' },
00182         { "populate", required_argument, NULL, 'p' },
00183         { "remove-lens", required_argument, NULL, REMOVE_LENS },
00184         { "remove-camera", required_argument, NULL, REMOVE_CAM },
00185         { "export-database", required_argument, NULL, EXPORT_DB },
00186         { "import-from-file", required_argument, NULL, IMPORT_DB },
00187         { "help", no_argument, NULL, 'h' },
00188         0
00189     };
00190 
00191     bool recursive=false;
00192     bool populate = false;
00193     bool compress = false;
00194     std::string basepath;
00195     std::string lensToRemove;
00196     std::string camToRemove;
00197     std::string exportDatabase;
00198     std::string importDatabase;
00199     int c;
00200     while ((c = getopt_long (argc, argv, optstring, longOptions,nullptr)) != -1)
00201     {
00202         switch (c)
00203         {
00204             case 'h':
00205                 usage(hugin_utils::stripPath(argv[0]).c_str());
00206                 return 0;
00207             case 'c':
00208                 compress=true;
00209                 break;
00210             case 'r':
00211                 recursive=true;
00212                 break;
00213             case 'p':
00214                 populate = true;
00215                 basepath = hugin_utils::StrTrim(std::string(optarg));
00216                 break;
00217             case REMOVE_LENS:
00218                 lensToRemove = hugin_utils::StrTrim(std::string(optarg));
00219                 break;
00220             case REMOVE_CAM:
00221                 camToRemove = hugin_utils::StrTrim(std::string(optarg));
00222                 break;
00223             case EXPORT_DB:
00224                 exportDatabase = hugin_utils::StrTrim(std::string(optarg));
00225                 break;
00226             case IMPORT_DB:
00227                 importDatabase = hugin_utils::StrTrim(std::string(optarg));
00228                 break;
00229             case ':':
00230             case '?':
00231                 // missing argument or invalid switch
00232                 return 1;
00233                 break;
00234             default:
00235                 // this should not happen
00236                 abort();
00237         }
00238     }
00239 
00240     if (!exportDatabase.empty() && !importDatabase.empty())
00241     {
00242         std::cerr << hugin_utils::stripPath(argv[0]) << ": Export and import can not be done at the same time. " << std::endl;
00243         return -1;
00244     };
00245 
00246     if (!populate && !compress && lensToRemove.empty() && camToRemove.empty() && exportDatabase.empty() && importDatabase.empty())
00247     {
00248         std::cout << "Lensdatabase file: " << HuginBase::LensDB::LensDB::GetSingleton().GetDBFilename() << std::endl;
00249         std::cout << "Nothing to do." << std::endl;
00250     };
00251 
00252     if (populate)
00253     {
00254         fs::path p(basepath);
00255         if (fs::exists(p))
00256         {
00257             p = fs::absolute(p);
00258             if (fs::is_directory(p))
00259             {
00260                 pathVec projectFiles;
00261                 FindPTOFiles(projectFiles, p.string(), recursive);
00262                 if (projectFiles.empty())
00263                 {
00264                     std::cerr << "ERROR: No project files found in given directory " << p.string() << std::endl;
00265                     return 1;
00266                 };
00267                 for (pathVec::const_iterator it = projectFiles.begin(); it != projectFiles.end(); ++it)
00268                 {
00269                     CheckProjectFile(*it);
00270                 };
00271             }
00272             else
00273             {
00274                 std::cerr << "ERROR: " << basepath << " is not a directory." << std::endl;
00275                 return 1;
00276             };
00277         }
00278         else
00279         {
00280             std::cerr << "ERROR: Path " << basepath << " does not exists." << std::endl;
00281             return 1;
00282         }
00283     };
00284 
00285     if (compress)
00286     {
00287         std::cout << "Compressing database..." << std::endl;
00288         if(HuginBase::LensDB::LensDB::GetSingleton().CleanUpDatabase())
00289         {
00290             std::cout << "Successful." << std::endl;
00291         }
00292         else
00293         {
00294             std::cout << "FAILED." << std::endl;
00295         }
00296     };
00297     // remove lens
00298     if (!lensToRemove.empty())
00299     {
00300         std::cout << "Removing lens \"" << lensToRemove << "\"..." << std::endl;
00301         if (HuginBase::LensDB::LensDB::GetSingleton().RemoveLens(lensToRemove))
00302         {
00303             std::cout << "Successful." << std::endl;
00304         }
00305         else
00306         {
00307             std::cout << "FAILED." << std::endl;
00308         }
00309     };
00310     // remove camera
00311     if (!camToRemove.empty())
00312     {
00313         std::vector<std::string> input = hugin_utils::SplitString(camToRemove, "|");
00314         if (input.size() == 2)
00315         {
00316             std::cout << "Removing camera \"" << input[1] << "\" (Maker: \"" << input[0] << "\")..." << std::endl;
00317             if (HuginBase::LensDB::LensDB::GetSingleton().RemoveCamera(input[0], input[1]))
00318             {
00319                 std::cout << "Successful." << std::endl;
00320             }
00321             else
00322             {
00323                 std::cout << "FAILED." << std::endl;
00324             }
00325         }
00326         else
00327         {
00328             std::cout << "\"" << camToRemove << "\" is not a valid string for the camera." << std::endl
00329                 << "    Use syntax MAKER|MODEL (separate camera maker and model by |)" << std::endl;
00330         };
00331     };
00332     // export database
00333     if (!exportDatabase.empty())
00334     {
00335         std::cout << "Exporting database to \"" << exportDatabase << "\"..." << std::endl;
00336         if (HuginBase::LensDB::LensDB::GetSingleton().ExportToFile(exportDatabase))
00337         {
00338             std::cout << "Successful." << std::endl;
00339         }
00340         else
00341         {
00342             std::cout << "FAILED." << std::endl;
00343         };
00344     };
00345     // import database
00346     if (!importDatabase.empty())
00347     {
00348         std::cout << "Importing data from \"" << importDatabase << "\"..." << std::endl;
00349         if (HuginBase::LensDB::LensDB::GetSingleton().ImportFromFile(importDatabase))
00350         {
00351             std::cout << "Successful." << std::endl;
00352         }
00353         else
00354         {
00355             std::cout << "FAILED." << std::endl;
00356         };
00357     };
00358     HuginBase::LensDB::LensDB::Clean();
00359     return 0;
00360 }

Generated on 23 Jan 2018 for Hugintrunk by  doxygen 1.4.7