-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
212 lines (185 loc) · 6.86 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
// created by Heitor Adao Junior
#include "mainwindow.h"
#include "configdialog.h"
#include "colorfrequency.h"
#include "ui_mainwindow.h"
#include "colormatrix.h"
#include <cmath>
#include <QFileDialog>
#include <QPainter>
#include <QVector>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->toolButtonOpenImage, SIGNAL(clicked()), this, SLOT(openImage()));
lastOpenedPath = QDir::homePath();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openImage()
{
QString pathSearch;
pathSearch = (ui->lineEditPath->text().isEmpty()) ? lastOpenedPath : ui->lineEditPath->text();
QString filename(QFileDialog::getOpenFileName(this, "", pathSearch, "Images (*.png *.bmp *.jpg *.gif);;All files(*.*)"));
ui->lineEditPath->setText(filename);
QDir dir(filename);
lastOpenedPath = dir.absolutePath();
imageOriginal.load(filename);
ui->labelOriginal->setPixmap(imageOriginal);
emit newOpenedImage();
switch (ui->comboBoxTransformType->currentIndex()) {
case 0:
transformGrayScale();
break;
case 1:
transformHighPass();
break;
case 2:
transformLowPass();
break;
case 3:
transformPaletted();
break;
}
}
void MainWindow::transformGrayScale()
{
// cria o novo pixmap
// seta o tamanho igual ao tamanho da iagem de origem
QSize size(imageOriginal.size());
imageModified = QPixmap(size);
QPainter painter(&imageModified);
for (int y = 0; y < size.height(); ++y) {
for (int x = 0; x < size.width(); ++x) {
QColor oldColor(imageOriginal.toImage().pixel(x,y));
int newColorComponents = oldColor.red() * 0.5 + oldColor.green() * 0.3 + oldColor.blue() * 0.2;
QColor newColor(newColorComponents, newColorComponents, newColorComponents);
painter.setPen(newColor);
painter.drawPoint(x, y);
}
}
ui->labelModified->setPixmap(imageModified);
}
void MainWindow::transformLowPass()
{
ColorMatrix colorMatrix;
QSize size(imageOriginal.size());
imageModified = QPixmap(size);
QPainter painter(&imageModified);
for (int y = 0; y < size.height(); ++y) {
for (int x = 0; x < size.width(); ++x) {
QColor newColor(colorMatrix.getPixel(imageOriginal.toImage(), QPoint(x, y)));
painter.setPen(newColor);
painter.drawPoint(x, y);
}
}
ui->labelModified->setPixmap(imageModified);
}
void MainWindow::transformHighPass()
{
int weightHighPass[9] = {0, -1, 0,
-1, 5, -1,
0, -1, 0};
ColorMatrix colorMatrix(3, weightHighPass);
QSize size(imageOriginal.size());
imageModified = QPixmap(size);
QPainter painter(&imageModified);
for (int y = 0; y < size.height(); ++y) {
for (int x = 0; x < size.width(); ++x) {
QColor newColor(colorMatrix.getPixel(imageOriginal.toImage(), QPoint(x, y)));
painter.setPen(newColor);
painter.drawPoint(x, y);
}
}
ui->labelModified->setPixmap(imageModified);
}
void MainWindow::transformPaletted(int numColors)
{
// value is the frequency of color
QMap<QColor, int> histogram;
QSize size(imageOriginal.size());
for (int y = 0; y < size.height(); ++y) { // for each pixel of image
for (int x = 0; x < size.width(); ++x) {
QColor oldColor(imageOriginal.toImage().pixel(x,y));
if (histogram.contains(oldColor)) { // if pixel already on histogram...
int frequency = histogram.value(oldColor); // increment
histogram[oldColor] = ++frequency;
} else { // else
histogram.insert(oldColor, 1); // insert, with default value = 1
}
}
}
// cria um vetor de tamanho = max colors
QVector<QColor> newPalette;
newPalette.resize(numColors);
int higherFrequency = 0;
QColor colorWithHigherFrequency;
for (int c = 0; c < numColors; ++c) {
//percorre o map
QMap<QColor, int>::iterator it = histogram.begin();
for (; it != histogram.end(); ++it) {
if (it.value() > higherFrequency)
colorWithHigherFrequency = it.key();
}
histogram.remove(colorWithHigherFrequency);
newPalette[c] = colorWithHigherFrequency;
}
imageModified = QPixmap(size);
QPainter painter(&imageModified);
// raster the image again, and for each pixel...
for (int y = 0; y < size.height(); ++y) {
for (int x = 0; x < size.width(); ++x) {
QColor oldColor(imageOriginal.toImage().pixel(x,y));
QColor closerColor;
double distanceOfCloserColor = std::numeric_limits<double>::max();
// para cara item do vetor
QVector<QColor>::iterator it = newPalette.begin();
for(; it != newPalette.end(); ++it) {
// calc the distance from actual pixel to item vector
double distance = colorDistance(oldColor, *it);
// comparae a distancia com a menor distancia, se for menor ou igual...
if (distance < distanceOfCloserColor) {
// store the distance and color
closerColor = *it;
distanceOfCloserColor = distance;
}
}
// draw the color in the second image
painter.setPen(closerColor);
painter.drawPoint(x, y);
}
}
#if 1
for (int i = 0; i < numColors; ++i) {
painter.setPen(newPalette.at(i));
painter.drawPoint(i, 0);
}
#endif
ui->labelModified->setPixmap(imageModified);
}
void MainWindow::on_toolButton_clicked()
{
ConfigDialog config;
config.exec();
}
void MainWindow::on_pushButtonSaveAs_clicked()
{
QString fileName(QFileDialog::getSaveFileName(this, "save modified image as...", lastOpenedPath,
"Bitmap (*.bmp);;"
"Portable Network Graphics (*.png)"));
QString extension(fileName.right(3));
extension.toUpper();
const int quality = 100; // no compression
imageModified.save(fileName, extension.toLatin1(), quality);
}
double MainWindow::colorDistance(const QColor &color1, const QColor &color2)
{
double red = std::pow((double)color1.red() - color2.red(), 2);
double green = std::pow((double)color1.green() - color2.green(), 2);
double blue = std::pow((double)color1.blue() - color2.blue(), 2);
return std::sqrt(red + green + blue);
}