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

Generated on 23 Oct 2014 for Hugintrunk by  doxygen 1.4.7