-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageblur.cpp
160 lines (128 loc) · 4.22 KB
/
imageblur.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
#include "imageblur.h"
#include "ui_imageblur.h"
#include <QThread>
#include <QImage>
#include <QMessageBox>
#include <QtConcurrent/QtConcurrent>
ImageBlur::ImageBlur(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ImageBlur)
//init
, blurRange(0)
, threadCount(0)
{
ui->setupUi(this);
// to view the picture in the lable
QPixmap pix;
ui->label->setPixmap(pix);
}
ImageBlur::~ImageBlur()
{
delete ui;
}
void ImageBlur::on_pushButton_clicked()
{
//open file
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images(*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "", tr("Image File(*.png *.jpg *.bmp)"));
if(!fileName.isEmpty()){
QImage image(fileName);
ui->txtPath->setText(fileName);
// ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->setScaledContents(false);
QSize labelSize = ui->label->size();
QPixmap pixmap = QPixmap::fromImage(image).scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
this->resize(pixmap.size());
//save picture
originalImage = image;
}
}
void ImageBlur::changeEvent(QEvent *e){
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
// int blurRange = 0;
// int threadCount = 0;
void ImageBlur::on_btnAdd_clicked()
{
blurRange = ui->setTxtRange->text().toInt();
blurRange += 1;
ui->setTxtRange->setText(QString::number(blurRange));
}
void ImageBlur::on_btnSub_clicked()
{
if(blurRange >0){
blurRange -=1;
ui->setTxtRange->setText(QString::number(blurRange));
}
}
void ImageBlur::on_btnBlur_clicked()
{
threadCount = ui->setTxtThread->text().toInt();
if(threadCount <1){
QMessageBox::warning(this, "Error","Thread count must be at least 1.");
return;
}
QFutureWatcher<QImage> *watcher = new QFutureWatcher<QImage>(this);
connect(watcher, &QFutureWatcher<QImage>::finished, this, [this, watcher]() {
QImage blurredImage = watcher->result();
QPixmap pixmap = QPixmap::fromImage(blurredImage).scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
watcher->deleteLater();
});
QFuture<QImage> future = QtConcurrent::run([this]() {
return applyBlur(originalImage, blurRange, threadCount);
});
watcher->setFuture(future);
}
QImage ImageBlur::applyBlur(const QImage &image, int range, int threadCount)
{
QImage blurredImage = image;
int width = image.width();
int height = image.height();
auto blurFunction = [&](int startY, int endY) {
for (int y = startY; y < endY; ++y) {
for (int x = 0; x < width; ++x) {
int redSum = 0, greenSum = 0, blueSum = 0, pcount = 0;
for (int i = y - range; i <= y + range; ++i) {
for (int j = x - range; j <= x + range; ++j) {
if (i >= 0 && i < height && j >= 0 && j < width) {
QColor color = image.pixelColor(j, i);
redSum += color.red();
greenSum += color.green();
blueSum += color.blue();
pcount++;
}
}
}
if (pcount > 0) {
QColor avgColor(redSum / pcount, greenSum / pcount, blueSum / pcount);
blurredImage.setPixelColor(x, y, avgColor);
}
}
}
};
QVector<QThread *> threads;
int rowsPerThread = height / threadCount;
for (int i = 0; i < threadCount; ++i) {
int startY = i * rowsPerThread;
int endY = (i == threadCount - 1) ? height : (i + 1) * rowsPerThread;
QThread *thread = QThread::create(blurFunction, startY, endY);
threads.append(thread);
thread->start();
}
for (QThread *thread : threads) {
thread->wait();
delete thread;
}
return blurredImage;
}