-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree.cpp
More file actions
197 lines (167 loc) · 6.13 KB
/
Copy pathquadtree.cpp
File metadata and controls
197 lines (167 loc) · 6.13 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
#include "quadtree.h"
#include <iostream>
QuadTree::QuadTree()
{
root = nullptr;
}
// Функция для вычисления евклидова расстояния между двумя цветами
double QuadTree::calculateColorDistance(const QColor& color1, const QColor& color2) {
double deltaR = color1.red() - color2.red();
double deltaG = color1.green() - color2.green();
double deltaB = color1.blue() - color2.blue();
return std::sqrt(deltaR * deltaR + deltaG * deltaG + deltaB * deltaB);
}
bool QuadTree::areColorsApproximatelyEqual(const QColor& color1, const QColor& color2)
{
double distance = calculateColorDistance(color1, color2);
return (distance < THRESHOLD);
}
void QuadTree::loadImage(QString path)
{
m_image = new SquarePowerOfTwoImage(path);
// Проверка на успешную загрузку
if (m_image->isNull()) {
qDebug() << "Failed to load image";
return;
}
QSize size = m_image->getSquareImageSize();
int x = size.width();
int y = size.height();
root = loadImageHelper(0, x, 0, y);
}
QuadTree::Node* QuadTree::loadImageHelper(int x, int width, int y, int height)
{
Node* newNode = new Node();
int Red = 0;
int Green = 0;
int Blue = 0;
int pixelsCnt = 0;
for (int i = y; i < y + height; i++)
{
for (int j = x; j < x + width; j++)
{
QColor pixelColor = m_image->pixelColor(j, i);
if (pixelColor != nullptr)
{
int red = pixelColor.red();;
int green = pixelColor.green();
int blue = pixelColor.blue();
Red += red;
Green += green;
Blue += blue;
pixelsCnt++;
}
}
}
QColor averageColor(0, 0, 0);
if (pixelsCnt != 0)
{
averageColor = QColor (Red / pixelsCnt, Green / pixelsCnt, Blue / pixelsCnt);
Red = Red / pixelsCnt;
Green = Green / pixelsCnt;
Blue = Blue / pixelsCnt;
}
double error = 0;
for (int i = y; i < y + height; i++)
{
for (int j = x; j < x + width; j++)
{
QColor pixelColor = m_image->pixelColor(j, i);
if (pixelColor != nullptr)
{
error += calculateColorDistance(pixelColor, averageColor);
}
}
}
error = error / pixelsCnt;
if (error > THRESHOLD)
{
Node* topLeft = loadImageHelper(x, width / 2, y, height / 2);
Node* topRight = loadImageHelper(x + width / 2, width / 2, y, height / 2);
Node* bottomLeft = loadImageHelper(x, width / 2, y + height / 2, height / 2);
Node* bottomRight = loadImageHelper(x + width / 2, width / 2, y + height / 2, height / 2);
QColor topLeftColor = topLeft->color;
QColor topRightColor = topRight->color;
QColor bottomLeftColor = bottomLeft->color;
QColor bottomRightColor = bottomRight->color;
Red = (topLeftColor.red() + topRightColor.red() + bottomLeftColor.red() + bottomRightColor.red()) / 4;
Green = (topLeftColor.green() + topRightColor.green() + bottomLeftColor.green() + bottomRightColor.green()) / 4;
Blue = (topLeftColor.blue() + topRightColor.blue() + bottomLeftColor.blue() + bottomRightColor.blue()) / 4;
QColor averageColor(Red, Green, Blue);
newNode->topLeft = topLeft;
newNode->topRight = topRight;
newNode->bottomLeft = bottomLeft;
newNode->bottomRight = bottomRight;
}
newNode->setRGB(Red, Green, Blue);
return newNode;
}
QImage QuadTree::convertToImage()
{
int width = m_image->getSquareImageSize().width();
int height = m_image->getSquareImageSize().width();
QImage *image = new QImage(width, height, QImage::Format_RGB888);
convertToImageHelper(0, width, 0, height, image, root);
QPoint startPoint = m_image->getQImageStartPoint();
QSize originalImageSize = m_image->getQImage()->size();
QImage newImage = image->copy(startPoint.x(), startPoint.y(), originalImageSize.width(), originalImageSize.height());
return newImage;
}
bool QuadTree::noChildren(Node *node)
{
bool haveTopLeft = node->topLeft == nullptr;
bool haveTopRight = node->topRight == nullptr;
bool haveBottomLeft= node->bottomLeft == nullptr;
bool haveBottomRight = node->bottomRight == nullptr;
return haveTopLeft * haveTopRight * haveBottomLeft * haveBottomRight;
}
void QuadTree::convertToImageHelper(int x, int width, int y, int height, QImage *image, Node* node)
{
if (noChildren(node))
{
if (width != 1 && height != 1)
{
for (int i = y; i < y + height; i++)
{
for (int j = x; j < x + width; j++)
{
image->setPixel(j, i, node->color.rgb());
}
}
}
else
{
image->setPixel(x, y, node->color.rgb());
}
}
else
{
convertToImageHelper(x, width/2, y, height/2, image, node->topLeft);
convertToImageHelper(x + width/2, width/2, y, height/2, image, node->topRight);
convertToImageHelper(x, width/2, y + height/2, height/2, image, node->bottomLeft);
convertToImageHelper(x + width/2, width/2, y + height/2, height/2,image, node->bottomRight);
}
}
int QuadTree::getImageSizeInBytes()
{
return getImageSizeInBytesHelper(root);
}
int QuadTree::getImageSizeInBytesHelper(Node* node)
{
if (noChildren(node))
{
return PIXEL_SIZE_IN_BYTES;
}
else
{
int sizeTopLeft = getImageSizeInBytesHelper(node->topLeft);
int sizeTopRight = getImageSizeInBytesHelper(node->topRight);
int sizeBottomLeft = getImageSizeInBytesHelper(node->bottomLeft);
int sizeBottomRight = getImageSizeInBytesHelper(node->bottomRight);
return sizeTopLeft + sizeTopRight + sizeBottomLeft + sizeBottomRight;
}
}
void QuadTree::setThreshold(double Threshold)
{
THRESHOLD = Threshold;
}