-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.cpp
184 lines (154 loc) · 4 KB
/
form.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
#include <iostream>
#include "form.h"
#include "ui_form.h"
#include <QPainter>
#include "../M1Kryptographie/nbild.h"
#include "../M1Kryptographie/cbild.h"
#include <QFile>
#include <QFileDialog>
#include <string>
#include <QMessageBox>
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
toogle_draw = false;
}
Form::~Form()
{
delete ui;
}
/*!
* @brief draws the image on the widget
*/
void Form::paintEvent(QPaintEvent *e){
this->draw_image(e,image);
}
/*!
* @brief draws the image on the widget
*/
void Form::draw_image(QPaintEvent *, NBild image){ //draw
int nrows = static_cast<int>(image.nrows);
int ncols = static_cast<int>(image.ncols);
QPainter painter(this);
painter.setBrush(Qt::black);
for (int row = 0; row < nrows; row++){
for (int col = 0; col < ncols; col++){
if(image(static_cast<size_t>(row), static_cast<size_t>(col)) == 0){
painter.setBrush(Qt::black);
painter.setPen(Qt::black);
} else {
painter.setBrush(Qt::white);
painter.setPen(Qt::white);
}
int x1 = (col*img_width)/ncols;
int y1 = (row*img_height)/nrows;
int x2 = ((col+1)*img_width)/ncols;
int y2 = ((row+1)*img_height)/nrows;
painter.drawRect(x1,y1,
x2-x1,y2-y1);
}
}
toogle_draw = false;
}
/*!
* @brief loads a image from the chose file
*/
void Form::on_pushButton_clicked() //load
{
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
string directory = fileName.toUtf8().constData();
try{
image.import(directory);
} catch(int e){
if(e == 1){
messageBox.critical(nullptr,"Error","Can't find the requested file");
} else if (e == 0){
messageBox.critical(nullptr,"Error","Invalid input format");
}
}
toogle_draw = true;
update();
}
/*!
* @brief encodes the image with the chosen key
*/
void Form::on_pushButton_2_clicked() //encode
{
string key;
if(ui->radioButton->isChecked()){
key = "A";
} else {
key = "B";
}
CBild crypt = CBild(key);
image = crypt.encode(image);
toogle_draw = true;
update();
}
/*!
* @brief decodes the image with the chosen key
*/
void Form::on_pushButton_3_clicked() //decode
{
string key;
if(ui->radioButton->isChecked()){
key = "A";
} else {
key = "B";
}
CBild crypt = CBild(key);
image = crypt.decode(image);
toogle_draw = true;
update();
}
/*!
* @brief lays the chosen image over the currently displayed image
*/
void Form::on_pushButton_4_clicked() //overlay
{
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
string directory = fileName.toUtf8().constData();
NBild overlay_img;
try{
overlay_img.import(directory);
} catch(int e){
if(e == 1){
messageBox.critical(nullptr,"Error","Can't find the requested file");
} else if (e == 0){
messageBox.critical(nullptr,"Error","Invalid input format");
}
}
string key;
if(ui->radioButton->isChecked()){
key = "A";
} else {
key = "B";
}
if (overlay_img.ncols == image.ncols && overlay_img.nrows == image.nrows){
CBild crypt = CBild(key);
image = crypt.overlay(image,overlay_img);
} else {
messageBox.critical(nullptr,"Error","Incompatible image sizes");
}
toogle_draw = true;
update();
}
/*!
* @brief saves the image in the chosen file
*/
void Form::on_pushButton_5_clicked() //save
{
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
string directory = fileName.toUtf8().constData();
try{
image.writeToFile(directory);
} catch(int e){
if(e == 1){
messageBox.critical(nullptr,"Error","Can't find the requested file");
}
}
toogle_draw = true;
update();
}