[b5bf61]: src / hugin1 / hugin / PreviewPanel.cpp Maximize Restore History

Download this file

PreviewPanel.cpp    887 lines (756 with data), 28.7 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
// -*- c-basic-offset: 4 -*-
/** @file PreviewPanel.cpp
*
* @brief implementation of PreviewPanel Class
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* 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 <config.h>
#include "panoinc_WX.h"
#include "panoinc.h"
#include <vigra/basicimageview.hxx>
#include "vigra_ext/blend.h"
#include "PT/Stitcher.h"
#include "base_wx/ImageCache.h"
#include "hugin/PreviewPanel.h"
#include "hugin/PreviewFrame.h"
#include "hugin/MainFrame.h"
#include "hugin/CommandHistory.h"
#include "hugin/config_defaults.h"
#include "hugin/huginApp.h"
//#include "hugin/ImageProcessing.h"
#include <math.h>
using namespace PT;
using namespace std;
using namespace vigra;
using namespace vigra_ext;
using namespace hugin_utils;
typedef RGBValue<unsigned char> BRGBValue;
BEGIN_EVENT_TABLE(PreviewPanel, wxPanel)
// EVT_PAINT(CPImageCtrl::OnPaint)
// EVT_LEFT_DOWN(CPImageCtrl::mousePressEvent)
// EVT_MOTION(CPImageCtrl::mouseMoveEvent)
// EVT_LEFT_UP(CPImageCtrl::mouseReleaseEvent)
EVT_SIZE(PreviewPanel::OnResize)
EVT_LEFT_DOWN(PreviewPanel::mousePressLMBEvent)
EVT_RIGHT_DOWN(PreviewPanel::mousePressRMBEvent)
EVT_MOUSE_EVENTS ( PreviewPanel::OnMouse )
EVT_PAINT ( PreviewPanel::OnDraw )
END_EVENT_TABLE()
PreviewPanel::PreviewPanel()
: pano(0), m_autoPreview(false),m_panoImgSize(1,1),
m_panoBitmap(0),
m_pano2erect(0), m_blendMode(BLEND_COPY),
m_state_rendering(false), m_rerender(false), m_imgsDirty(true)
{
}
bool PreviewPanel::Create(wxWindow* parent, wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString& name)
{
DEBUG_TRACE(" Create called *************");
if (! wxPanel::Create(parent, id, pos, size, style, name) ) {
return false;
}
DEBUG_TRACE("");
DEBUG_DEBUG("m_state_rendering = " << m_state_rendering);
#if defined(__WXMSW__)
wxString cursorPath = huginApp::Get()->GetXRCPath() + wxT("/data/cursor_cp_pick.cur");
m_cursor = new wxCursor(cursorPath, wxBITMAP_TYPE_CUR);
#else
m_cursor = new wxCursor(wxCURSOR_CROSS);
#endif
SetCursor(*m_cursor);
return true;
}
void PreviewPanel::Init(PreviewFrame *parent, PT::Panorama * panorama )
{
pano = panorama;
parentWindow = parent;
pano->addObserver(this);
m_alphas.clear();
}
PreviewPanel::~PreviewPanel()
{
DEBUG_TRACE("dtor");
delete m_cursor;
pano->removeObserver(this);
if (m_panoBitmap) {
delete m_panoBitmap;
}
if (m_pano2erect) {
delete m_pano2erect;
}
delete & vigra_ext::ThreadManager::get();
DEBUG_TRACE("dtor end");
}
void PreviewPanel::panoramaChanged(Panorama &pano)
{
// avoid recursive calls.. don't know if they can happen at all,
// but they might lead to crashes.
bool dirty = false;
const PanoramaOptions & newOpts = pano.getOptions();
// check if an important options has been changed
if (newOpts.getHFOV() != opts.getHFOV()) {
DEBUG_DEBUG("HFOV changed");
dirty = true;
}
if (newOpts.getVFOV() != opts.getVFOV()) {
DEBUG_DEBUG("VFOV changed");
dirty = true;
}
if (newOpts.getProjection() != opts.getProjection()) {
DEBUG_DEBUG("projection changed");
dirty = true;
}
opts = newOpts;
if (dirty) {
// have to remap all images
m_remapCache.invalidate();
}
if (m_autoPreview && dirty) {
DEBUG_DEBUG("forcing preview update");
ForceUpdate();
// resize
} else if(m_autoPreview && m_imgsDirty ) {
DEBUG_DEBUG("updating preview after image change");
updatePreview();
m_imgsDirty=false;
}
}
void PreviewPanel::panoramaImagesChanged(Panorama &pano, const UIntSet &changed)
{
DEBUG_TRACE("");
/*
for(PT::UIntSet::const_iterator it = changed.begin(); it != changed.end();
++it)
{
// TODO: need to check if just the active flag changed and skip invalidate in that case
if (pano->getSrcImage(*it) != m_remapCache.getSrcDescription()) {
m_remapCache.invalidate(*it);
}
}
*/
m_imgsDirty = true;
/*
if (m_autoPreview) {
DEBUG_DEBUG("updating preview after image change");
updatePreview();
}
*/
}
/*
void PreviewPanel::SetDisplayedImages(const UIntSet & imgs)
{
m_displayedImages = imgs;
if (m_autoPreview) {
updatePreview();
}
}
*/
void PreviewPanel::SetBlendMode(BlendMode b)
{
m_blendMode = b;
updatePreview();
}
void PreviewPanel::ForceUpdate()
{
updatePreview();
}
void PreviewPanel::SetAutoUpdate(bool enabled)
{
m_autoPreview = enabled;
if (enabled) {
updatePreview();
}
}
vector<pair<vigra::BRGBImage*, vigra::BImage*> > PreviewPanel::getRemappedImages()
{
vector<pair<vigra::BRGBImage*, vigra::BImage*> > vec;
UIntSet set;
for(int i = 0; i < pano->getNrOfImages(); i++)
{
set.insert(i);
vec.push_back(mapPreviewImage(set, NULL, i));
set.erase(i);
}
return vec;
}
void PreviewPanel::setAlpha(std::vector<vigra::BImage*> alphas)
{
m_alphas.clear();
copy(alphas.begin(), alphas.end(), back_inserter<vector<vigra::BImage*> >(m_alphas));
}
void PreviewPanel::freeAlpha()
{
m_alphas.clear();
}
/** just apply exposure and response to linear data
*/
template <class OP>
struct ExposureResponseFunctor2
{
ExposureResponseFunctor2(double exposure, const OP & operation)
: op(operation), e(exposure)
{
}
const OP & op;
double e;
template <class VT>
typename vigra::NumericTraits<VT>::RealPromote
operator()(VT v) const
{
return op(v*e);
}
};
pair<vigra::BRGBImage*,vigra::BImage*> PreviewPanel::mapPreviewImage(UIntSet &displayedImages, vigra::Diff2D *p_panoImgSize, int index)
{
double finalWidth = pano->getOptions().getWidth();
double finalHeight = pano->getOptions().getHeight();
vigra::Diff2D panoImgSize = Diff2D(GetClientSize().GetWidth(), GetClientSize().GetHeight());
double ratioPano = finalWidth / finalHeight;
double ratioPanel = (double)panoImgSize.x / (double)panoImgSize.y;
DEBUG_DEBUG("panorama ratio: " << ratioPano << " panel ratio: " << ratioPanel);
if (ratioPano < ratioPanel) {
// panel is wider than pano
panoImgSize.x = ((int) (panoImgSize.y * ratioPano));
DEBUG_DEBUG("portrait: " << panoImgSize);
} else {
// panel is taller than pano
panoImgSize.y = ((int)(panoImgSize.x / ratioPano));
DEBUG_DEBUG("landscape: " << panoImgSize);
}
PanoramaOptions opts = pano->getOptions();
opts.setWidth(panoImgSize.x, false);
opts.setHeight(panoImgSize.y);
//m_panoImgSize.y = opts.getHeight();
// always use bilinear for preview.
// reset ROI. The preview needs to draw the parts outside the ROI, too!
opts.setROI(Rect2D(opts.getSize()));
opts.interpolator = vigra_ext::INTERP_BILINEAR;
vigra::BRGBImage *panoImg8 = new vigra::BRGBImage(panoImgSize);
vigra::BImage *alpha = NULL;
if(!p_panoImgSize) {
alpha = new vigra::BImage(panoImgSize);
} else {
if(index == -1 || m_alphas.size() <= index) {
alpha = new vigra::BImage(panoImgSize);
m_alphas.push_back(alpha);
} else {
alpha = m_alphas[index];
}
}
assert(alpha != NULL);
try {
//vigra::BasicImageView<RGBValue<unsigned char> > *panoImg8((RGBValue<unsigned char> *)panoImage.GetData(), panoImage.GetWidth(), panoImage.GetHeight());
FRGBImage panoImg(panoImgSize);
// the empty panorama roi
// Rect2D panoROI;
DEBUG_DEBUG("about to stitch images, pano size: " << panoImgSize);
if (displayedImages.size() > 0) {
if (opts.outputMode == PanoramaOptions::OUTPUT_HDR) {
DEBUG_DEBUG("HDR output merge");
ReduceToHDRFunctor<RGBValue<float> > hdrmerge;
ReduceStitcher<FRGBImage, BImage> stitcher(*pano, *parentWindow);
stitcher.stitch(opts, displayedImages,
destImageRange(panoImg), destImage(*alpha),
m_remapCache,
hdrmerge);
/*
std::vector<RemappedPanoImage<FRGBImage, BImage> *> remapped;
// get all remapped images
for (UIntSet::const_iterator it = displayedImages.begin();
it != displayedImages.end(); ++it)
{
remapped.push_back(m_remapCache.getRemapped(pano, opts, *it, *parentWindow));
}
reduceROIImages(remapped,
destImageRange(panoImg), destImage(*alpha),
hdrmerge);
*/
#ifdef DEBUG_REMAP
{
vigra::ImageExportInfo exi( DEBUG_FILE_PREFIX "hugin04_preview_HDR_Reduce.tif"); \
vigra::exportImage(vigra::srcImageRange(panoImg), exi); \
}
{
vigra::ImageExportInfo exi(DEBUG_FILE_PREFIX "hugin04_preview_HDR_Reduce_*alpha.tif"); \
vigra::exportImage(vigra::srcImageRange(*alpha), exi); \
}
#endif
// find min and max
vigra::FindMinMax<float> minmax; // init functor
vigra::inspectImageIf(srcImageRange(panoImg), srcImage(*alpha),
minmax);
double min = std::max(minmax.min, 1e-6f);
double max = minmax.max;
#if 0
for (int i=0; i<3; i++) {
if (minmax.min[i]> 1e-6 && minmax.min[i] < min)
min = minmax.min[i];
}
double max = DBL_MIN;
for (int i=0; i<3; i++) {
if (minmax.max[i]> 1e-6 && minmax.max[i] > max)
max = minmax.max[i];
}
#endif
int mapping = wxConfigBase::Get()->Read(wxT("/ImageCache/MappingFloat"), HUGIN_IMGCACHE_MAPPING_FLOAT);
applyMapping(srcImageRange(panoImg), destImage(*panoImg8), min, max, mapping);
} else {
// LDR output
// FileRemapper<BRGBImage, BImage> m;
switch (m_blendMode) {
case BLEND_COPY:
{
StackingBlender blender;
// SimpleStitcher<BRGBImage, BImage> stitcher(pano, *(MainFrame::Get()));
SimpleStitcher<FRGBImage, BImage> stitcher(*pano, *parentWindow);
stitcher.stitch(opts, displayedImages,
destImageRange(panoImg), destImage(*alpha),
m_remapCache,
blender);
break;
}
case BLEND_DIFFERENCE:
{
ReduceToDifferenceFunctor<RGBValue<float> > func;
ReduceStitcher<FRGBImage, BImage> stitcher(*pano, *parentWindow);
stitcher.stitch(opts, displayedImages,
destImageRange(panoImg), destImage(*alpha),
m_remapCache,
func);
break;
/*
WeightedStitcher<BRGBImage, BImage> stitcher(pano, *(MainFrame::Get()));
stitcher.stitch(opts, m_displayedImages,
destImageRange(panoImg), destImage(*alpha),
m_remapCache);
break;
*/
}
/*
case BLEND_DIFFERENCE:
{
DifferenceBlender blender;
SimpleStitcher<BRGBImage, BImage> stitcher(pano, *(MainFrame::Get()));
stitcher.stitch(opts, m_displayedImages,
destImageRange(panoImg), destImage(*alpha),
m_remapCache,
blender);
break;
}
*/
}
#ifdef DEBUG_REMAP
{
vigra::ImageExportInfo exi( DEBUG_FILE_PREFIX "hugin04_preview_AfterRemap.tif"); \
vigra::exportImage(vigra::srcImageRange(panoImg), exi); \
}
{
vigra::ImageExportInfo exi(DEBUG_FILE_PREFIX "hugin04_preview_AfterRemapalpha.tif"); \
vigra::exportImage(vigra::srcImageRange(*alpha), exi); \
}
#endif
// apply default exposure and convert to 8 bit
SrcPanoImage src = pano->getSrcImage(0);
// apply the exposure
double scale = 1.0/pow(2.0,opts.outputExposureValue);
vigra::transformImage(srcImageRange(panoImg), destImage(panoImg),
vigra::functor::Arg1()*vigra::functor::Param(scale));
DEBUG_DEBUG("LDR output, with response: " << src.getResponseType());
if (src.getResponseType() == SrcPanoImage::RESPONSE_LINEAR) {
vigra::copyImage(srcImageRange(panoImg), destImage(*panoImg8));
// vigra::transformImage(srcImageRange(panoImg), destImage(*panoImg8),
// vigra::functor::Arg1()*vigra::functor::Param(255));
} else {
// create suitable lut for response
typedef std::vector<double> LUT;
LUT lut;
switch(src.getResponseType())
{
case SrcPanoImage::RESPONSE_EMOR:
EMoR::createEMoRLUT(src.getEMoRParams(), lut);
break;
case SrcPanoImage::RESPONSE_GAMMA:
lut.resize(256);
createGammaLUT(1/src.getGamma(), lut);
break;
default:
vigra_fail("Unknown or unsupported response function type");
break;
}
// scale lut
for (size_t i=0; i < lut.size(); i++)
lut[i] = lut[i]*255;
typedef vigra::RGBValue<float> FRGB;
LUTFunctor<FRGB, LUT> lutf(lut);
vigra::transformImage(srcImageRange(panoImg), destImage(*panoImg8),
lutf);
}
}
}
#ifdef DEBUG_REMAP
{
vigra::ImageExportInfo exi( DEBUG_FILE_PREFIX "hugin05_preview_final.tif"); \
vigra::exportImage(vigra::srcImageRange(*panoImg8), exi); \
}
#endif
} catch (std::exception & e) {
m_state_rendering = false;
DEBUG_ERROR("error during stitching: " << e.what());
wxMessageBox(wxString(e.what(), wxConvLocal), _("Error during Stitching"));
}
if(p_panoImgSize)
*p_panoImgSize = panoImgSize;
return make_pair(panoImg8, alpha);
}
void PreviewPanel::updatePreview()
{
DEBUG_TRACE("");
// we can accidentally end up here recursively, because wxWidgets
// allows user input during redraw of the progress in the bottom
if (m_state_rendering) {
DEBUG_DEBUG("m_state_rendering == true, aborting rendering");
m_rerender = true;
return;
}
DEBUG_DEBUG("m_state_rendering = true");
m_state_rendering = true;
m_rerender = false;
long nthreads = wxConfigBase::Get()->Read(wxT("/Nona/NumberOfThreads"), wxThread::GetCPUCount());
if (nthreads < 1) nthreads = 1;
vigra_ext::ThreadManager::get().setNThreads(nthreads);
{
// Even though the frame is hidden, the panel is not
// so check the parent instead
if (parentWindow) {
if (parentWindow->IsShown() && (! parentWindow->IsIconized())) {
DEBUG_INFO("Parent window shown - updating");
} else {
DEBUG_INFO("Parent window hidden - not updating");
m_state_rendering = false;
return;
}
}
}
// bool seaming = wxConfigBase::Get()->Read("/PreviewPanel/UseSeaming",0l) != 0;
// temporary bitmap for our remapped image
// calculate the image size from panel widht, height from vfov
// long cor = wxConfigBase::Get()->Read("/PreviewPanel/correctDistortion",0l);
// bool corrLens = cor != 0;
wxBusyCursor wait;
pair<vigra::BRGBImage*, vigra::BImage*> panoImg8_alpha;
HuginBase::UIntSet activeImgs = pano->getActiveImages();
panoImg8_alpha = mapPreviewImage(activeImgs, &m_panoImgSize);
vigra::BRGBImage *panoImg8 = panoImg8_alpha.first;
wxImage panoImage(panoImg8->width(), panoImg8->height(),
(unsigned char *) panoImg8->data(), true);
//delete panoImg8_alpha.first;
//delete panoImg8_alpha.second;
// update the transform for pano -> erect coordinates
if (m_pano2erect) delete m_pano2erect;
SrcPanoImage src;
src.setProjection(SrcPanoImage::EQUIRECTANGULAR);
src.setHFOV(360);
src.setSize(Size2D(360,180));
m_pano2erect = new PTools::Transform;
m_pano2erect->createTransform(src, opts);
if (m_panoBitmap) {
delete m_panoBitmap;
}
m_panoBitmap = new wxBitmap(panoImage);
// always redraw
wxClientDC dc(this);
DrawPreview(dc);
m_state_rendering = false;
DEBUG_DEBUG("m_state_rendering = false");
m_rerender = false;
}
void PreviewPanel::DrawPreview(wxDC & dc)
{
if (!IsShown()){
return;
}
DEBUG_TRACE("");
// bool drawOutlines = wxConfigBase::Get()->Read("/PreviewPanel/drawOutlines",1l) != 0;
int offsetX = 0;
int offsetY = 0;
wxSize sz = GetClientSize();
if (sz.GetWidth() > m_panoImgSize.x) {
offsetX = (sz.GetWidth() - m_panoImgSize.x) / 2;
}
if (sz.GetHeight() > m_panoImgSize.y) {
offsetY = (sz.GetHeight() - m_panoImgSize.y) / 2;
}
dc.SetPen(wxPen(GetBackgroundColour(),1,wxSOLID));
dc.SetBrush(wxBrush(GetBackgroundColour(),wxSOLID));
dc.DrawRectangle(0, 0, offsetX, sz.GetHeight());
dc.DrawRectangle(offsetX, 0, sz.GetWidth(), offsetY);
dc.DrawRectangle(offsetX, sz.GetHeight() - offsetY,
sz.GetWidth(), sz.GetHeight());
dc.DrawRectangle(sz.GetWidth() - offsetX, offsetY,
sz.GetWidth(), sz.GetHeight() - offsetY);
// set a clip region to draw stuff accordingly
dc.DestroyClippingRegion();
dc.SetClippingRegion(offsetX, offsetY,
m_panoImgSize.x, m_panoImgSize.y);
dc.SetPen(wxPen(wxT("BLACK"),1,wxSOLID));
dc.SetBrush(wxBrush(wxT("BLACK"),wxSOLID));
dc.DrawRectangle(offsetX, offsetY, m_panoImgSize.x, m_panoImgSize.y);
wxCoord w = m_panoImgSize.x;
wxCoord h = m_panoImgSize.y;
// draw panorama image
if (m_panoBitmap) {
dc.DrawBitmap(*m_panoBitmap, offsetX, offsetY);
// draw ROI
Size2D panoSize = pano->getOptions().getSize();
Rect2D panoROI = pano->getOptions().getROI();
if (panoROI != vigra::Rect2D(panoSize)) {
double scale = min(w/(float)panoSize.x, h/(float)panoSize.y);
Rect2D previewROI = Rect2D(panoROI.upperLeft()* scale, panoROI.lowerRight() * scale);
Rect2D screenROI = previewROI;
screenROI.moveBy(offsetX, offsetY);
// TODO: make areas outside ROI darker than the rest of the image
// overdraw areas outside the ROI with some black half transparent
// bitmap
// it seems to be quite complicated to create a half transparent black image...
wxImage blackImg(w,h);
// init alpha channel
if (!blackImg.HasAlpha()) {
blackImg.InitAlpha();
}
unsigned char * aptr = blackImg.GetAlpha();
unsigned char * aend = aptr + w*h;
for (; aptr != aend; ++aptr)
*aptr = 128;
wxBitmap blackBitmap(blackImg);
// left
if (screenROI.left() > offsetX) {
dc.DestroyClippingRegion();
dc.SetClippingRegion(offsetX, offsetY,
previewROI.left(), h);
dc.DrawBitmap(blackBitmap, offsetX, offsetY);
}
// top
if (screenROI.top() > offsetY ) {
dc.DestroyClippingRegion();
dc.SetClippingRegion(screenROI.left(), offsetY,
previewROI.width(), previewROI.top());
dc.DrawBitmap(blackBitmap, offsetX, offsetY);
}
// right
if (screenROI.right() < offsetX + w) {
dc.DestroyClippingRegion();
dc.SetClippingRegion(screenROI.right(), offsetY,
w - previewROI.right(), h);
dc.DrawBitmap(blackBitmap, offsetX, offsetY);
}
// bottom
if (screenROI.bottom() < offsetY + h ) {
dc.DestroyClippingRegion();
dc.SetClippingRegion(screenROI.left(), screenROI.bottom(),
screenROI.width(), h - previewROI.bottom());
dc.DrawBitmap(blackBitmap, offsetX, offsetY);
}
// reset clipping region
dc.DestroyClippingRegion();
dc.SetClippingRegion(offsetX, offsetY,
m_panoImgSize.x, m_panoImgSize.y);
// draw boundaries
dc.SetPen(wxPen(wxT("WHITE"), 1, wxSOLID));
dc.SetLogicalFunction(wxINVERT);
DEBUG_DEBUG("ROI scale factor: " << scale << " screen ROI: " << screenROI);
dc.DrawLine(screenROI.left(),screenROI.top(),
screenROI.right(),screenROI.top());
dc.DrawLine(screenROI.right(),screenROI.top(),
screenROI.right(),screenROI.bottom());
dc.DrawLine(screenROI.right(),screenROI.bottom(),
screenROI.left(),screenROI.bottom());
dc.DrawLine(screenROI.left(),screenROI.bottom(),
screenROI.left(),screenROI.top());
}
}
#if 0
// currently disabled
if (drawOutlines) {
for (UIntSet::iterator it = m_displayedImages.begin();
it != m_displayedImages.end();
++it)
{
dc.SetPen(wxPen(wxT("GREY"), 1, wxSOLID));
DrawOutline(m_remapped[*it]->getOutline(), dc, offsetX, offsetY);
}
}
#endif
dc.DestroyClippingRegion();
dc.SetClippingRegion(offsetX, offsetY,
m_panoImgSize.x, m_panoImgSize.y);
// draw center lines over display
dc.SetPen(wxPen(wxT("WHITE"), 1, wxSOLID));
dc.SetLogicalFunction(wxINVERT);
dc.DrawLine(offsetX + w/2, offsetY,
offsetX + w/2, offsetY + h);
dc.DrawLine(offsetX, offsetY + h/2,
offsetX + w, offsetY+ h/2);
}
void PreviewPanel::OnDraw(wxPaintEvent & event)
{
wxPaintDC dc(this);
DrawPreview(dc);
}
void PreviewPanel::OnResize(wxSizeEvent & e)
{
DEBUG_TRACE("");
wxSize sz = GetClientSize();
if (sz.GetWidth() != m_panoImgSize.x && sz.GetHeight() != m_panoImgSize.y) {
m_remapCache.invalidate();
if (m_autoPreview) {
ForceUpdate();
}
}
}
void PreviewPanel::mousePressLMBEvent(wxMouseEvent & e)
{
DEBUG_DEBUG("mousePressLMBEvent: " << e.m_x << "x" << e.m_y);
double yaw, pitch;
mouse2erect(e.m_x, e.m_y, yaw, pitch);
// calculate new rotation angles.
Matrix3 rotY;
rotY.SetRotationPT(DEG_TO_RAD(-yaw), 0, 0);
Matrix3 rotP;
rotP.SetRotationPT(0, DEG_TO_RAD(pitch), 0);
double y,p,r;
Matrix3 rot = rotP * rotY;
rot.GetRotationPT(y,p,r);
y = RAD_TO_DEG(y);
p = RAD_TO_DEG(p);
r = RAD_TO_DEG(r);
DEBUG_DEBUG("rotation angles pitch*yaw: " << y << " " << p << " " << r);
GlobalCmdHist::getInstance().addCommand(
new PT::RotatePanoCmd(*pano, y, p, r)
);
if (!m_autoPreview) {
ForceUpdate();
}
}
void PreviewPanel::mousePressRMBEvent(wxMouseEvent & e)
{
DEBUG_DEBUG("mousePressRMBEvent: " << e.m_x << "x" << e.m_y);
double yaw, pitch;
mouse2erect(e.m_x, e.m_y, yaw, pitch);
// theta, phi: spherical coordinates in mathworld notation.
double theta = DEG_TO_RAD(yaw);
double phi = DEG_TO_RAD(90+pitch);
// convert to cartesian coordinates.
double x = cos(theta)* sin(phi);
double y = sin(theta)* sin(phi);
double z = cos(phi);
DEBUG_DEBUG("theta: " << theta << " phi: " << phi << " x y z:" << x << " " << y << " " << z);
double roll = RAD_TO_DEG(atan(z/y));
DEBUG_DEBUG("roll correction: " << roll);
GlobalCmdHist::getInstance().addCommand(
new PT::RotatePanoCmd(*pano, 0, 0, roll)
);
if (!m_autoPreview) {
ForceUpdate();
}
}
void PreviewPanel::OnMouse(wxMouseEvent & e)
{
double yaw, pitch;
mouse2erect(e.m_x, e.m_y, yaw, pitch);
/*
wxSize sz = GetClientSize();
int offsetX = 0;
int offsetY = 0;
if (sz.GetWidth() > m_panoImgSize.x) {
offsetX = (sz.GetWidth() - m_panoImgSize.x) / 2;
}
if (sz.GetHeight() > m_panoImgSize.y) {
offsetY = (sz.GetHeight() - m_panoImgSize.y) / 2;
}
double x = e.m_x - offsetX - m_panoImgSize.x/2;
double y = e.m_y - offsetY - m_panoImgSize.y/2;
int w = pano->getOptions().getWidth();
double scale = w/(double)m_panoImgSize.x;
x *= scale;
y *= scale;
*/
parentWindow->SetStatusText(_("Left click to define new center point, right click to move point to horizon."),0);
parentWindow->SetStatusText(wxString::Format(wxT("%.1f %.1f"), yaw, pitch), 1);
}
void PreviewPanel::mouse2erect(int xm, int ym, double &xd, double & yd)
{
int offsetX=0, offsetY=0;
if (m_pano2erect) {
wxSize sz = GetClientSize();
if (sz.GetWidth() > m_panoImgSize.x) {
offsetX = (sz.GetWidth() - m_panoImgSize.x) / 2;
}
if (sz.GetHeight() > m_panoImgSize.y) {
offsetY = (sz.GetHeight() - m_panoImgSize.y) / 2;
}
double x = xm - offsetX - m_panoImgSize.x/2;
double y = ym - offsetY - m_panoImgSize.y/2;
m_pano2erect->transform(xd, yd, x, y);
//DEBUG_DEBUG("pano: " << x << " " << y << " erect: " << xd << "�� " << yd << "��");
}
}
void PreviewPanel::DrawOutline(const vector<FDiff2D> & points, wxDC & dc, int offX, int offY)
{
for (vector<FDiff2D>::const_iterator pnt = points.begin();
pnt != points.end() ;
++pnt)
{
Diff2D point = pnt->toDiff2D();
if (point.x < 0) point.x = 0;
if (point.y < 0) point.y = 0;
if (point.x >= m_panoImgSize.x)
point.x = m_panoImgSize.y-1;
if (point.y >= m_panoImgSize.y)
point.y = m_panoImgSize.y -1;
dc.DrawPoint(roundi(offX + point.x), roundi(offY + point.y));
}
}
IMPLEMENT_DYNAMIC_CLASS(PreviewPanel, wxPanel)
PreviewPanelXmlHandler::PreviewPanelXmlHandler()
: wxXmlResourceHandler()
{
AddWindowStyles();
}
wxObject *PreviewPanelXmlHandler::DoCreateResource()
{
XRC_MAKE_INSTANCE(cp, PreviewPanel)
cp->Create(m_parentAsWindow,
GetID(),
GetPosition(), GetSize(),
GetStyle(wxT("style")),
GetName());
SetupWindow( cp);
return cp;
}
bool PreviewPanelXmlHandler::CanHandle(wxXmlNode *node)
{
return IsOfClass(node, wxT("PreviewPanel"));
}
IMPLEMENT_DYNAMIC_CLASS(PreviewPanelXmlHandler, wxXmlResourceHandler)