pto_template.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00011 /*  This program is free software; you can redistribute it and/or
00012  *  modify it under the terms of the GNU General Public
00013  *  License as published by the Free Software Foundation; either
00014  *  version 2 of the License, or (at your option) any later version.
00015  *
00016  *  This software is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *  General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public
00022  *  License along with this software. If not, see
00023  *  <http://www.gnu.org/licenses/>.
00024  *
00025  */
00026 
00027 #include <fstream>
00028 #include <sstream>
00029 #include <getopt.h>
00030 #ifndef _WIN32
00031 #include <unistd.h>
00032 #endif
00033 #include <panodata/Panorama.h>
00034 
00035 static void usage(const char* name)
00036 {
00037     std::cout << name << ": apply template" << std::endl
00038          << name << " version " << hugin_utils::GetHuginVersion() << std::endl
00039          << std::endl
00040          << "Usage:  " << name << " [options] input.pto" << std::endl
00041          << std::endl
00042          << "  Options:" << std::endl
00043          << "     -o, --output=file.pto  Output Hugin PTO file." << std::endl
00044          << "                            Default: <filename>_template.pto" << std::endl
00045          << "     --template=template.pto   Apply the given template file" << std::endl
00046          << "     -h, --help             Shows this help" << std::endl
00047          << std::endl;
00048 }
00049 
00050 int main(int argc, char* argv[])
00051 {
00052     // parse arguments
00053     const char* optstring = "o:t:h";
00054     enum
00055     {
00056         MINOVERLAP=1000
00057     };
00058 
00059     static struct option longOptions[] =
00060     {
00061         {"output", required_argument, NULL, 'o' },
00062         {"template", required_argument, NULL, 't'},
00063         {"help", no_argument, NULL, 'h' },
00064         0
00065     };
00066 
00067     int c;
00068     int optionIndex = 0;
00069     std::string output;
00070     std::string templateFile;
00071     while ((c = getopt_long (argc, argv, optstring, longOptions,&optionIndex)) != -1)
00072     {
00073         switch (c)
00074         {
00075             case 'o':
00076                 output = optarg;
00077                 break;
00078             case 't':
00079                 templateFile = optarg;
00080                 if(!hugin_utils::FileExists(templateFile))
00081                 {
00082                     std::cerr << "Error: Template \"" << templateFile << "\" not found." << std::endl;
00083                     return 1;
00084                 };
00085                 break;
00086             case 'h':
00087                 usage(hugin_utils::stripPath(argv[0]).c_str());
00088                 return 0;
00089             case ':':
00090                 std::cerr <<"Option " << longOptions[optionIndex].name << " requires a parameter" << std::endl;
00091                 return 1;
00092                 break;
00093             case '?':
00094                 break;
00095             default:
00096                 abort ();
00097         }
00098     }
00099 
00100     if (argc - optind == 0)
00101     {
00102         std::cout << "Error: No project file given." << std::endl;
00103         return 1;
00104     };
00105     if (argc - optind != 1)
00106     {
00107         std::cout << "Error: pto_template can only work on one project file at one time" << std::endl;
00108         return 1;
00109     };
00110     if (templateFile.length()==0)
00111     {
00112         std::cerr << "Error: No template given." << std::endl;
00113         return 1;
00114     };
00115 
00116     std::string input=argv[optind];
00117     // read panorama
00118     HuginBase::Panorama pano;
00119     std::ifstream prjfile(input.c_str());
00120     if (!prjfile.good())
00121     {
00122         std::cerr << "Error: could not open script : " << input << std::endl;
00123         return 1;
00124     }
00125     pano.setFilePrefix(hugin_utils::getPathPrefix(input));
00126     AppBase::DocumentData::ReadWriteError err = pano.readData(prjfile);
00127     if (err != AppBase::DocumentData::SUCCESSFUL)
00128     {
00129         std::cerr << "Error while parsing panos tool script: " << input << std::endl;
00130         std::cerr << "AppBase::DocumentData::ReadWriteError code: " << err << std::endl;
00131         return 1;
00132     }
00133 
00134     if(pano.getNrOfImages()==0)
00135     {
00136         std::cerr << "Error: project file does not contains any image" << std::endl;
00137         std::cerr << "aborting processing" << std::endl;
00138         return 1;
00139     };
00140 
00141     HuginBase::Panorama newPano;
00142     std::ifstream templateStream(templateFile.c_str());
00143     if (!templateStream.good())
00144     {
00145         std::cerr << "Error: could not open template script : " << templateFile << std::endl;
00146         return 1;
00147     }
00148     newPano.setFilePrefix(hugin_utils::getPathPrefix(templateFile));
00149     err = newPano.readData(templateStream);
00150     if (err != AppBase::DocumentData::SUCCESSFUL)
00151     {
00152         std::cerr << "Error while parsing template script: " << templateFile << std::endl;
00153         std::cerr << "AppBase::DocumentData::ReadWriteError code: " << err << std::endl;
00154         return 1;
00155     }
00156 
00157     if (pano.getNrOfImages() != newPano.getNrOfImages())
00158     {
00159         std::cerr << "Error: template expects " << newPano.getNrOfImages() << " images," << std::endl
00160              << "       current project contains " << pano.getNrOfImages() << " images" << std::endl
00161              << "       Could not apply template" << std::endl;
00162         return false;
00163     }
00164 
00165     // check image sizes, and correct parameters if required.
00166     for (unsigned int i = 0; i < newPano.getNrOfImages(); i++)
00167     {
00168         // check if image size is correct
00169         const HuginBase::SrcPanoImage& oldSrcImg = pano.getImage(i);
00170         HuginBase::SrcPanoImage newSrcImg = newPano.getSrcImage(i);
00171 
00172         // just keep the file name
00173         newSrcImg.setFilename(oldSrcImg.getFilename());
00174         if (oldSrcImg.getSize() != newSrcImg.getSize())
00175         {
00176             // adjust size properly.
00177             newSrcImg.resize(oldSrcImg.getSize());
00178         }
00179         newPano.setSrcImage(i, newSrcImg);
00180     }
00181     // keep old control points.
00182     newPano.setCtrlPoints(pano.getCtrlPoints());
00183 
00184     //write output
00185     HuginBase::UIntSet imgs;
00186     fill_set(imgs, 0, newPano.getNrOfImages()-1);
00187     // Set output .pto filename if not given
00188     if (output=="")
00189     {
00190         output=input.substr(0,input.length()-4).append("_template.pto");
00191     }
00192     std::ofstream of(output.c_str());
00193     newPano.printPanoramaScript(of, newPano.getOptimizeVector(), newPano.getOptions(), imgs, false, hugin_utils::getPathPrefix(input));
00194 
00195     std::cout << std::endl << "Written output to " << output << std::endl;
00196     return 0;
00197 }

Generated on 30 Jun 2016 for Hugintrunk by  doxygen 1.4.7