nona_gui.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 #include <hugin_config.h>
00028 #include <hugin_version.h>
00029 #include "panoinc_WX.h"
00030 #include "panoinc.h"
00031 
00032 
00033 #include <fstream>
00034 #include <sstream>
00035 #include <vigra/error.hxx>
00036 #include "PT/Stitcher.h"
00037 #include "base_wx/MyProgressDialog.h"
00038 #include "base_wx/huginConfig.h"
00039 #include "base_wx/platform.h"
00040 
00041 #include <tiffio.h>
00042 
00043 // somewhere SetDesc gets defined.. this breaks wx/cmdline.h on OSX
00044 #ifdef SetDesc
00045 #undef SetDesc
00046 #endif
00047 
00048 #include <wx/cmdline.h>
00049 
00050 using namespace vigra;
00051 using namespace PT;
00052 using namespace std;
00053 
00058 class nonaApp : public wxApp
00059 {
00060 public:
00061 
00064     nonaApp();
00065 
00068     virtual ~nonaApp();
00069 
00072     virtual bool OnInit();
00073 
00075     virtual int OnExit();
00076     
00077 #ifdef __WXMAC__
00078 
00079     void MacOpenFile(const wxString &fileName);
00080 #endif
00081 
00082 private:
00083     wxLocale m_locale;
00084 #ifdef __WXMAC__
00085     wxString m_macFileNameToOpenOnStart;
00086 #endif
00087 };
00088 
00089 
00090 nonaApp::nonaApp()
00091 {
00092     // suppress tiff warnings
00093     TIFFSetWarningHandler(0);
00094 
00095     DEBUG_TRACE("ctor");
00096 }
00097 
00098 nonaApp::~nonaApp()
00099 {
00100     DEBUG_TRACE("dtor");
00101     DEBUG_TRACE("dtor end");
00102 }
00103 
00104 bool nonaApp::OnInit()
00105 {
00106     SetAppName(wxT("nona_gui"));
00107     m_locale.Init(wxLANGUAGE_DEFAULT);
00108 #if defined __WXMSW__
00109         wxString nonaExeDir = getExePath(argv[0]);
00110         
00111         wxString nonaRoot;
00112         wxFileName::SplitPath(nonaExeDir, &nonaRoot, NULL, NULL);
00113         // locale setup
00114     m_locale.AddCatalogLookupPathPrefix(nonaRoot + wxT("/share/locale"));
00115 #elif (defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE)
00116     // TODO: add localisation init
00117 #else
00118     DEBUG_INFO("add locale path: " << INSTALL_LOCALE_DIR);
00119     m_locale.AddCatalogLookupPathPrefix(wxT(INSTALL_LOCALE_DIR));
00120 #endif
00121 
00122     // set the name of locale recource to look for
00123     m_locale.AddCatalog(wxT("hugin"));
00124         
00125 #if 0
00126 #ifdef wxUSE_UNIX
00127     wxLog *logger=new wxLogStream(&cerr);
00128     wxLog::SetActiveTarget(logger);
00129 #endif
00130 #endif
00131 
00132     // parse arguments
00133     static const wxCmdLineEntryDesc cmdLineDesc[] =
00134     {
00135         //On wxWidgets 2.9, wide characters don't work here.
00136         //On previous versions, the wxT macro is required for unicode builds.
00137 #if wxCHECK_VERSION(2,9,0)
00138       { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
00139         wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
00140       { wxCMD_LINE_OPTION, "o", "output",  "output file" },
00141       { wxCMD_LINE_OPTION, "t", "threads",  "number of threads",
00142              wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL },
00143       { wxCMD_LINE_PARAM,  NULL, NULL, "<project> <images>",
00144 #else
00145       { wxCMD_LINE_SWITCH, wxT("h"), wxT("help"), wxT("show this help message"),
00146         wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
00147       { wxCMD_LINE_OPTION, wxT("o"), wxT("output"),  wxT("output file") },
00148       { wxCMD_LINE_OPTION, wxT("t"), wxT("threads"),  wxT("number of threads"),
00149              wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL },
00150       { wxCMD_LINE_PARAM,  NULL, NULL, wxT("<project> <images>"),
00151 #endif
00152         wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL + wxCMD_LINE_PARAM_MULTIPLE },
00153       { wxCMD_LINE_NONE }
00154     };
00155 
00156     wxCmdLineParser parser(cmdLineDesc, argc, argv);
00157 
00158     switch ( parser.Parse() ) {
00159       case -1: // -h or --help was given, and help displayed so exit
00160         return false;
00161         break;
00162       case 0:  // all is well
00163         break;
00164       default:
00165         wxLogError(_("Syntax error in parameters detected, aborting."));
00166         return false;
00167         break;
00168     }
00169 
00170     bool imgsFromCmdline = false;
00171 
00172     wxString scriptFile;
00173 #ifdef __WXMAC__
00174     m_macFileNameToOpenOnStart = wxT("");
00175     wxYield();
00176     scriptFile = m_macFileNameToOpenOnStart;
00177 #endif
00178     if( parser.GetParamCount() == 0 && wxIsEmpty(scriptFile)) 
00179     {
00180         wxFileDialog dlg(0,_("Specify project source project file"),
00181                         wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")),
00182                         wxT(""), wxT(""),
00183                         wxFD_OPEN, wxDefaultPosition);
00184         dlg.SetDirectory(wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")));
00185         if (dlg.ShowModal() == wxID_OK) {
00186         wxConfig::Get()->Write(wxT("/actualPath"), dlg.GetDirectory());  // remember for later
00187         scriptFile = dlg.GetPath();
00188         } else { // bail
00189         return false;
00190         }
00191     } else if(wxIsEmpty(scriptFile)) {
00192         scriptFile = parser.GetParam(0);
00193         if ( parser.GetParamCount() > 1) {
00194           // load images.
00195           imgsFromCmdline = true;
00196         }
00197     }
00198 
00199     DEBUG_DEBUG("input file is " << (const char *)scriptFile.mb_str(wxConvLocal))
00200 
00201     wxFileName fname(scriptFile);
00202     if ( !fname.FileExists() ) {
00203       wxLogError( _("Could not find project file:") + scriptFile);
00204       return false;
00205     }
00206     wxString path = fname.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
00207 
00208     wxString outname;
00209 
00210     if ( !parser.Found(wxT("o"), &outname) ) {
00211         // ask for output.
00212         wxFileDialog dlg(0,_("Specify output image filename"),
00213                          wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")),
00214                          wxT(""), wxT(""),
00215                          wxFD_SAVE, wxDefaultPosition);
00216         dlg.SetDirectory(wxConfigBase::Get()->Read(wxT("/actualPath"),wxT("")));
00217         if (dlg.ShowModal() == wxID_OK) {
00218             wxConfig::Get()->Write(wxT("/actualPath"), dlg.GetDirectory());  // remember for later
00219             outname = dlg.GetPath();
00220         } else { // bail
00221             wxLogError( _("No project files specified"));
00222             return false;
00223         }
00224     }
00225     DEBUG_DEBUG("output file specified is " << (const char *)outname.mb_str(wxConvLocal));
00226 
00227     long nThreads = wxThread::GetCPUCount();
00228     parser.Found(wxT("t"), & nThreads);
00229     if (nThreads <= 0) nThreads = 1;
00230     vigra_ext::ThreadManager::get().setNThreads((unsigned) nThreads);
00231 
00232     wxString basename;
00233     wxString outpath;
00234     wxFileName::SplitPath(outname, &outpath, &basename, NULL);
00235 
00236     //utils::StreamMultiProgressDisplay pdisp(cout);
00237     MyProgressDialog pdisp(_("Stitching Panorama"), wxT(""), NULL, wxPD_ELAPSED_TIME | wxPD_AUTO_HIDE | wxPD_APP_MODAL );
00238 
00239     Panorama pano;
00240     PanoramaMemento newPano;
00241     ifstream prjfile((const char *)scriptFile.mb_str(HUGIN_CONV_FILENAME));
00242     if (prjfile.bad()) {
00243         wxLogError( wxString::Format(_("could not open script : %s"), scriptFile.c_str()) );
00244         return false;
00245     }
00246     int ptoVersion = 0;
00247     if (newPano.loadPTScript(prjfile, ptoVersion, (const char *)path.mb_str(HUGIN_CONV_FILENAME))) {
00248       pano.setMemento(newPano);
00249     } else {
00250       wxLogError( wxString::Format(_("error while parsing panotools script: %s"), scriptFile.c_str()) );
00251       return false;
00252     }
00253 
00254     if (imgsFromCmdline) {
00255         if (parser.GetParamCount() -1 != pano.getNrOfImages()) {
00256             wxLogError(_("Wrong number of images specified on command line"));
00257             return false;
00258         }
00259         for (size_t i = 0; i < pano.getNrOfImages(); i++) {
00260             pano.setImageFilename(i, (const char *)parser.GetParam(i+1).mb_str(HUGIN_CONV_FILENAME));
00261         }
00262     }
00263 
00264     PanoramaOptions  opts = pano.getOptions();
00265 
00266     // check for some options
00267 
00268     int w = opts.getWidth();
00269     int h = opts.getHeight();
00270 
00271     cout << (const char *)wxString::Format(wxT("%s"), _("output image size: ")).mb_str(wxConvLocal) << w << "x" << h << std::endl;
00272     wxString outfile;
00273     if ( outpath != wxT("") ) {
00274       outfile = outpath + wxFileName::GetPathSeparator() + basename;
00275     } else {
00276       outfile = basename;
00277     }
00278     DEBUG_DEBUG("output name: " << (const char *)outfile.mb_str(wxConvLocal));
00279 
00280     try {
00281         // stitch panorama
00282         UIntSet simgs = pano.getActiveImages();
00283         PT::stitchPanorama(pano, opts,
00284                            pdisp, (const char *)outfile.mb_str(HUGIN_CONV_FILENAME), simgs);
00285     } catch (std::exception & e) {
00286         cerr << "caught exception: " << e.what() << std::endl;
00287         return false;
00288     }
00289 
00290     return false;
00291 }
00292 
00293 
00294 int nonaApp::OnExit()
00295 {
00296     DEBUG_TRACE("");
00297     return 0;
00298 }
00299 
00300 
00301 #ifdef __WXMAC__
00302 // wx calls this method when the app gets "Open file" AppleEvent 
00303 void nonaApp::MacOpenFile(const wxString &fileName)
00304 {
00305     m_macFileNameToOpenOnStart = fileName;
00306 }
00307 #endif
00308 
00309 // make wxwindows use this class as the main application
00310 IMPLEMENT_APP(nonaApp)

Generated on Mon Sep 9 01:25:38 2013 for Hugintrunk by  doxygen 1.3.9.1