-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqtree.cpp
204 lines (180 loc) · 4.33 KB
/
sqtree.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
#include "sqtree.h"
// First Node constructor, given.
SQtree::Node::Node(pair<int, int> ul, int w, int h, RGBAPixel a, double v)
: upLeft(ul), width(w), height(h), avg(a), var(v), NW(NULL), NE(NULL),
SE(NULL), SW(NULL) {}
// Second Node constructor, given
SQtree::Node::Node(stats &s, pair<int, int> ul, int w, int h)
: upLeft(ul), width(w), height(h), NW(NULL), NE(NULL), SE(NULL), SW(NULL)
{
avg = s.getAvg(ul, w, h);
var = s.getVar(ul, w, h);
}
// SQtree destructor, given.
SQtree::~SQtree() { clear(); }
// SQtree copy constructor, given.
SQtree::SQtree(const SQtree &other) { copy(other); }
// SQtree assignment operator, given.
SQtree &SQtree::operator=(const SQtree &rhs)
{
if (this != &rhs)
{
clear();
copy(rhs);
}
return *this;
}
/**
* SQtree constructor given tolerance for variance.
*/
SQtree::SQtree(PNG &imIn, double tol)
{
stats s(imIn);
pair<int, int> ul = make_pair(0, 0);
root = buildTree(s, ul, imIn.width(), imIn.height(), tol);
}
/**
* Helper for the SQtree constructor.
*/
SQtree::Node *SQtree::buildTree(stats &s, pair<int, int> &ul, int w, int h,
double tol)
{
if (h * w == 0)
{
return NULL;
}
Node *curr = new Node(s, ul, w, h);
if ((w == 1 && h == 1) || (s.getVar(ul, w, h) <= tol))
{
return curr;
}
double minvariance = s.getVar(ul, w, h);
int bestx = 0;
int besty = 0;
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
if (!(x == 0 && y == 0)) {
double maxvariance = -1;
if (x > 0 && y > 0) {
double d0 = s.getVar(ul, x, y);
maxvariance = max(maxvariance, d0);
}
if (y > 0) {
pair<int, int> ul1 = make_pair(x + ul.first, ul.second);
double d1 = s.getVar(ul1, w - x, y);
maxvariance = max(maxvariance, d1);
}
if (x > 0) {
pair<int, int> ul2 = make_pair(ul.first, ul.second + y);
double d2 = s.getVar(ul2, x, h - y);
maxvariance = max(maxvariance, d2);
}
pair<int, int> ul3 = make_pair(ul.first + x, ul.second + y);
double d3 = s.getVar(ul3, w - x, h - y);
maxvariance = max(maxvariance, d3);
if (maxvariance < minvariance) {
minvariance = maxvariance;
bestx = x;
besty = y;
}
}
}
}
if (bestx > 0 && besty > 0) {
curr->NW = buildTree(s, ul, bestx, besty, tol);
}
if (besty > 0) {
pair<int, int> ul1 = make_pair(bestx + ul.first, ul.second);
curr->NE = buildTree(s, ul1, w - bestx, besty, tol);
}
if (bestx > 0) {
pair<int, int> ul2 = make_pair(ul.first, ul.second + besty);
curr->SW = buildTree(s, ul2, bestx, h - besty, tol);
}
pair<int, int> ul3 = make_pair(ul.first + bestx, ul.second + besty);
curr->SE = buildTree(s, ul3, w - bestx, h - besty, tol);
return curr;
}
/**
* Render SQtree and return the resulting image.
*/
PNG SQtree::render()
{
PNG im(root->width, root->height);
render(im, root);
return im;
}
void SQtree::render(PNG &im, Node *curr)
{
if (!curr)
{
return;
}
//check if node is a leaf
if (!(curr->NW || curr->NE || curr->SE || curr->SW))
{
for (int x = curr->upLeft.first; x < curr->upLeft.first + curr->width;
x++)
{
for (int y = curr->upLeft.second; y < curr->upLeft.second + curr->height;
y++)
{
RGBAPixel *pix = im.getPixel(x, y);
*pix = curr->avg;
}
}
}
else
{
render(im, curr->NW);
render(im, curr->NE);
render(im, curr->SE);
render(im, curr->SW);
}
}
/**
* Delete allocated memory.
*/
void SQtree::clear() { clear(root); }
void SQtree::clear(Node *&n)
{
if (n != NULL)
{
clear(n->NW);
clear(n->NE);
clear(n->SE);
clear(n->SW);
delete n;
n = NULL;
}
}
void SQtree::copy(const SQtree &other)
{
Node *otheroot = other.root;
root = copy(otheroot);
}
SQtree::Node *SQtree::copy(const Node *n)
{
if (n)
{
Node *temp = new Node(n->upLeft, n->width, n->height, n->avg, n->var);
temp->NE = copy(n->NE);
temp->NW = copy(n->NW);
temp->SE = copy(n->SE);
temp->SW = copy(n->SW);
return temp;
}
else
{
return NULL;
}
}
int SQtree::size() { return size(root); }
int SQtree::size(const Node *n)
{
if (n != NULL)
{
return 1 + size(n->NW) + size(n->NE) + size(n->SE) + size(n->SW);
}
return 0;
}