/
mainwindow.cpp
402 lines (338 loc) · 11.5 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDropEvent>
#include <QUrl>
#include <QFileDialog>
#include <QMessageBox>
#include <QInputDialog>
#include "AES.h"
#include "md5.h"
// return i in [a, b]
int bound(short i,short a,short b)
{
return min(max(i,min(a,b)),max(a,b));
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->action_Open,SIGNAL(triggered()),this,SLOT(load()));
connect(ui->action_Save,SIGNAL(triggered()),this,SLOT(save()));
connect(ui->encryptBtn,SIGNAL(clicked()),this,SLOT(encrypt()));
connect(ui->decryptBtn,SIGNAL(clicked()),this,SLOT(decrypt()));
connect(ui->actionChange_Median_Blur_Kernel,SIGNAL(triggered()),this,SLOT(setKernel()));
connect(ui->actionChange_Eraser_Size,SIGNAL(triggered()),this,SLOT(setR()));
setAcceptDrops(true);
/* initialization */
setMode(2); // Lasso
setStyle(1); // median blur
kernel = 51;
setR(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if(event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> droppedUrls = event->mimeData()->urls();
QString localPath = droppedUrls[0].toLocalFile();
if (!localPath.endsWith(".png", Qt::CaseInsensitive) && !localPath.endsWith(".jpg", Qt::CaseInsensitive)) {
QMessageBox::critical(0, "Error", "Path: "+localPath+"\nThe filetype is not supported!!");
return;
}
filename = localPath.toAscii().data();
destroyAllWindows();
_src = imread(filename);
if (!_src.data) {
return;
}
namedWindow("Image", CV_WINDOW_AUTOSIZE);
imshow("Image", _src);
_dst.release();
ui->encryptBtn->setEnabled(true);
ui->decryptBtn->setEnabled(true);
event->acceptProposedAction();
}
void MainWindow::load()
{
QString imageName = QFileDialog::getOpenFileName(this,"Open Image", "", "Image Files (*.jpg *.png)");
if (imageName == NULL)
return;
filename = String(imageName.toAscii().data());
destroyAllWindows();
_src = imread(filename);
if (!_src.data) {
QMessageBox::critical(0, "Error", "No image file is loaded!!");
return;
}
namedWindow("Image", CV_WINDOW_AUTOSIZE);
imshow("Image", _src);
_dst.release();
ui->encryptBtn->setEnabled(true);
ui->decryptBtn->setEnabled(true);
}
void MainWindow::save()
{
if (!_dst.data) {
QMessageBox::critical(0, "Error", "There is nothing to be saved");
return;
}
QString imageName = QFileDialog::getSaveFileName(this,"Save Image", "", "Image Files (*.jpg)");
if (imageName == NULL)
return;
imwrite(imageName.toAscii().data(), _dst);
if (isEncryption) {
AES aes(key);
int buf_size[2];
vector<unsigned char> mask_buf, secret_buf;
unsigned char *temp;
vector<int> p;
p.push_back(CV_IMWRITE_JPEG_QUALITY);
p.push_back(50);
imencode(".jpg", zone, mask_buf, p);
for (int i = (((mask_buf.size()+15)>>4)<<4) - mask_buf.size(); i > 0; i--)
mask_buf.push_back(0);
for (unsigned int i = 0; i < mask_buf.size(); i+=16) {
temp = &mask_buf[0]+i;
aes.Cipher(temp);
}
Mat secret;
dilate(zone, zone, Mat(7,7,CV_8U,Scalar(1)));
_src.copyTo(secret, zone);
imencode(".jpg", secret, secret_buf);
for (int i = (((secret_buf.size()+15)>>4)<<4) - secret_buf.size(); i > 0; i--)
secret_buf.push_back(0);
for (unsigned int i = 0; i < secret_buf.size(); i+=16) {
temp = &secret_buf[0]+i;
aes.Cipher(temp);
}
unsigned char info[16] = "";
buf_size[0] = mask_buf.size();
buf_size[1] = secret_buf.size();
memcpy(info, buf_size, sizeof(int)*2);
memcpy(info+8, "SEAcret", 7);
aes.Cipher(info);
// append encrypted block of image and encryted info to blurred image
FILE *f = fopen(imageName.toAscii().data(), "ab");
fwrite(&mask_buf[0], 1, mask_buf.size(), f);
fwrite(&secret_buf[0], 1, secret_buf.size(), f);
fwrite(info, 1, 16, f);
fclose(f);
}
}
void MainWindow::encrypt()
{
isEncryption = true;
QString text = QInputDialog::getText(this, "Password",
"Please enter your password for encryption",
QLineEdit::Password, QString());
string pwd = string((const char *)text.toLocal8Bit());
MD5 md5(pwd);
key = md5.getDigest();
_dst = _src.clone();
zone = Mat::zeros(_src.size(), CV_8UC1);
setMouseCallback("Image", &mouseHandler, this);
imshow("Image", _dst);
waitKey(0);
destroyAllWindows();
}
void MainWindow::decrypt()
{
FILE *f = fopen(filename.c_str(), "rb");
if (!f) {
QMessageBox::critical(0, "Error", "No image file is loaded!!");
return;
}
isEncryption = false;
QString text = QInputDialog::getText(this, "Password",
"Please enter your password for decryption",
QLineEdit::Password, QString());
string pwd = string((const char *)text.toLocal8Bit());
MD5 md5(pwd);
key = md5.getDigest();
AES aes(key);
unsigned char info[16] = "";
fseek(f, -16, SEEK_END);
fread(info, 1, 16, f);
// verify key and info
aes.InvCipher(info);
if (memcmp(info+8, "SEAcret", 7)) {
QMessageBox::critical(0, "Error", "Incorrect password or there is no secret in the image");
return;
}
int buf_size[2];
memcpy(buf_size, info, sizeof(int)*2);
fseek(f, -16-buf_size[0]-buf_size[1], SEEK_END);
unsigned char *temp;
vector<unsigned char> mask_buf(buf_size[0]);
vector<unsigned char> secret_buf(buf_size[1]);
fread(&mask_buf[0], 1, buf_size[0], f);
fread(&secret_buf[0], 1, buf_size[1], f);
fclose(f);
for (int i = 0; i < buf_size[0]; i+=16) {
temp = &mask_buf[0]+i;
aes.InvCipher(temp);
}
Mat mask = imdecode((Mat)mask_buf, CV_LOAD_IMAGE_GRAYSCALE);
mask = mask > 128;
for (int i = 0; i < buf_size[1]; i+=16) {
temp = &secret_buf[0]+i;
aes.InvCipher(temp);
}
Mat secret = imdecode((Mat)secret_buf, 1);
_dst = imread(filename, 1);
secret.copyTo(_dst, mask);
imshow("Result", _dst);
waitKey(0);
destroyAllWindows();
}
void MainWindow::mouseHandler(int event, int x, int y, int flags, void* param)
{
MainWindow* window = (MainWindow*) param;
if (window->mode == 3) {
x = bound(x, window->R, window->_src.cols-1-window->R);
y = bound(y, window->R, window->_src.rows-1-window->R);
}
else {
x = bound(x, 0, window->_src.cols-1);
y = bound(y, 0, window->_src.rows-1);
}
// user press left button
if (event == CV_EVENT_LBUTTONDOWN && !window->drag) {
if (window->mode == 1)
window->point = Point(x, y);
else if (window->mode == 2) {
window->points.clear();
window->points.push_back(Point(x, y));
}
else if (window->mode == 3) {
Rect box = Rect(x-window->R, y-window->R, 1+window->R*2, 1+window->R*2);
Mat temp;
medianBlur(window->_dst(box), temp, window->kernel);
temp.copyTo(window->_dst(box), window->round_mask);
window->round_mask.copyTo(window->zone(box), window->round_mask);
imshow("Image", window->_dst);
}
window->drag = true;
}
// user drag the mouse
if (event == CV_EVENT_MOUSEMOVE && window->drag) {
Mat temp = window->_dst.clone();
if (window->mode == 1)
rectangle(temp, window->point, Point(x, y), CV_RGB(255, 0, 0), 2, 8, 0);
else if (window->mode == 2) {
window->pts = (const Point*)Mat(window->points).data;
window->npts = window->points.size();
window->points.push_back(Point(x, y));
polylines(temp, &window->pts, &window->npts, 1, false, Scalar(0,0,255), 2, 8, 0);
}
else if (window->mode == 3) {
Rect box = Rect(x-window->R, y-window->R, 1+window->R*2, 1+window->R*2);
Mat temp;
medianBlur(window->_dst(box), temp, window->kernel);
temp.copyTo(window->_dst(box), window->round_mask);
window->round_mask.copyTo(window->zone(box), window->round_mask);
imshow("Image", window->_dst);
return;
}
imshow("Image", temp);
}
// user release left button
if (event == CV_EVENT_LBUTTONUP && window->drag) {
if (window->mode == 1) {
Point temp(window->point);
window->point.x = min(x, temp.x);
x = max(x, temp.x);
window->point.y = min(y, temp.y);
y = max(y, temp.y);
rectangle(window->zone, window->point, Point(x, y), Scalar(255), CV_FILLED, 8, 0);
Rect box = Rect(window->point.x, window->point.y, x - window->point.x, y - window->point.y);
if (window->style == 1) {
medianBlur(window->_dst(box), window->_dst(box), window->kernel);
medianBlur(window->_dst(box), window->_dst(box), window->kernel);
}
else {
Mat mask = Mat::zeros(window->_src.size(), CV_8UC1);
rectangle(mask, window->point, Point(x, y), CV_RGB(255, 255, 255), CV_FILLED, 8, 0);
inpaint(window->_dst, mask, window->_dst, 5, INPAINT_TELEA);
}
}
else if (window->mode == 2) {
Rect box = boundingRect(window->points);
vector< vector<Point> > contour;
contour.push_back(window->points);
Mat mask = Mat::zeros(window->_src.size(), CV_8UC1);
fillPoly(mask, contour, Scalar(255));
fillPoly(window->zone, contour, Scalar(255));
if (window->style == 1) {
Mat temp;
medianBlur(window->_dst(box), temp, window->kernel);
medianBlur(temp, temp, window->kernel);
temp.copyTo(window->_dst(box), mask(box));
}
else
inpaint(window->_dst, mask, window->_dst, 5, INPAINT_TELEA);
}
imshow("Image", window->_dst);
window->drag = false;
}
// user click right button: reset all
if (event == CV_EVENT_RBUTTONUP) {
window->_dst = window->_src.clone();
window->zone = Mat::zeros(window->_src.size(), CV_8UC1);
window->drag = false;
imshow("Image", window->_dst);
}
}
void MainWindow::setMode(int m)
{
mode = m;
drag = false;
}
void MainWindow::setStyle(int s)
{
style = s;
drag = false;
}
void MainWindow::setKernel()
{
int input = QInputDialog::getInteger(this, "Medain Blur Kernel Size", "Enter an integer (1~49)", 25, 1, 49, 1);
kernel = 1 + input*2;
}
void MainWindow::setR(int r)
{
if (r)
R = r;
else {
int input = QInputDialog::getInteger(this, "Eraser Size", "Enter an integer (1~49)", 10, 1, 49, 1);
R = input;
}
round_mask = Mat::zeros(Size(1+R*2, 1+R*2), CV_8UC1);
circle(round_mask, Point(R,R), R, Scalar(255), CV_FILLED, 8, 0);
}
void MainWindow::on_actionRectangle_triggered()
{
setMode(1);
}
void MainWindow::on_actionLasso_triggered()
{
setMode(2);
}
void MainWindow::on_actionEraser_triggered()
{
setMode(3);
}
void MainWindow::on_actionMedian_Blur_triggered()
{
setStyle(1);
}
void MainWindow::on_actionInpaint_triggered()
{
setStyle(2);
}