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

Download this file

MaskEdEditWnd.cpp    512 lines (464 with data), 16.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
// -*- c-basic-offset: 4 -*-
/** @file MaskEdEditWnd.cpp
*
* @author Fahim Mannan <fmannan@gmail.com>
*
* $Id$
*
* This 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
* Lesser 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/PreviewFrame.h"
#include "hugin/MainFrame.h"
#include "hugin/CommandHistory.h"
#include "hugin/config_defaults.h"
#include "hugin/huginApp.h"
//#include "hugin/ImageProcessing.h"
#include <vigra_ext/ROIImage.h>
#include <math.h>
#include "ISegmentation.h"
#include "MaskEdEditWnd.h"
#include "MaskMgr.h"
using namespace PT;
using namespace std;
using namespace vigra;
using namespace vigra_ext;
using namespace hugin_utils;
using HuginBase::ImageCache;
typedef RGBValue<unsigned char> BRGBValue;
BEGIN_EVENT_TABLE(MaskEdEditWnd, wxScrolledWindow)
EVT_LEFT_DOWN(MaskEdEditWnd::OnMouseButtonDown)
EVT_LEFT_UP(MaskEdEditWnd::OnLeftMouseButtonUp)
EVT_RIGHT_DOWN(MaskEdEditWnd::OnMouseButtonDown)
EVT_RIGHT_UP(MaskEdEditWnd::OnRightMouseButtonUp)
EVT_MOTION(MaskEdEditWnd::OnMotion)
EVT_PAINT(MaskEdEditWnd::OnPaint)
END_EVENT_TABLE()
MaskEdEditWnd::MaskEdEditWnd(wxWindow *parent,
wxWindowID winid,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString& name)
: wxScrolledWindow(parent, winid, pos, size, style, name), m_scale(1.0), m_active(0),
m_max_width(0), m_max_height(0), m_bShowOverlappedRect(false)
//m_state_rendering(false), m_rerender(false), m_imgsDirty(true)
{
MaskMgr::getInstance()->setSegmentationOption(0);
}
MaskEdEditWnd::~MaskEdEditWnd()
{
init();
m_pano->removeObserver(this);
}
void MaskEdEditWnd::init(PreviewFrame *parent, PT::Panorama * panorama )
{
m_pano = panorama;
m_parentWindow = parent;
m_pano->addObserver(this);
}
void MaskEdEditWnd::init()
{
m_bShowOverlappedRect = false;
m_bimgs.clear();
m_wximgs.clear();
m_imgfiles.clear();
m_selected.clear();
m_brushstroke.clear();
m_max_width = 0;
m_max_height = 0;
m_canvas_size.clear();
m_pos.clear();
/*m_state_rendering = false;
m_rerender = false;
m_imgsDirty = true;*/
}
void MaskEdEditWnd::setEditMode(MaskEdEditMode_t edmode)
{
m_edmode = edmode;
}
void MaskEdEditWnd::loadImage(const wxString &filename)
{
loadImage(string(filename.mb_str()));
}
void MaskEdEditWnd::loadImage(const string &filename)
{
if(filename != "")
{
ImageCache::EntryPtr e = ImageCache::getInstance().getImage(filename);
HuginBase::ImageCache::ImageCacheRGB8Ptr img = e->get8BitImage();
vigra::ImageImportInfo info(filename.c_str());
vigra::Size2D sz = info.getCanvasSize();
vigra::Diff2D pos = info.getPosition();
m_canvas_size.push_back(wxPoint(sz.width(), sz.height()));
m_pos.push_back(wxPoint(pos.x, pos.y));
m_selected.push_back(true);
m_max_width = sz.width() > m_max_width ? sz.width() : m_max_width;
m_max_height = sz.height() > m_max_height ? sz.height() : m_max_height;
if (img) {
m_max_width = img->width() > m_max_width ? img->width() : m_max_width;
m_max_height = img->height() > m_max_height ? img->height() : m_max_height;
wxImage img_temp(img->width(),
img->height(),
(unsigned char *) img->data(),
true);
m_wximgs.push_back(new wxImage(img->width(),
img->height(),
(unsigned char *) img->data(),
true));
wxBitmap *mask = MaskMgr::getInstance()->getSegmentation(filename)->getMaskBitmap();
img_temp.SetMaskFromImage(mask->ConvertToImage(), 0, 0, 0);
//img_temp.Rescale(img->width()*m_scale, img->height()*m_scale);
m_bimgs.push_back(new wxBitmap(img_temp));
}
SetScrollbars( 1, 1, m_max_width, m_max_height);
}
}
void MaskEdEditWnd::loadImage(const vector<string> &filesv)
{
init();
//remove_copy_if(filesv.begin(), filesv.end(), back_insert_iterator<vector<string> >(m_imgfiles), );
copy(filesv.begin(), filesv.end(), back_insert_iterator<vector<string> >(m_imgfiles));
//creates local cache of images
for(vector<string>::const_iterator it = filesv.begin(); it != filesv.end(); it++)
{
loadImage(*it);
}
}
void MaskEdEditWnd::loadImage(const string &imgId, const vigra::BRGBImage* img, vigra::BImage *alpha)
{
if(!img) return;
m_imgfiles.push_back(imgId);
m_canvas_size.push_back(wxPoint(img->width(), img->height()));
m_pos.push_back(wxPoint(0, 0));
m_selected.push_back(true);
m_max_width = img->width() > m_max_width ? img->width() : m_max_width;
m_max_height = img->height() > m_max_height ? img->height() : m_max_height;
wxImage img_temp(img->width(),
img->height(),
(unsigned char *) img->data(),
true);
m_wximgs.push_back(new wxImage(img->width(),
img->height(),
(unsigned char *) img->data(),
true));
//MaskMgr::getInstance()->loadImage(imgId, img, alpha);
wxBitmap *mask = MaskMgr::getInstance()->getSegmentation(imgId)->getMaskBitmap();
img_temp.SetMaskFromImage(mask->ConvertToImage(), 0, 0, 0);
//img_temp.SetAlpha((unsigned char*) alpha->data(), true);
//img_temp.Rescale(img->width()*m_scale, img->height()*m_scale);
m_bimgs.push_back(new wxBitmap(img_temp));
SetScrollbars( 1, 1, m_max_width, m_max_height);
}
void MaskEdEditWnd::loadImage(const std::vector<vigra::BRGBImage*> &imgs, std::vector<vigra::BImage*> &alphas)
{
init();
stringstream ss;
int i = 0;
vector<vigra::BImage*>::iterator it_alpha = alphas.begin();
for(vector<vigra::BRGBImage*>::const_iterator it = imgs.begin(); it != imgs.end(); it++, it_alpha++, i++)
{
ss << i;
loadImage(ss.str(), *it, *it_alpha);
ss.clear();
}
m_active = i - 1;
}
void MaskEdEditWnd::loadImage(std::vector<std::pair<vigra::BRGBImage*, vigra::BImage*> > &imgs)
{
init();
stringstream ss;
int i = 0;
for(vector<pair<vigra::BRGBImage*, vigra::BImage*> >::iterator it = imgs.begin(); it != imgs.end(); it++, i++)
{
ss << i;
loadImage(ss.str(), it->first, it->second);
ss.clear();
}
m_active = i - 1;
}
struct BRGBToBImage
{
public:
vigra::BImage::value_type operator() (const vigra::BRGBImage::value_type &p) const
{
return ((p.red() + p.green() + p.blue())/3) > 0 ? 255 : 0;
}
};
std::vector<vigra::BImage*> MaskEdEditWnd::getAlpha()
{
vector<vigra::BImage*> vec;
wxBitmap *mask;
for(vector<string>::iterator it = m_imgfiles.begin(); it != m_imgfiles.end(); it++)
{
mask = MaskMgr::getInstance()->getSegmentation(*it)->getMaskBitmap();
wxImage img = mask->ConvertToImage();
vigra::BasicImageView<RGBValue<unsigned char> > panoImg8((RGBValue<unsigned char> *)img.GetData(), img.GetWidth(), img.GetHeight());
vigra::BImage *bimg = new vigra::BImage(mask->GetWidth(), mask->GetHeight());
vigra::transformImage(srcImageRange(panoImg8), destImage(*bimg), BRGBToBImage());
vec.push_back(bimg);
}
return vec;
}
void MaskEdEditWnd::findOverlappingRect(int i, int j, wxRect &rect)
{
wxRect rectA, rectB;
//rectA is the leftmost rectangle
if(m_pos[i].x < m_pos[j].x) {
rectA = wxRect(m_pos[i], wxSize(m_bimgs[i]->GetWidth(), m_bimgs[i]->GetHeight()));
rectB = wxRect(m_pos[j], wxSize(m_bimgs[j]->GetWidth(), m_bimgs[j]->GetHeight()));
} else {
rectA = wxRect(m_pos[j], wxSize(m_bimgs[j]->GetWidth(), m_bimgs[j]->GetHeight()));
rectB = wxRect(m_pos[i], wxSize(m_bimgs[i]->GetWidth(), m_bimgs[i]->GetHeight()));
}
//set upper-left corner
rect.x = rectB.x;
if(rectB.y < rectA.y)
rect.y = rectA.y;
else
rect.y = rectB.y;
//set bottom-right corner
wxPoint brA = rectA.GetBottomRight();
wxPoint brB = rectB.GetBottomRight();
wxPoint br;
if(brB.y < brA.y) {
br.y = brB.y;
} else {
br.y = brA.y;
}
if(brB.x < brA.x)
br.x = brB.x;
else
br.x = brA.x;
rect.SetBottomRight(br);
}
void MaskEdEditWnd::ForceUpdate()
{
//updatePreview();
Refresh();
}
void MaskEdEditWnd::OnPaint(wxPaintEvent &event)
{
if(m_bimgs.empty()) return;
wxPaintDC dc(this);
//DoPrepareDC(dc);
int x,y;
x = GetScrollPos(wxSB_HORIZONTAL);
y = GetScrollPos(wxSB_VERTICAL);
int i = m_bimgs.size() - 1;
int lastDrawn = -1;
//dc.SetDeviceOrigin(-x, -y);
dc.SetUserScale(m_scale, m_scale);
UIntSet displayedImgs = m_pano->getActiveImages();
//for(vector<wxBitmap*>::reverse_iterator it = m_bimgs.rbegin(); it != m_bimgs.rend(); it++, i--)
for(UIntSet::iterator it = displayedImgs.begin(); it != displayedImgs.end(); it++)
{
//if(!m_selected[i]) continue;
i = *it;
dc.DrawBitmap(*m_bimgs[i], -x + m_pos[i].x, -y + m_pos[i].y, true);
if(m_bShowOverlappedRect && lastDrawn > -1) {
wxRect rect;
findOverlappingRect(lastDrawn, i, rect);
wxPen pen = dc.GetPen();
wxBrush brush = dc.GetBrush();
dc.SetPen(*wxRED);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRectangle(rect);
dc.SetPen(pen);
dc.SetBrush(brush);
}
lastDrawn = i;
//ignore this case for the timebeing
////drawing is not really needed here but just in case the window is refershed
////while the brushstroke is drawn
//if(m_brushstroke.pt.size() > 1)
//{
// std::vector<wxPoint>::iterator it = m_brushstroke.pt.begin();
// wxPoint pt = *it + wxPoint(-x,-y);
// wxPen pen(*wxRED, 1);
// dc.SetPen(pen);
// for(it++; it != m_brushstroke.pt.end(); it++)
// {
// dc.DrawLine(pt, *it + wxPoint(-x,-y));
// pt = *it + wxPoint(-x,-y);
// }
// dc.SetPen(wxNullPen);
//}
}
if(m_poly.pt.size() > 0) {
wxPoint *pts = new wxPoint[m_poly.pt.size()];
dc.SetPen(*wxRED);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
//copy(m_poly.pt.begin(), m_poly.pt.end(), pts);
int i = 0;
for(vector<PixelCoord>::iterator it = m_poly.pt.begin(); it != m_poly.pt.end(); it++,i++)
{
pts[i].x = it->x * m_scale - x + m_pos[m_active].x;
pts[i].y = it->y * m_scale - y + m_pos[m_active].y;
}
dc.DrawPolygon(m_poly.pt.size(), pts);
}
}
void MaskEdEditWnd::OnMouseButtonDown(wxMouseEvent &event)
{
m_brushstroke.clear();
event.Skip();
}
void MaskEdEditWnd::OnLeftMouseButtonUp(wxMouseEvent &event)
{
if(m_bimgs.empty() || m_active < 0) return;
if(m_edmode == ME_BSTROKE)
{
m_brushstroke.label = ISegmentation::FGND;
m_MaskEdCmdHist.addCommand(new BrushStrokeCmd(MaskMgr::getInstance(), m_brushstroke, m_active));
//reloadImages();
updateMask(m_active);
m_brushstroke.clear();
}
else if(m_edmode == ME_POLY) {
int x, y;
x = GetScrollPos(wxSB_HORIZONTAL);
y = GetScrollPos(wxSB_VERTICAL);
wxPoint pos = event.GetPosition()+wxPoint(x,y)-m_pos[m_active];
m_poly.add(PixelCoord(pos.x/m_scale, pos.y/m_scale));
}
event.Skip();
Refresh();
}
void MaskEdEditWnd::OnRightMouseButtonUp(wxMouseEvent &event)
{
if(m_bimgs.empty() || m_active < 0) return;
if(m_edmode == ME_BSTROKE)
{
m_brushstroke.label = ISegmentation::BKGND;
m_MaskEdCmdHist.addCommand(new BrushStrokeCmd(MaskMgr::getInstance(), m_brushstroke, m_active));
//reloadImages();
updateMask(m_active);
m_brushstroke.clear();
}
else if(m_edmode == ME_POLY) {
//end of creating polygon
m_poly.label = ISegmentation::FGND;
m_MaskEdCmdHist.addCommand(new PolygonCmd(MaskMgr::getInstance(), m_poly, m_active));
//reloadImages();
updateMask(m_active);
m_poly.clear();
}
event.Skip();
Refresh();
}
void MaskEdEditWnd::OnMotion(wxMouseEvent &event)
{
if(event.Dragging() && !event.MiddleIsDown() && m_bimgs.size() > 0 && m_edmode == ME_BSTROKE)
{
int x, y;
x = GetScrollPos(wxSB_HORIZONTAL);
y = GetScrollPos(wxSB_VERTICAL);
if(m_brushstroke.pt.size() > 0)
{
wxClientDC dc(this);
//DoPrepareDC(dc);
wxPen oldpen = dc.GetPen();
wxPen *pen;
if(event.LeftIsDown())
pen = new wxPen(*wxRED, 1);
else
pen = new wxPen(*wxBLUE, 1);
dc.SetPen(*pen);
wxPoint lastPt(m_brushstroke.pt.back().x, m_brushstroke.pt.back().y);
dc.DrawLine(lastPt - wxPoint(x, y) + m_pos[m_active], event.GetPosition());
dc.SetPen(oldpen);
}
wxPoint newPt = event.GetPosition() + wxPoint(x, y) - m_pos[m_active];
m_brushstroke.pt.push_back(PixelCoord(newPt.x, newPt.y));
}
}
//void MaskEdEditWnd::reloadImages()
//{
// m_bimgs.clear();
// for(vector<string>::iterator it = m_imgfiles.begin(); it != m_imgfiles.end(); it++)
// loadImage(*it);
//}
void MaskEdEditWnd::updateMask(int nimgId)
{
wxBitmap *mask = MaskMgr::getInstance()->getSegmentation(m_imgfiles[nimgId])->getMaskBitmap();
wxImage img = *m_wximgs[nimgId];
img.SetMaskFromImage(mask->ConvertToImage(), 0, 0, 0);
delete m_bimgs[nimgId];
m_bimgs[nimgId] = new wxBitmap(img);
}
void MaskEdEditWnd::updateMask(string &imgId)
{
//wxBitmap *mask = MaskMgr::getInstance()->getSegmentation(imgId)->getMaskBitmap();
vector<string>::iterator it = find(m_imgfiles.begin(), m_imgfiles.end(), imgId);
if(it != m_imgfiles.end())
{
int index = it - m_imgfiles.begin();
updateMask(index);
/*wxImage img = *m_wximgs[index];
img.SetMaskFromImage(mask->ConvertToImage(), 0, 0, 0);
delete m_bimgs[index];
m_bimgs[index] = new wxBitmap(img);*/
}
}
void MaskEdEditWnd::zoom(float scale, wxRect region)
{
m_scale = scale;
//reloadImages();
//SetScrollbars( 1, 1, m_bimgs[0]->GetWidth()*m_scale, m_bimgs[0]->GetHeight()*m_scale);
Refresh();
}
float MaskEdEditWnd::getZoomLevel() const
{
return m_scale;
}
void MaskEdEditWnd::setDisplayImages(const std::map<int, std::pair<std::string, bool> > &selection)
{
}
void MaskEdEditWnd::setSelectedImage(int index, bool state)
{
m_selected[index] = state;
this->Refresh();
}
void MaskEdEditWnd::toggleShowOverlappedRect()
{
m_bShowOverlappedRect = m_bShowOverlappedRect ? false : true;
Refresh();
}
void MaskEdEditWnd::undo()
{
m_MaskEdCmdHist.undo();
}
void MaskEdEditWnd::redo()
{
m_MaskEdCmdHist.redo();
}
void MaskEdEditWnd::panoramaImagesChanged(PT::Panorama &pano, const PT::UIntSet & imgNr)
{
Refresh();
UIntSet activeImgs = pano.getActiveImages();
if(activeImgs.empty()) {
m_active = -1;
return;
}
m_active = *activeImgs.rbegin();
}