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

Generated on 10 Dec 2016 for Hugintrunk by  doxygen 1.4.7