-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
394 lines (344 loc) · 10.3 KB
/
mainwindow.cpp
File metadata and controls
394 lines (344 loc) · 10.3 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "geometric_transformer.h"
#include <QPainter>
#include <QFile>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFormLayout>
#include <QLabel>
#include <QSpinBox>
#include <QDialogButtonBox>
#include <QInputDialog>
#include "gray_histogram.h"
#include "gray_transformer.h"
;
#pragma pack(push)
#pragma pack(1)
struct BMPFILEHEAD
{
char bfType[2];
unsigned int bfSize;
int bReserved;
unsigned int bfOffBits;
};
#pragma pack(pop)
;
#pragma pack(push)
#pragma pack(1)
struct BITMAPFILEHEADER
{
unsigned int ciSize;//BITMAPFILEHEADER所占的字节数
unsigned int ciWidth;//宽度
int ciHeight;//高度
char ciPlanes[2];//目标设备的位平面数,值为1
short ciBitCount;//每个像素的位数
int ciCompress;//压缩说明
int ciSizeImage;//用字节表示的图像大小,该数据必须是4的倍数
int ciXPelsPerMeter;//目标设备的水平像素数/米
int ciYPelsPerMeter;//目标设备的垂直像素数/米
int ciClrUsed; //位图使用调色板的颜色数
int ciClrImportant; //指定重要的颜色数,当该域的值等于颜色数时(或者等于0时),表示所有颜色都一样重要
};
#pragma pack(pop)
;
#pragma pack(push)
#pragma pack(1)
struct RGB
{
unsigned char b,g,r;
};
#pragma pack(pop)
;
#pragma pack(push)
#pragma pack(1)
struct RGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
};
#pragma pack(pop)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
imgloaded = false;
image = nullptr;
ihp = nullptr;
fhp = nullptr;
}
MainWindow::~MainWindow()
{
delete ui;
if(image != nullptr)delete image;
if(ihp != nullptr) delete (BITMAPFILEHEADER*)ihp;
if(fhp != nullptr) delete (BMPFILEHEAD*)fhp;
}
void MainWindow::readbmp(QString filename){
QFile* fileno = new QFile(filename);
if(!fileno->open(QIODevice::ReadOnly)){
qDebug() << "打开失败";
}
QDataStream stream(fileno);
BMPFILEHEAD * fh = new BMPFILEHEAD;
BITMAPFILEHEADER * ih = new BITMAPFILEHEADER;
stream.readRawData((char*)fh,14);
stream.readRawData((char*)ih,40);
fhp = fh; ihp = ih;
if(ih->ciHeight < 0){
//TODO
}
RGBQUAD* tsb = nullptr;
if(ih->ciBitCount != 24){
//有调色板
int tiaolines = 1 << ih->ciBitCount;
tsb = new RGBQUAD[tiaolines];
stream.readRawData((char*)tsb,tiaolines * sizeof(RGBQUAD));//256 * 4
}
//qDebug() <<tsb[255].rgbRed << tsb[255].rgbGreen << tsb[255].rgbBlue;
stream.device()->seek(fh->bfOffBits);
image = new QImage(ih->ciWidth,ih->ciHeight,QImage::Format_RGB888);
if(tsb != nullptr){
char* perline = new char[ih->ciWidth];
int skip = 4 - (((ih->ciWidth * ih->ciBitCount) >> 3) & 3);
skip %= 4;
for(int i = 0;i < ih->ciHeight;i++){
stream.readRawData(perline,ih->ciWidth);
stream.skipRawData(skip);
for(int j = 0; j < ih->ciWidth;j++){
RGBQUAD t = tsb[(unsigned char)perline[j]];
//qDebug() << (int)perline[j];
image->setPixelColor(j,ih->ciHeight - i - 1,qRgb(t.rgbRed,t.rgbGreen,t.rgbBlue));
}
}
delete [] perline;
delete tsb;
}
else{
RGB* pixels = new RGB[ih->ciWidth];
int skip = 4 - (((ih->ciWidth * ih->ciBitCount) >> 3) & 3);
skip %= 4;
for(int i = 0;i < ih->ciHeight;i++){
stream.readRawData((char*)pixels,ih->ciWidth * sizeof(RGB));
stream.skipRawData(skip); //跳过对齐用的00字节
for(int j = 0; j < ih->ciWidth;j++){
(*image).setPixelColor(j,ih->ciHeight - i - 1,qRgb(pixels[j].r,pixels[j].g,pixels[j].b));
}
}
delete [] pixels;
}
delete fileno;
imgloaded = true;
}
void MainWindow::paintEvent(QPaintEvent *){
if(image != nullptr){
QPainter painter(this);
int w = ui->horizontalScrollBar->value();
int h = ui->verticalScrollBar->value();
//工具栏有高度26
painter.drawImage(QPoint(0,26),image->copy(w,h,image->width() - w,image->height() - h));
}
}
void MainWindow::on_openfile_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,"打开图片",".","bmp files (*.bmp)");
if(filename != "") {
if(image != nullptr){
delete image;
image = nullptr;
}
readbmp(filename);
}
else return;
update();
}
void MainWindow::on_gray_triggered()
{
if(image == nullptr)return;
Gray_Transformer::gray(image);
update();
}
void MainWindow::on_profile_triggered()
{
if(image == nullptr)return;
BMPFILEHEAD * fh = (BMPFILEHEAD*)fhp;
BITMAPFILEHEADER * ih = (BITMAPFILEHEADER*)ihp;
QMessageBox::about(NULL, "关于该图片",
QString("图片大小:") +
QString::number(fh->bfSize) + QString("字节\r\n") +
QString("宽度:") +
QString::number(ih->ciWidth) + QString("像素\r\n") +
QString("高度:") +
QString::number(ih->ciHeight) + QString("像素"));
}
void MainWindow::on_translate_triggered()
{
if(image == nullptr)return;
QDialog dialog(this);
dialog.setMinimumSize(QSize(200,150));
QFormLayout form(&dialog);
form.addRow(new QLabel("请输入参数:"));
// Value1
QString value1 = QString("宽: ");
QSpinBox *spinbox1 = new QSpinBox(&dialog);
spinbox1->setMaximum(9999);
form.addRow(value1, spinbox1);
// Value2
QString value2 = QString("高: ");
QSpinBox *spinbox2 = new QSpinBox(&dialog);
spinbox2->setMaximum(9999);
form.addRow(value2, spinbox2);
// Add Cancel and OK button
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, &dialog);
form.addRow(&buttonBox);
QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
// Process when OK button is clicked
if (dialog.exec() == QDialog::Accepted) {
//qDebug() << spinbox1->value() << spinbox2->value();
Geometric_Transformer::translate(spinbox1->value(),spinbox2->value(),image);
}
}
void MainWindow::on_Horizontal_Mirror_triggered()
{
if(image == nullptr)return;
Geometric_Transformer::Horizontal_Mirror(image);
update();
}
void MainWindow::on_Vertical_Mirror_triggered()
{
if(image == nullptr)return;
Geometric_Transformer::Vertical_Mirror(image);
update();
}
void MainWindow::on_Rotate_triggered()
{
if(image == nullptr)return;
bool ok;
double angle = QInputDialog::getDouble(this,"角度","弧度制角度",0,-7,7,2,&ok,Qt::Dialog,0.01);
if(ok){
Geometric_Transformer::Rotate(angle,image);
update();
}
}
void MainWindow::on_verticalScrollBar_valueChanged(int value)
{
update();
}
void MainWindow::on_horizontalScrollBar_valueChanged(int value)
{
update();
}
void MainWindow::on_Shrink_triggered()
{
if(image == nullptr)return;
bool ok;
int k = QInputDialog::getInt(this,"倍数","原图的百分比",100,1,100,1,&ok);
if(ok){
Geometric_Transformer::Shrink(k,image);
update();
}
}
void MainWindow::on_Enlarge_triggered()
{
if(image == nullptr)return;
bool ok;
int k = QInputDialog::getInt(this,"倍数","原图的百分比",100,100,1000,1,&ok);
if(ok){
Geometric_Transformer::Enlarge(k,image);
update();
}
}
void MainWindow::on_saveas_triggered()
{
if(image == nullptr)return;
QString filename = QFileDialog::getSaveFileName(this,"保存图片",".","Images (*.bmp *.png *.jpg)");
if(filename != ""){
image->save(filename);
}
}
void MainWindow::on_Horizontal_Shear_triggered()
{
if(image == nullptr)return;
bool ok;
int k = QInputDialog::getInt(this,"角度","与y轴的错切角度",0,-89,89,1,&ok);
if(ok){
Geometric_Transformer::Horizontal_Shear(k,image);
update();
}
}
void MainWindow::on_Vertical_Shear_triggered()
{
if(image == nullptr)return;
bool ok;
int k = QInputDialog::getInt(this,"角度","与x轴的错切角度",0,-89,89,1,&ok);
if(ok){
Geometric_Transformer::Vertical_Shear(k,image);
update();
}
}
void MainWindow::on_Histogram_triggered()
{
if(image == nullptr)return;
Gray_histogram* gray = new Gray_histogram(image);
gray->show();
}
void MainWindow::on_Histogram_Equalize_triggered()
{
if(image == nullptr)return;
Gray_Transformer::Histogram_Equalize(image);
update();
}
void MainWindow::on_HSI_Histogram_Equalize_triggered()
{
if(image == nullptr)return;
Gray_Transformer::HSI_Histogram_Equalize(image);
update();
}
void MainWindow::on_avg_filter_triggered()
{
if(image == nullptr)return;
Gray_Transformer::avg_filter(image);
update();
}
void MainWindow::on_high_freq_filter_triggered()
{
if(image == nullptr)return;
Gray_Transformer::high_freq_filter(image);
update();
}
void MainWindow::on_Gradient_sharpen_triggered()
{
if(image == nullptr)return;
Gray_Transformer::Gradient_sharpen(image);
update();
}
void MainWindow::on_Edge_detection_triggered()
{
if(image == nullptr)return;
Gray_Transformer::Edge_detection(image);
update();
}
void MainWindow::on_Image_segmentation_triggered()
{
if(image == nullptr)return;
Gray_Transformer::Image_segmentation(image);
update();
}
void MainWindow::on_Add_Gaussian_Noise_triggered()
{
if(image == nullptr)return;
Gray_Transformer::Add_Gaussian_Noise(image);
update();
}
void MainWindow::on_Maximum_entropy_segmentation_triggered()
{
if(image == nullptr)return;
Gray_Transformer::Maximum_entropy_segmentation(image);
update();
}