00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define NDEBUG 1
00024
00025 #include <iomanip>
00026 #include <fstream>
00027 #include <string>
00028 #include <sstream>
00029 #include <exception>
00030 #include <iostream>
00031
00032 #include <boost/program_options.hpp>
00033 #include <boost/thread.hpp>
00034 #include <boost/thread/xtime.hpp>
00035 #include <boost/date_time/posix_time/posix_time.hpp>
00036 #include <boost/filesystem/path.hpp>
00037 #include <boost/filesystem/operations.hpp>
00038 #include <boost/scoped_ptr.hpp>
00039
00040 #include "lux.h"
00041 #include "api.h"
00042 #include "context.h"
00043 #include "fleximage.h"
00044 #include "error.h"
00045 #include "osfunc.h"
00046
00047 #if defined(WIN32) && !defined(__CYGWIN__)
00048 #include <io.h>
00049 #include <fcntl.h>
00050 #endif
00051
00052 #if defined(WIN32) && !defined(__CYGWIN__)
00053 #include "direct.h"
00054 #define chdir _chdir
00055 #endif
00056
00057 using namespace lux;
00058 namespace po = boost::program_options;
00059
00060 int main(int ac, char *av[]) {
00061
00062 try {
00063 std::stringstream ss;
00064
00065
00066
00067 po::options_description generic("Generic options");
00068 generic.add_options()
00069 ("version,v", "Print version string")
00070 ("help,h", "Produce help message")
00071 ("debug,d", "Enable debug mode")
00072 ("output,o", po::value< std::string >()->default_value("merged.flm"), "Output file")
00073 ("verbosity,V", po::value< int >(), "Log output verbosity")
00074 ;
00075
00076
00077
00078 po::options_description hidden("Hidden options");
00079 hidden.add_options()
00080 ("input-file", po::value< vector<string> >(), "input file")
00081 ("test", "debug test mode")
00082 ;
00083
00084 po::options_description cmdline_options;
00085 cmdline_options.add(generic).add(hidden);
00086
00087 po::options_description visible("Allowed options");
00088 visible.add(generic);
00089
00090 po::positional_options_description p;
00091
00092 p.add("input-file", -1);
00093
00094 po::variables_map vm;
00095 store(po::command_line_parser(ac, av).
00096 options(cmdline_options).positional(p).run(), vm);
00097
00098 if (vm.count("help")) {
00099 ss.str("");
00100 ss << "Usage: flmmerger [options] file...\n" << visible;
00101 luxError(LUX_SYSTEM, LUX_ERROR, ss.str().c_str());
00102 return 0;
00103 }
00104
00105 if (vm.count("verbosity"))
00106 luxLogFilter = vm["verbosity"].as<int>();
00107
00108 ss.str("");
00109 ss << "Lux version " << LUX_VERSION << " of " << __DATE__ << " at " << __TIME__;
00110 luxError(LUX_NOERROR, LUX_INFO, ss.str().c_str());
00111 if (vm.count("version"))
00112 return 0;
00113
00114 if (vm.count("debug")) {
00115 luxError(LUX_NOERROR, LUX_INFO, "Debug mode enabled");
00116 }
00117
00118 string outputFileName = vm["output"].as<string>();
00119
00120 boost::scoped_ptr<FlexImageFilm> film;
00121 int mergedCount = 0;
00122
00123 if (vm.count("input-file")) {
00124 const std::vector<std::string> &v = vm["input-file"].as < vector<string> > ();
00125 for (unsigned int i = 0; i < v.size(); i++) {
00126 boost::filesystem::path fullPath(boost::filesystem::initial_path());
00127 fullPath = boost::filesystem::system_complete(boost::filesystem::path(v[i], boost::filesystem::native));
00128
00129 if (!boost::filesystem::exists(fullPath) && v[i] != "-") {
00130 ss.str("");
00131 ss << "Unable to open file '" << fullPath.string() << "'";
00132 luxError(LUX_NOFILE, LUX_SEVERE, ss.str().c_str());
00133 continue;
00134 }
00135
00136 std::string flmFileName = fullPath.string();
00137
00138 if (!film) {
00139
00140 film.reset((FlexImageFilm*)FlexImageFilm::CreateFilmFromFLM(flmFileName));
00141 if (!film) {
00142 ss.str("");
00143 ss << "Error reading FLM file '" << flmFileName << "'";
00144 luxError(LUX_NOFILE, LUX_SEVERE, ss.str().c_str());
00145 continue;
00146 }
00147 } else {
00148
00149 std::ifstream ifs(flmFileName.c_str(), std::ios_base::in | std::ios_base::binary);
00150
00151 if(ifs.good()) {
00152
00153 luxError(LUX_NOERROR, LUX_INFO, (std::string("Merging FLM file ") + flmFileName).c_str());
00154 float newSamples = film->UpdateFilm(ifs);
00155 if (newSamples <= 0) {
00156 ss.str("");
00157 ss << "Error reading FLM file '" << flmFileName << "'";
00158 luxError(LUX_NOFILE, LUX_SEVERE, ss.str().c_str());
00159 ifs.close();
00160 continue;
00161 } else {
00162 ss.str("");
00163 ss << "Merged " << newSamples << " samples from FLM file";
00164 luxError(LUX_NOERROR, LUX_DEBUG, ss.str().c_str());
00165 }
00166 }
00167
00168 ifs.close();
00169 }
00170
00171 mergedCount++;
00172 }
00173
00174 if (!film) {
00175 ss.str("");
00176 ss << "No files merged";
00177 luxError(LUX_NOERROR, LUX_WARNING, ss.str().c_str());
00178 return 2;
00179 }
00180
00181 ss.str("");
00182 ss << "Merged " << mergedCount << " FLM files, writing merged FLM to " << outputFileName;
00183 luxError(LUX_NOERROR, LUX_INFO, ss.str().c_str());
00184
00185 film->WriteFilm(outputFileName);
00186 } else {
00187 ss.str("");
00188 ss << "flmmerger: no input file";
00189 luxError(LUX_SYSTEM, LUX_ERROR, ss.str().c_str());
00190 }
00191
00192 } catch (std::exception & e) {
00193 std::stringstream ss;
00194 ss << "Command line argument parsing failed with error '" << e.what() << "', please use the --help option to view the allowed syntax.";
00195 luxError(LUX_SYNTAX, LUX_SEVERE, ss.str().c_str());
00196 return 1;
00197 }
00198 return 0;
00199 }