-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscribblearea.cpp
361 lines (283 loc) · 7.37 KB
/
scribblearea.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
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
#include <QMouseEvent>
#include <QPen>
#include <QPainter>
#include <QFileDialog>
#include <QLayout>
#include <fstream>
#include <iostream>
#include "scribblearea.h"
using namespace std;
Vec3f getRgbOfLabel(int label)
{
switch(label)
{
case 0:
return Vec3f(1,0,0);
case 1:
return Vec3f(0,1,0);
case 2:
return Vec3f(0,0,1);
case 3:
return Vec3f(0,1,1);
case 4:
return Vec3f(1,1,1);
case 5:
return Vec3f(0.5,0.5,0.5);
case 7:
return Vec3f(0.5,1,1);
case 8:
return Vec3f(1,1,0.5);
case 9:
return Vec3f(1,0.5,1);
}
return Vec3f(0,0,0);
}
ScribbleArea::ScribbleArea(QWidget *parent)
: QGraphicsView(parent)
{
scribbling = false;
readOnly = false;
scribbles = new int*[1000];
scribbles_orig = new int*[1000];
for(int i =0; i < 1000; i++)
{
scribbles[i] = new int[1000];
scribbles_orig[i] = new int[1000];
}
brushSize=10;
currLabel = 0;
clearScribbles();
}
void ScribbleArea::setLabel(int x, int y, int label)
{
if(readOnly) return;
for (int i = x-brushSize/2.0; i <= x+((brushSize/2.0));i++)
for (int j = y-brushSize/2.0; j <= y+((brushSize/2.0));j++)
{
if (i >= back.size().width() || j >= back.size().height() || i < 0 || j<0)
continue;
scribbles[i][j] = label;
}
scribbles_orig[x][y] = label;
}
void ScribbleArea::dumpImageWithScribbles()
{
/*
QImage im(back);
for(int i =0; i < 1000; i++)
for(int j =0; j < 1000; j++)
if(scribbles[i][j]> -1)
{
im.setPixel(i,j,getRgbOfLabel(scribbles[i][j]).rgb());
}
im.save("dump_left.png");
*/
}
void ScribbleArea::setLabel(int x, int y, int label, int brushSize)
{
if(readOnly) return;
scribbles_orig[x][y] = label;
if(brushSize==1)
{
scribbles[x][y] = label;
return;
}
for (int i = x-brushSize/2.0; i <= x+((brushSize/2.0));i++)
for (int j = y-brushSize/2.0; j <= y+((brushSize/2.0));j++)
{
if (i >= back.size().width() || j >= back.size().height() || i < 0 || j<0)
continue;
scribbles[i][j] = label;
}
}
void ScribbleArea::setIcgLabel(int x, int y, int label, int brushSize, Mat >)
{
if(readOnly) return;
scribbles_orig[x][y] = label;
if(brushSize==1)
{
scribbles[x][y] = label;
return;
}
for (int i = x-brushSize/2.0; i <= x+((brushSize/2.0));i++)
for (int j = y-brushSize/2.0; j <= y+((brushSize/2.0));j++)
{
if (i >= back.size().width() || j >= back.size().height() || i < 0 || j<0)
continue;
if(gt.at<int>(i,j) != label) continue;
scribbles[i][j] = label;
}
}
void ScribbleArea::removeLastLabel()
{
if(readOnly) return;
for (int i = lastClick.x()-brushSize/2.0; i <= lastClick.x()+((brushSize/2.0));i++)
for (int j = lastClick.y()-brushSize/2.0; j <= lastClick.y()+((brushSize/2.0));j++)
{
if (i >= back.size().width() || j >= back.size().height() || i < 0 || j<0)
continue;
scribbles[i][j] = -1;
scribbles_orig[i][j] = -1;
}
}
void ScribbleArea::mousePressEvent(QMouseEvent *event)
{
scribbling = true;
if (event->buttons() == Qt::LeftButton && scribbling)
{
if(readOnly)
{
tool->printPixelInformation(event->x(),event->y());
return;
}
setLabel(event->x(),event->y(),currLabel);
lastClick.setX(event->x());
lastClick.setY(event->y());
tool->ColorDone=false;
tool->TextureDone = false;
}
else if(event->buttons() == Qt::RightButton)
{
//removeLastLabel();
brushSize+=5;
setLabel(event->x(),event->y(),-1);
tool->ColorDone=false;
tool->TextureDone = false;
brushSize-=5;
}
update();
}
void ScribbleArea::mouseMoveEvent(QMouseEvent *event)
{
//return;
if (event->buttons() == Qt::LeftButton && scribbling)
{
if(readOnly)
{
tool->printPixelInformation(event->x(),event->y());
return;
}
setLabel(event->x(),event->y(),currLabel);
lastClick.setX(event->x());
lastClick.setY(event->y());
tool->ColorDone=false;
tool->TextureDone = false;
}
else if(event->buttons() == Qt::RightButton)
{
//removeLastLabel();
brushSize+=5;
setLabel(event->x(),event->y(),-1);
tool->ColorDone=false;
tool->TextureDone = false;
brushSize-=5;
}
update();
}
void ScribbleArea::mouseReleaseEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton && scribbling)
{
scribbling=false;
}
update();
}
void ScribbleArea::clearScribbles()
{
for(int i =0; i < 1000; i++)
for(int j =0; j < 1000; j++)
{
scribbles[i][j] = -1;
scribbles_orig[i][j] = -1;
}
update();
}
void ScribbleArea::paintEvent(QPaintEvent *event)
{
QPainter painter(viewport());
painter.drawImage(0,0,back);
QPen p;
p.setWidth(1);
event->accept();
for(int i=0; i < 1000;i++)
for(int j=0; j < 1000;j++)
{
int label = scribbles[i][j];
if(label < 0) continue;
Vec3f lab = getRgbOfLabel(label)*255;
QRgb col = qRgb(lab[2],lab[1],lab[0]);
p.setColor(col);
painter.setPen(p);
painter.drawPoint(i,j);
}
QGraphicsView::paintEvent(event);
}
QSize ScribbleArea::sizeHint() const
{
QSize q(800,800);
if(readOnly)
return q;
return back.size();
}
void ScribbleArea::setCurrLabel(int l)
{
currLabel = l;
}
void ScribbleArea::setImage(QImage im)
{
back = im;
resize(back.size());
updateGeometry();
}
void ScribbleArea::loadScribbles(QString fileName)
{
ifstream file(fileName.toStdString().c_str());
int oldBrush = brushSize;
brushSize=1;
while(file.good())
{
int x,y,label;
file >> x;
file >> y;
file >> label;
setLabel(x,y,label);
}
file.close();
update();
brushSize=oldBrush;
}
void ScribbleArea::loadScribblesDialog()
{
QString fileName = QFileDialog::getOpenFileName(
this,tr("Open Scribbles"), "d.scribbles", tr("Scribble Files (*.scribbles)"));
loadScribbles(fileName);
}
void ScribbleArea::saveScribblesDialog()
{
QString fileName = QFileDialog::getSaveFileName(
this,tr("Save Scribbles"), "d.scribbles", tr("Scribble Files (*.scribbles)"));
ofstream file(fileName.toStdString().c_str());
for(int i=0; i < 1000;i++)
for(int j=0; j < 1000;j++)
{
int label = scribbles[i][j];
if(label < 0) continue;
file << i << " " << j << " " << label << endl;
}
file.close();
}
void ScribbleArea::loadImage(QString fileName)
{
back.load(fileName);
resize(back.size());
clearScribbles();
tool->ColorDone=false;
tool->TextureDone = false;
updateGeometry();
}
void ScribbleArea::loadImageDialog()
{
QString fileName = QFileDialog::getOpenFileName(
this,tr("Open Image"), "d.JPG", tr("Image Files (*.png *.jpg *.bmp)"));
loadImage(fileName);
}