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 using namespace std;
00036 using namespace HuginBase;
00037 using namespace AppBase;
00038 
00039 static void usage(const char* name)
00040 {
00041     cout << name << ": apply template" << endl
00042          << name << " version " << hugin_utils::GetHuginVersion() << endl
00043          << endl
00044          << "Usage:  " << name << " [options] input.pto" << endl
00045          << endl
00046          << "  Options:" << endl
00047          << "     -o, --output=file.pto  Output Hugin PTO file." << endl
00048          << "                            Default: <filename>_template.pto" << endl
00049          << "     --template=template.pto   Apply the given template file" << endl
00050          << "     -h, --help             Shows this help" << endl
00051          << endl;
00052 }
00053 
00054 int main(int argc, char* argv[])
00055 {
00056     // parse arguments
00057     const char* optstring = "o:t:h";
00058     enum
00059     {
00060         MINOVERLAP=1000
00061     };
00062 
00063     static struct option longOptions[] =
00064     {
00065         {"output", required_argument, NULL, 'o' },
00066         {"template", required_argument, NULL, 't'},
00067         {"help", no_argument, NULL, 'h' },
00068         0
00069     };
00070 
00071     int c;
00072     int optionIndex = 0;
00073     string output;
00074     string templateFile;
00075     while ((c = getopt_long (argc, argv, optstring, longOptions,&optionIndex)) != -1)
00076     {
00077         switch (c)
00078         {
00079             case 'o':
00080                 output = optarg;
00081                 break;
00082             case 't':
00083                 templateFile = optarg;
00084                 if(!hugin_utils::FileExists(templateFile))
00085                 {
00086                     cerr << "Error: Template \"" << templateFile << "\" not found." << endl;
00087                     return 1;
00088                 };
00089                 break;
00090             case 'h':
00091                 usage(hugin_utils::stripPath(argv[0]).c_str());
00092                 return 0;
00093             case ':':
00094                 cerr <<"Option " << longOptions[optionIndex].name << " requires a parameter" << endl;
00095                 return 1;
00096                 break;
00097             case '?':
00098                 break;
00099             default:
00100                 abort ();
00101         }
00102     }
00103 
00104     if (argc - optind == 0)
00105     {
00106         cout << "Error: No project file given." << endl;
00107         return 1;
00108     };
00109     if (argc - optind != 1)
00110     {
00111         cout << "Error: pto_template can only work on one project file at one time" << endl;
00112         return 1;
00113     };
00114     if (templateFile.length()==0)
00115     {
00116         cerr << "Error: No template given." << endl;
00117         return 1;
00118     };
00119 
00120     string input=argv[optind];
00121     // read panorama
00122     Panorama pano;
00123     ifstream prjfile(input.c_str());
00124     if (!prjfile.good())
00125     {
00126         cerr << "Error: could not open script : " << input << endl;
00127         return 1;
00128     }
00129     pano.setFilePrefix(hugin_utils::getPathPrefix(input));
00130     DocumentData::ReadWriteError err = pano.readData(prjfile);
00131     if (err != DocumentData::SUCCESSFUL)
00132     {
00133         cerr << "Error while parsing panos tool script: " << input << endl;
00134         cerr << "DocumentData::ReadWriteError code: " << err << endl;
00135         return 1;
00136     }
00137 
00138     if(pano.getNrOfImages()==0)
00139     {
00140         cerr << "Error: project file does not contains any image" << endl;
00141         cerr << "aborting processing" << endl;
00142         return 1;
00143     };
00144 
00145     Panorama newPano;
00146     ifstream templateStream(templateFile.c_str());
00147     if (!templateStream.good())
00148     {
00149         cerr << "Error: could not open template script : " << templateFile << endl;
00150         return 1;
00151     }
00152     newPano.setFilePrefix(hugin_utils::getPathPrefix(templateFile));
00153     err = newPano.readData(templateStream);
00154     if (err != DocumentData::SUCCESSFUL)
00155     {
00156         cerr << "Error while parsing template script: " << templateFile << endl;
00157         cerr << "DocumentData::ReadWriteError code: " << err << endl;
00158         return 1;
00159     }
00160 
00161     if (pano.getNrOfImages() != newPano.getNrOfImages())
00162     {
00163         cerr << "Error: template expects " << newPano.getNrOfImages() << " images," << endl
00164              << "       current project contains " << pano.getNrOfImages() << " images" << endl
00165              << "       Could not apply template" << endl;
00166         return false;
00167     }
00168 
00169     // check image sizes, and correct parameters if required.
00170     for (unsigned int i = 0; i < newPano.getNrOfImages(); i++)
00171     {
00172         // check if image size is correct
00173         const SrcPanoImage& oldSrcImg = pano.getImage(i);
00174         SrcPanoImage newSrcImg = newPano.getSrcImage(i);
00175 
00176         // just keep the file name
00177         newSrcImg.setFilename(oldSrcImg.getFilename());
00178         if (oldSrcImg.getSize() != newSrcImg.getSize())
00179         {
00180             // adjust size properly.
00181             newSrcImg.resize(oldSrcImg.getSize());
00182         }
00183         newPano.setSrcImage(i, newSrcImg);
00184     }
00185     // keep old control points.
00186     newPano.setCtrlPoints(pano.getCtrlPoints());
00187 
00188     //write output
00189     UIntSet imgs;
00190     fill_set(imgs, 0, newPano.getNrOfImages()-1);
00191     // Set output .pto filename if not given
00192     if (output=="")
00193     {
00194         output=input.substr(0,input.length()-4).append("_template.pto");
00195     }
00196     ofstream of(output.c_str());
00197     newPano.printPanoramaScript(of, newPano.getOptimizeVector(), newPano.getOptions(), imgs, false, hugin_utils::getPathPrefix(input));
00198 
00199     cout << endl << "Written output to " << output << endl;
00200     return 0;
00201 }

Generated on 3 Aug 2015 for Hugintrunk by  doxygen 1.4.7