/
sourceimage.cpp
157 lines (133 loc) · 3.62 KB
/
sourceimage.cpp
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
#include "sourceimage.h"
SourceImage::SourceImage(QWidget *parent) :
QWidget(parent)
{
}
void SourceImage::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if(!sourceImage.isNull())
painter.drawImage(0,0,sourceImage);
painter.setPen(Qt::red);
painter.drawPolygon(boundary);
}
void SourceImage::loadImage()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("select source"));
if(!fileName.isEmpty())
{
sourceImage.load(fileName);
qDebug()<<sourceImage.format();
}
}
void SourceImage::linkPoint(QPoint a, QPoint b)
{
QPoint del=b;
del-=a;
if(del.x()<=1&&del.x()>=-1
&&del.y()<=1&&del.y()>=-1)
return;
if(del.rx()>1)
del.rx()=1;
if(del.rx()<-1)
del.rx()=-1;
if(del.ry()>1)
del.ry()=1;
if(del.ry()<-1)
del.ry()=-1;
a+=del;
boundary<<a;
linkPoint(a,b);
}
void SourceImage::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() &Qt::LeftButton)
{
if(boundary.isEmpty())
{
boundary<<event->pos();
return;
}
QPoint oldpos = boundary.last();
QPoint newpos = event->pos();
qDebug()<<event->pos();
if(((oldpos.rx()-newpos.rx())<=1)
&&((oldpos.rx()-newpos.rx())>=-1)
&&((oldpos.ry()-newpos.ry())<=1)
&&((oldpos.ry()-newpos.ry())>=-1))
boundary<<newpos;
else
{
linkPoint(oldpos, newpos);
boundary<<newpos;
}
this->update();
}
}
void SourceImage::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
boundary.clear();
emit resetSub();
max_x=0;
max_y=0;
min_x=sourceImage.width();
min_y=sourceImage.height();
}
}
void SourceImage::mouseReleaseEvent(QMouseEvent *)
{
linkPoint(boundary.last(),boundary.first());
for(int i=0; i<boundary.count();i++)
{
if(boundary[i].rx()<min_x)
min_x=boundary[i].rx();
if(boundary[i].rx()>max_x)
max_x=boundary[i].rx();
if(boundary[i].ry()<min_y)
min_y=boundary[i].ry();
if(boundary[i].ry()>max_y)
max_y=boundary[i].ry();
}
QVector<QPoint> inner;
inner.resize(max_y-min_y+1);
for(int i=0;i<(max_y-min_y+1);i++ )
{
inner[i].rx()=-1;
inner[i].ry()=-1;
}
for(int i=0;i<boundary.count();i++)
{
if(inner[boundary[i].y()-min_y].x()<0)
inner[boundary[i].y()-min_y].rx()=inner[boundary[i].y()-min_y].ry()=boundary[i].x()-min_x;
else if(inner[boundary[i].y()-min_y].x()>(boundary[i].x()-min_x))
{
inner[boundary[i].y()-min_y].rx()=(boundary[i].x()-min_x);
}
else if(inner[boundary[i].y()-min_y].y()<(boundary[i].x()-min_x))
inner[boundary[i].y()-min_y].ry()=(boundary[i].x()-min_x);
}
QImage mask((max_x-min_x+1),(max_y-min_y+1),QImage::Format_Mono);
mask.fill(0);
for(int i=0; i<boundary.count();i++)
{
mask.setPixel(boundary[i].rx()-min_x,
boundary[i].ry()-min_y,1);
}
for(int i=0;i<=(max_y-min_y);i++)
{
int left=inner[i].x();
int right=inner[i].y();
while((++left)<right)
{
mask.setPixel(left,i,1);
}
}
maskImage = mask;
subImage = sourceImage.copy(min_x, min_y,
(max_x-min_x+1),
(max_y-min_y+1));
subImage = subImage.convertToFormat(QImage::Format_ARGB32);
subImage.setAlphaChannel(mask);
}