[a6a619]: src / hugin1 / base_wx / RunStitchPanel.cpp Maximize Restore History

Download this file

RunStitchPanel.cpp    436 lines (388 with data), 15.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// -*- c-basic-offset: 4 -*-
/** @file RunStitchPanel.cpp
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id: hugin_stitch_project.cpp 2705 2008-01-27 19:56:06Z ippei $
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <hugin_config.h>
#include <hugin_version.h>
#include "panoinc_WX.h"
#include "panoinc.h"
#include <wx/wfstream.h>
#include <fstream>
#include <sstream>
#include <vigra/error.hxx>
#include <vigra_ext/MultiThreadOperations.h>
#include "PT/Panorama.h"
#include "PT/utils.h"
#include "base_wx/huginConfig.h"
#include "base_wx/MyProgressDialog.h"
#include "base_wx/MyExternalCmdExecDialog.h"
#include "base_wx/platform.h"
#include "base_wx/wxPlatform.h"
#include "RunStitchPanel.h"
#include "hugin/config_defaults.h"
using namespace vigra;
using namespace PT;
using namespace std;
using namespace hugin_utils;
// ==========================================
// Implementation of stitch window
// event ID's for RunStitchPanel
enum
{
ID_Quit = 1,
ID_About
};
BEGIN_EVENT_TABLE(RunStitchPanel, wxPanel)
EVT_END_PROCESS(-1, RunStitchPanel::OnProcessTerminate)
END_EVENT_TABLE()
RunStitchPanel::RunStitchPanel(wxWindow * parent)
: wxPanel(parent)
{
m_paused=false;
m_overwrite=false;
/*
wxMenu *menuFile = new wxMenu;
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenu *menuHelp = new wxMenu;
menuHelp->Append( ID_About, _("&About...") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
menuBar->Append( menuHelp, _("&Help") );
SetMenuBar( menuBar );
*/
wxBoxSizer * topsizer = new wxBoxSizer( wxVERTICAL );
m_execPanel = new MyExecPanel(this);
#ifdef __WXMSW__
// wxFrame does have a strange background color on Windows, copy color from a child widget
this->SetBackgroundColour(m_execPanel->GetBackgroundColour());
#endif
topsizer->Add(m_execPanel, 1, wxEXPAND, 0);
SetSizer( topsizer );
// topsizer->SetSizeHints( this ); // set size hints to honour minimum size
}
wxString getGNUMakeCmd(const wxString &args)
{
#if defined __WXMAC__ && defined MAC_SELF_CONTAINED_BUNDLE
wxString cmd = MacGetPathToBundledExecutableFile(CFSTR("gnumake"));
if(cmd != wxT(""))
{
cmd = wxQuoteString(cmd);
}
else
{
wxMessageBox(wxString::Format(_("External program %s not found in the bundle, reverting to system path"), wxT("gnumake")), _("Error"));
cmd = wxT("make");
}
cmd += wxT(" ") + args;
#elif defined __FreeBSD__
wxString cmd = wxT("gmake ") + args;
#elif defined __WXMSW__
wxString cmdExe;
if(!wxGetEnv(wxT("ComSpec"),&cmdExe))
cmdExe=wxT("cmd");
wxString cmd = cmdExe + wxString::Format(wxT(" /C \"chcp %d >NUL && "),GetACP())+
wxT("\"") + getExePath(wxTheApp->argv[0])+wxT("\\make\" ") + args + wxT("\"");
#else
wxString cmd = wxT("make ") + args;
#endif
return cmd;
};
bool RunStitchPanel::StitchProject(wxString scriptFile, wxString outname,
HuginBase::PanoramaMakefilelibExport::PTPrograms progs)
{
DEBUG_TRACE("");
wxFileName fname(scriptFile);
if ( !fname.FileExists() ) {
wxLogError( _("Could not open project file:") + scriptFile);
return false;
}
wxString pathToPTO;
wxFileName::SplitPath(scriptFile, &pathToPTO, NULL, NULL);
pathToPTO.Append(wxFileName::GetPathSeparator());
ifstream prjfile((const char *)scriptFile.mb_str(HUGIN_CONV_FILENAME));
if (prjfile.bad()) {
wxLogError( wxString::Format(_("could not open script : %s"), scriptFile.c_str()) );
return false;
}
PT::Panorama pano;
PT::PanoramaMemento newPano;
int ptoVersion = 0;
if (newPano.loadPTScript(prjfile, ptoVersion, (const char *)pathToPTO.mb_str(HUGIN_CONV_FILENAME))) {
pano.setMemento(newPano);
HuginBase::PanoramaOptions opts = pano.getOptions();
if (ptoVersion < 2) {
// no options stored in file, use default arguments in config
wxConfig* config = new wxConfig(wxT("hugin")); //needed for PTBatcher console application
wxConfigBase::Set(config); //
opts.enblendOptions = wxConfigBase::Get()->Read(wxT("/Enblend/Args"), wxT(HUGIN_ENBLEND_ARGS)).mb_str(wxConvLocal);
opts.enfuseOptions = wxConfigBase::Get()->Read(wxT("/Enfuse/Args"), wxT(HUGIN_ENFUSE_ARGS)).mb_str(wxConvLocal);
}
opts.remapUsingGPU = wxConfigBase::Get()->Read(wxT("/Nona/UseGPU"), HUGIN_NONA_USEGPU) == 1;
pano.setOptions(opts);
} else {
wxLogError( wxString::Format(_("error while parsing panotools script: %s"), scriptFile.c_str()) );
return false;
}
// get options and correct for correct makefile
PanoramaOptions opts = pano.getOptions();
opts.outputFormat = PanoramaOptions::TIFF_m;
if (opts.enblendOptions.length() == 0) {
// no options stored in file, use default arguments in config file
wxConfig* config = new wxConfig(wxT("hugin")); //needed for PTBatcher console application
wxConfigBase::Set(config); //
opts.enblendOptions = wxConfigBase::Get()->Read(wxT("/Enblend/Args"), wxT(HUGIN_ENBLEND_ARGS)).mb_str(wxConvLocal);
}
pano.setOptions(opts);
// make sure we got an absolute path
if (! wxIsAbsolutePath(outname)) {
outname = wxGetCwd() + wxFileName::GetPathSeparator() + outname;
}
DEBUG_DEBUG("output file specified is " << (const char *)outname.mb_str(wxConvLocal));
wxString basename;
wxString outpath;
wxFileName outputPrefix(outname);
outpath = outputPrefix.GetPath();
basename = outputPrefix.GetFullName();
// stitch only active images
UIntSet activeImgs = pano.getActiveImages();
//get temp dir from preferences
wxString tempDir= wxConfigBase::Get()->Read(wxT("tempDir"),wxT(""));
if(!tempDir.IsEmpty())
if(tempDir.Last()!=wxFileName::GetPathSeparator())
tempDir.Append(wxFileName::GetPathSeparator());
try {
PanoramaOptions opts = pano.getOptions();
//preview image generation
/*opts.setHeight(150);
opts.setWidth(300);
opts.setROI(vigra::Rect2D(vigra::Size2D(300,150)));
pano.setOptions(opts);*/
// copy pto file to temporary file
m_currentPTOfn = wxFileName::CreateTempFileName(tempDir+wxT("huginpto_"));
if(m_currentPTOfn.size() == 0) {
wxLogError(_("Could not create temporary file"));
}
DEBUG_DEBUG("tmpPTOfn file: " << (const char *)m_currentPTOfn.mb_str(wxConvLocal));
// copy is not enough, need to adjust image path names...
ofstream script(m_currentPTOfn.mb_str(HUGIN_CONV_FILENAME));
PT::UIntSet all;
if (pano.getNrOfImages() > 0) {
fill_set(all, 0, pano.getNrOfImages()-1);
}
pano.printPanoramaScript(script, pano.getOptimizeVector(), pano.getOptions(), all, false, "");
script.close();
// produce suitable makefile
wxFile makeFile;
//TODO: change to implementatin with config->Read(wxT("tempDir"),wxT(""))
m_currentMakefn = wxFileName::CreateTempFileName(tempDir+wxT("huginmk_"), &makeFile);
if(m_currentMakefn.size() == 0) {
wxLogError(_("Could not create temporary file"));
return false;
}
DEBUG_DEBUG("makefn file: " << (const char *)m_currentMakefn.mb_str(wxConvLocal));
ofstream makeFileStream(m_currentMakefn.mb_str(HUGIN_CONV_FILENAME));
makeFile.Close();
std::string resultFn(basename.mb_str(HUGIN_CONV_FILENAME));
std::string tmpPTOfnC = (const char *) m_currentPTOfn.mb_str(HUGIN_CONV_FILENAME);
std::string tmpDir((wxConfigBase::Get()->Read(wxT("tempDir"),wxT(""))).mb_str(HUGIN_CONV_FILENAME));
std::vector<std::string> outputFiles;
HuginBase::PanoramaMakefilelibExport::createMakefile(pano,
activeImgs,
tmpPTOfnC,
resultFn,
progs,
"",
outputFiles,
makeFileStream,
tmpDir);
makeFileStream.close();
// cd to output directory, if one is given.
wxString oldCWD = wxFileName::GetCwd();
wxFileName::SetCwd(outpath);
// check output directories.
//std::vector<std::string> overwrittenFiles;
wxString overwrittenFiles;
for(size_t i=0; i < outputFiles.size(); i++) {
wxString fn(outputFiles[i].c_str(), wxConvLocal);
if (wxFile::Exists(fn) ) {
overwrittenFiles.Append(fn + wxT(" "));
}
}
if (!m_overwrite && overwrittenFiles.size() > 0) {
int overwriteret = wxMessageBox(_("Overwrite existing images?\n\n") + overwrittenFiles, _("Overwrite existing images"), wxYES_NO | wxICON_QUESTION);
// TODO: change button label ok to overwrite
if (overwriteret != wxYES) {
DEBUG_DEBUG("Abort, do not overwrite images!");
return false;
}
DEBUG_DEBUG("Overwrite existing images!");
}
#if defined __WXMSW__
wxString args = wxT("-f ") + wxQuoteFilename(m_currentMakefn) + wxT(" info test all clean");
#else
wxString args = wxT("-f ") + wxQuoteString(m_currentMakefn) + wxT(" info test all clean");
#endif
wxString caption = wxString::Format(_("Stitching %s"), scriptFile.c_str());
wxString cmd=getGNUMakeCmd(args);
if (m_execPanel->ExecWithRedirect(cmd) == -1) {
wxMessageBox(wxString::Format(_("Error while stitching project\n%s"), cmd.c_str()),
_("Error during stitching"), wxICON_ERROR | wxOK );
}
wxFileName::SetCwd(oldCWD);
} catch (std::exception & e) {
cerr << "caught exception: " << e.what() << std::endl;
wxMessageBox(wxString(e.what(), wxConvLocal),
_("Error during stitching"), wxICON_ERROR | wxOK );
}
return true;
}
bool RunStitchPanel::DetectProject(wxString scriptFile,
HuginBase::AssistantMakefilelibExport::AssistantPrograms progs)
{
m_currentPTOfn=wxEmptyString;
wxFileName fname(scriptFile);
if ( !fname.FileExists() ) {
wxLogError( _("Could not open project file:") + scriptFile);
return false;
}
//read project file
ifstream prjfile((const char *)scriptFile.mb_str(HUGIN_CONV_FILENAME));
if (prjfile.bad())
{
wxLogError( wxString::Format(_("could not open script : %s"), scriptFile.c_str()));
return false;
}
PT::Panorama pano;
PT::PanoramaMemento newPano;
int ptoVersion = 0;
if (!newPano.loadPTScript(prjfile, ptoVersion))
{
wxLogError(wxString::Format(_("error while parsing panotools script: %s"), scriptFile.c_str()));
return false;
}
pano.setMemento(newPano);
//read settings
wxConfig config(wxT("hugin"));
bool runCeleste=config.Read(wxT("/Celeste/Auto"), HUGIN_CELESTE_AUTO)!=0;
double celesteThreshold;
config.Read(wxT("/Celeste/Threshold"), &celesteThreshold, HUGIN_CELESTE_THRESHOLD);
bool celesteSmall=config.Read(wxT("/Celeste/Filter"), HUGIN_CELESTE_FILTER)!=0;
bool runLinefind=config.Read(wxT("/Assistant/Linefind"), HUGIN_ASS_LINEFIND)!=0;
bool runCPClean=config.Read(wxT("/Assistant/AutoCPClean"), HUGIN_ASS_AUTO_CPCLEAN)!=0;
double scale;
config.Read(wxT("/Assistant/panoDownsizeFactor"), &scale, HUGIN_ASS_PANO_DOWNSIZE_FACTOR);
int scalei=roundi(scale*100);
wxString tempDir= config.Read(wxT("tempDir"),wxT(""));
if(!tempDir.IsEmpty())
if(tempDir.Last()!=wxFileName::GetPathSeparator())
tempDir.Append(wxFileName::GetPathSeparator());
try {
//generate makefile
m_currentMakefn=wxFileName::CreateTempFileName(tempDir+wxT("ham"));
if(m_currentMakefn.size() == 0)
{
wxLogError(_("Could not create temporary file"));
return false;
}
std::ofstream makefile(m_currentMakefn.mb_str(HUGIN_CONV_FILENAME));
makefile.exceptions( std::ofstream::eofbit | std::ofstream::failbit | std::ofstream::badbit );
std::string scriptString(scriptFile.mb_str(HUGIN_CONV_FILENAME));
HuginBase::AssistantMakefilelibExport::createMakefile(pano,progs,runLinefind,runCeleste,celesteThreshold,celesteSmall,
runCPClean,scale,makefile,scriptString);
makefile.close();
//execute makefile
wxString args = wxT("-f ") + wxQuoteFilename(m_currentMakefn) + wxT(" all");
wxString cmd=getGNUMakeCmd(args);
wxFileName path(scriptFile);
path.MakeAbsolute();
wxString oldcwd=path.GetCwd();
path.SetCwd();
if (m_execPanel->ExecWithRedirect(cmd) == -1)
{
wxMessageBox(wxString::Format(_("Error while running assistant\n%s"), cmd.c_str()),
_("Error during running assistant"), wxICON_ERROR | wxOK );
}
wxFileName::SetCwd(oldcwd);
}
catch (std::exception & e)
{
cerr << "caught exception: " << e.what() << std::endl;
wxMessageBox(wxString(e.what(), wxConvLocal),
_("Error during running assistant"), wxICON_ERROR | wxOK );
}
return true;
}
void RunStitchPanel::OnProcessTerminate(wxProcessEvent & event)
{
DEBUG_TRACE("");
//if(!m_paused)
//{
// delete temporary files
#ifndef DEBUG
if(!m_currentMakefn.IsEmpty())
{
wxRemoveFile(m_currentMakefn);
};
if(!m_currentPTOfn.IsEmpty())
{
wxRemoveFile(m_currentPTOfn);
};
#endif
// notify parent of exit
if (this->GetParent()) {
event.SetEventObject( this );
DEBUG_TRACE("Sending wxProcess event");
this->GetParent()->GetEventHandler()->ProcessEvent( event );
}
//}
}
void RunStitchPanel::CancelStitch()
{
DEBUG_TRACE("");
m_execPanel->KillProcess();
}
bool RunStitchPanel::IsPaused()
{
return m_paused;
}
void RunStitchPanel::SetOverwrite(bool over)
{
m_overwrite = over;
}
void RunStitchPanel::PauseStitch()
{
m_paused=true;
m_execPanel->PauseProcess();
}
void RunStitchPanel::ContinueStitch()
{
m_execPanel->ContinueProcess();
m_paused=false;
}
long RunStitchPanel::GetPid()
{
return m_execPanel->GetPid();
}
bool RunStitchPanel::SaveLog(const wxString &filename)
{
return m_execPanel->SaveLog(filename);
};