Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pto_template.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
2 
11 /* This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This software is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this software. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #include <fstream>
28 #include <sstream>
29 #include <getopt.h>
30 #include <panodata/Panorama.h>
31 
32 static void usage(const char* name)
33 {
34  std::cout << name << ": apply template" << std::endl
35  << name << " version " << hugin_utils::GetHuginVersion() << std::endl
36  << std::endl
37  << "Usage: " << name << " [options] input.pto" << std::endl
38  << std::endl
39  << " Options:" << std::endl
40  << " -o, --output=file.pto Output Hugin PTO file." << std::endl
41  << " Default: <filename>_template.pto" << std::endl
42  << " --template=template.pto Apply the given template file" << std::endl
43  << " -h, --help Shows this help" << std::endl
44  << std::endl;
45 }
46 
47 int main(int argc, char* argv[])
48 {
49  // parse arguments
50  const char* optstring = "o:t:h";
51  enum
52  {
53  MINOVERLAP=1000
54  };
55 
56  static struct option longOptions[] =
57  {
58  {"output", required_argument, NULL, 'o' },
59  {"template", required_argument, NULL, 't'},
60  {"help", no_argument, NULL, 'h' },
61  0
62  };
63 
64  int c;
65  std::string output;
66  std::string templateFile;
67  while ((c = getopt_long (argc, argv, optstring, longOptions,nullptr)) != -1)
68  {
69  switch (c)
70  {
71  case 'o':
72  output = optarg;
73  break;
74  case 't':
75  templateFile = optarg;
76  if(!hugin_utils::FileExists(templateFile))
77  {
78  std::cerr << hugin_utils::stripPath(argv[0]) << ": Template \"" << templateFile << "\" not found." << std::endl;
79  return 1;
80  };
81  break;
82  case 'h':
83  usage(hugin_utils::stripPath(argv[0]).c_str());
84  return 0;
85  case ':':
86  case '?':
87  // missing argument or invalid switch
88  return 1;
89  break;
90  default:
91  // this should not happen
92  abort();
93  }
94  }
95 
96  if (argc - optind != 1)
97  {
98  if (argc - optind < 1)
99  {
100  std::cerr << hugin_utils::stripPath(argv[0]) << ": No project file given." << std::endl;
101  }
102  else
103  {
104  std::cerr << hugin_utils::stripPath(argv[0]) << ": Only one project file expected." << std::endl;
105  };
106  return 1;
107  };
108  if (templateFile.empty())
109  {
110  std::cerr << hugin_utils::stripPath(argv[0]) << ": No template given." << std::endl;
111  return 1;
112  };
113 
114  std::string input=argv[optind];
115  // read panorama
116  HuginBase::Panorama pano;
117  if (!pano.ReadPTOFile(input, hugin_utils::getPathPrefix(input)))
118  {
119  return 1;
120  };
121 
122  HuginBase::Panorama newPano;
123  if (!newPano.ReadPTOFile(templateFile, hugin_utils::getPathPrefix(templateFile)))
124  {
125  return 1;
126  };
127 
128  if (pano.getNrOfImages() != newPano.getNrOfImages())
129  {
130  std::cerr << "Error: template expects " << newPano.getNrOfImages() << " images," << std::endl
131  << " current project contains " << pano.getNrOfImages() << " images" << std::endl
132  << " Could not apply template" << std::endl;
133  return false;
134  }
135 
136  // check image sizes, and correct parameters if required.
138  for (unsigned int i = 0; i < newPano.getNrOfImages(); i++)
139  {
140  // check if image size is correct
141  const HuginBase::SrcPanoImage& oldSrcImg = pano.getImage(i);
142  HuginBase::SrcPanoImage newSrcImg = newPano.getSrcImage(i);
143 
144  // just keep the file name
145  newSrcImg.setFilename(oldSrcImg.getFilename());
146  if (oldSrcImg.getSize() != newSrcImg.getSize())
147  {
148  // adjust size properly.
149  newSrcImg.resize(oldSrcImg.getSize(), &vars[i]);
150  }
151  newPano.setSrcImage(i, newSrcImg);
152  }
153  // now update all possible linked variables
154  for (unsigned int i = 0; i < newPano.getNrOfImages(); ++i)
155  {
156  if (!vars[i].empty())
157  {
158  newPano.updateVariables(i, vars[i]);
159  };
160  };
161 
162  // keep old control points.
163  newPano.setCtrlPoints(pano.getCtrlPoints());
164 
165  //write output
166  // Set output .pto filename if not given
167  output = hugin_utils::GetOutputFilename(output, input, "template");
168  if (newPano.WritePTOFile(output, hugin_utils::getPathPrefix(output)))
169  {
170  std::cout << std::endl << "Written project file " << output << std::endl;
171  };
172  return 0;
173 }
bool FileExists(const std::string &filename)
checks if file exists
Definition: utils.cpp:362
SrcPanoImage getSrcImage(unsigned imgNr) const
get a description of a source image
Definition: Panorama.cpp:1620
std::string GetOutputFilename(const std::string &out, const std::string &in, const std::string &suffix)
construct output filename, if ouput is known return this value otherwise use the input filename and a...
Definition: utils.cpp:420
const CPVector & getCtrlPoints() const
get all control point of this Panorama
Definition: Panorama.h:319
virtual void updateVariables(const VariableMapVector &vars)
Set the variables.
Definition: Panorama.cpp:171
std::vector< VariableMap > VariableMapVector
Model for a panorama.
Definition: Panorama.h:152
std::string getPathPrefix(const std::string &filename)
Get the path to a filename.
Definition: utils.cpp:184
std::size_t getNrOfImages() const
number of images.
Definition: Panorama.h:205
void setCtrlPoints(const CPVector &points)
set all control points (Ippei: Is this supposed to be &#39;add&#39; method?)
Definition: Panorama.cpp:449
bool ReadPTOFile(const std::string &filename, const std::string &prefix="")
read pto file from the given filename into Panorama object it does some checks on the file and issues...
Definition: Panorama.cpp:2023
void resize(const vigra::Size2D &size, VariableMap *potentialLinkedVars)
&quot;resize&quot; image, adjusts all distortion coefficients for usage with a source image of size size potent...
static void usage()
Definition: Main.cpp:32
std::string GetHuginVersion()
return a string with version numbers
Definition: utils.cpp:920
bool WritePTOFile(const std::string &filename, const std::string &prefix="")
write data to given pto file
Definition: Panorama.cpp:2059
const SrcPanoImage & getImage(std::size_t nr) const
get a panorama image, counting starts with 0
Definition: Panorama.h:211
void setSrcImage(unsigned int nr, const SrcPanoImage &img)
set input image parameters
All variables of a source image.
Definition: SrcPanoImage.h:194
std::string stripPath(const std::string &filename)
remove the path of a filename (mainly useful for gui display of filenames)
Definition: utils.cpp:160
int main(int argc, char *argv[])
Definition: Main.cpp:167