-
Notifications
You must be signed in to change notification settings - Fork 0
/
QTree.cpp
executable file
·470 lines (414 loc) · 14.9 KB
/
QTree.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
*
* Balanced Quad Tree (pa3)
*
* This file will be used for grading.
*
*/
#include "QTree.h"
// Return the biggest power of 2 less than or equal to n
int biggestPow2(int n) {
if( n < 1 ) return 0;
int v = 1;
while( v <= n ) v <<= 1;
return v >> 1;
}
QTree::Node::Node(PNG & im, pair<int,int> ul, int sz, Node *par)
:upLeft(ul),size(sz),parent(par),nw(NULL),ne(NULL),sw(NULL),se(NULL)
{
var = varAndAvg(im,ul,size,avg); //calculate the color variability for a pixel using its RGB and the average RGB of its neighbours
}
QTree::~QTree(){
clear(); //deallocate all the dynamic (heap) memory associated with a quadtree object
}
QTree::QTree(const QTree & other) {
copy(other);
}
QTree & QTree::operator=(const QTree & rhs){
if (this != &rhs) {
clear();
copy(rhs);
}
return *this;
}
QTree::QTree(PNG & imIn, int leafB, RGBAPixel frameC, bool bal)
: leafBound(leafB), balanced(bal), drawFrame(true), frameColor(frameC)
{
im = imIn;
pair<int,int> rx_ry(0,0);
numLeaf = 1; //before any split, num leaves = 1 (the whole image)
root = new Node(im, rx_ry, biggestPow2(im.width()), NULL);
split(root); //recursively perform node splitting to compress the image
}
QTree::QTree(PNG & imIn, int leafB, bool bal)
: leafBound(leafB), balanced(bal), drawFrame(false)
{
//no frameColor version of the one above
im = imIn;
pair<int,int> rx_ry(0,0);
numLeaf = 1;
root = new Node(im, rx_ry, biggestPow2(im.width()), NULL);
split(root);
}
bool QTree::isLeaf( Node *t ) {
//check if a node has children (a node has either 0 or 4 children)
if(t == NULL) {
return false;
}
return (t->nw == NULL);
}
void QTree::split( Node *t ) {
// FOR BALANCED QTREES-------------------------------------------------
// A split might cause one or two nbrs of the parent of t to split
// to maintain balance. Note that these two nbrs exist (unless they're
// not in the image region) because the current set of leaves are
// balanced.
// if( t is a NW (or NE or SW or SE) child ) then we need to check that
// the North and West (or North and East or South and West or
// South and East) nbrs of t->parent have children. If they don't
// we need to split them.
priority_queue<QTree::Node*, vector<QTree::Node*>, CmpNodes> nodePQ; //PQ that holds all the leaf nodes in the image, and removes, upon requested, the node with the highest variance
if(balanced == false){
splitRecursiveImb(t, nodePQ); //recursive imbalanced splitting
} else {
splitRecursiveBal(t, nodePQ); //recursive balanced splitting
}
}
//helper function for balanced splitting
bool QTree::needsSplit(Node* t, Node* neighbour){
if(t == NULL || neighbour == NULL) return false;
int childSize;
if(isLeaf(t) == true){
childSize = t->size; //compare with the smallest size
} else {
childSize = t->size/2; //compare with the smallest size
}
/*the above distinction (the if-else condition to determine the childSize) is NEEDED because!!:
a leaf node and its parent will have the same neighbour if that neighbour node does not have children (is a leaf)
we have to compare the smallest existing node length with the neighbour to correctly identify the need for a rebalance */
if( (childSize == neighbour->size) || (childSize == (neighbour->size / 2)) || (childSize == (neighbour->size * 2)) ){
return false; //neighbour size is not 'significantly' different, don't split the neighbour yet
}
return true;
}
//coordinate checks to determine NW/NE/SW/SE of parent
bool QTree::isNorthWestChild(Node* t){
return ( (t->upLeft.first == t->parent->upLeft.first) && (t->upLeft.second == t->parent->upLeft.second) );
}
bool QTree::isNorthEastChild(Node* t){
return ( (t->upLeft.first == t->parent->upLeft.first + t->size) && (t->upLeft.second == t->parent->upLeft.second) );
}
bool QTree::isSouthWestChild(Node* t){
return ( (t->upLeft.first == t->parent->upLeft.first) && (t->upLeft.second == t->parent->upLeft.second + t->size) );
}
bool QTree::isSouthEastChild(Node* t){
return ( (t->upLeft.first == t->parent->upLeft.first + t->size) && (t->upLeft.second == t->parent->upLeft.second + t->size) );
}
//non-recursive helper function for balanced split, which simply splits a given node and does nothing else
//why? because maintainBalance() needs a function that will run non-recursively (once) to split a given node (for example, neighbours of a node to maintain the balance)
//because maintainBalance() itself is recursive, it needs this non-rec helper to do a single split of a given node
void QTree::balanceSplit(Node *t, priority_queue<QTree::Node*, vector<QTree::Node*>, CmpNodes>& nodePQ){
pair<int,int> nw_pair(t->upLeft.first, t->upLeft.second);
pair<int,int> ne_pair(t->upLeft.first + t->size/2, t->upLeft.second);
pair<int,int> sw_pair(t->upLeft.first, t->upLeft.second + t->size/2);
pair<int,int> se_pair(t->upLeft.first + t->size/2, t->upLeft.second + t->size/2);
t->nw = new Node(im, nw_pair, t->size/2, t);
t->ne = new Node(im, ne_pair, t->size/2, t);
t->sw = new Node(im, sw_pair, t->size/2, t);
t->se = new Node(im, se_pair, t->size/2, t);
nodePQ.push(t->nw);
nodePQ.push(t->ne);
nodePQ.push(t->sw);
nodePQ.push(t->se);
numLeaf += 3;
}
//recursive function to check if a given node's neighbours (2 of them - NW/NE/SW/SE) need to be split to maintain the balance of the qtree
//if any neighbour needs to be split; split it (balanceSplit()), and call maintainBalance on that node - why?
//because the concept of "maintaining balance" itself is RECURSIVE!!! a split may cause other splits which may cause other splits...
//when to stop? when no other neighbour needs split
void QTree::maintainBalance(Node *t, priority_queue<QTree::Node*, vector<QTree::Node*>, CmpNodes>& nodePQ){
bool split = false;
if(t->parent != NULL){
if(isNorthWestChild(t)){
Node* nnbr = NNbr(t);
split = needsSplit(t, nnbr);
if(split) {
balanceSplit(nnbr, nodePQ);
maintainBalance(nnbr, nodePQ);
}
Node* wnbr = WNbr(t);
split = needsSplit(t, wnbr);
if(split) {
balanceSplit(wnbr, nodePQ);
maintainBalance(wnbr, nodePQ);
}
}
else if(isNorthEastChild(t)){
Node* nnbr = NNbr(t);
split = needsSplit(t, nnbr);
if(split) {
balanceSplit(nnbr, nodePQ);
maintainBalance(nnbr, nodePQ);
}
Node* enbr = ENbr(t);
split = needsSplit(t, enbr);
if(split) {
balanceSplit(enbr, nodePQ);
maintainBalance(enbr, nodePQ);
}
}
else if(isSouthWestChild(t)){
Node* snbr = SNbr(t);
split = needsSplit(t, snbr);
if(split) {
balanceSplit(snbr, nodePQ);
maintainBalance(snbr, nodePQ);
}
Node* wnbr = WNbr(t);
split = needsSplit(t, wnbr);
if(split) {
balanceSplit(wnbr, nodePQ);
maintainBalance(wnbr, nodePQ);
}
}
else if(isSouthEastChild(t)){
Node* snbr = SNbr(t);
split = needsSplit(t, snbr);
if(split) {
balanceSplit(snbr, nodePQ);
maintainBalance(snbr, nodePQ);
}
Node* enbr = ENbr(t);
split = needsSplit(t, enbr);
if(split) {
balanceSplit(enbr, nodePQ);
maintainBalance(enbr, nodePQ);
}
}
}
}
void QTree::splitRecursiveBal(Node *t, priority_queue<QTree::Node*, vector<QTree::Node*>, CmpNodes>& nodePQ){
//if(t->size <= 1) return;
if(numLeaf < leafBound){
if(isLeaf(t) == true){ //we need this check right here! why? (this correction is the line that made my code pass the gradescope tests!)
pair<int,int> nw_pair(t->upLeft.first, t->upLeft.second);
pair<int,int> ne_pair(t->upLeft.first + t->size/2, t->upLeft.second);
pair<int,int> sw_pair(t->upLeft.first, t->upLeft.second + t->size/2);
pair<int,int> se_pair(t->upLeft.first + t->size/2, t->upLeft.second + t->size/2);
t->nw = new Node(im, nw_pair, t->size/2, t);
t->ne = new Node(im, ne_pair, t->size/2, t);
t->sw = new Node(im, sw_pair, t->size/2, t);
t->se = new Node(im, se_pair, t->size/2, t);
nodePQ.push(t->nw);
nodePQ.push(t->ne);
nodePQ.push(t->sw);
nodePQ.push(t->se);
numLeaf += 3;
maintainBalance(t, nodePQ); //note this is different than the imbalanced version - look at the function for info!
}
//well, we need that line because, a node in the PQ that we pop to split may have already been split while balancing the quadtree
//if this is the case, ignore that node and simply pop the next node from the PQ!
Node* next = nodePQ.top();
nodePQ.pop();
splitRecursiveBal(next, nodePQ);
}
}
void QTree::splitRecursiveImb(Node *t, priority_queue<QTree::Node*, vector<QTree::Node*>, CmpNodes>& nodePQ){
//if(t->size <= 1) return;
if(numLeaf < leafBound){
pair<int,int> nw_pair(t->upLeft.first, t->upLeft.second);
pair<int,int> ne_pair(t->upLeft.first + t->size/2, t->upLeft.second);
pair<int,int> sw_pair(t->upLeft.first, t->upLeft.second + t->size/2);
pair<int,int> se_pair(t->upLeft.first + t->size/2, t->upLeft.second + t->size/2);
t->nw = new Node(im, nw_pair, t->size/2, t);
t->ne = new Node(im, ne_pair, t->size/2, t);
t->sw = new Node(im, sw_pair, t->size/2, t);
t->se = new Node(im, se_pair, t->size/2, t);
nodePQ.push(t->nw);
nodePQ.push(t->ne);
nodePQ.push(t->sw);
nodePQ.push(t->se);
//1 leaf removed, 4 leaves added
numLeaf += 3;
Node* next = nodePQ.top(); //find the leaf node with the highest variance (= largest sum of color differences between a pixel in the node and the average of all pixels in that node)
nodePQ.pop(); //remove - the node that will be split next
splitRecursiveImb(next, nodePQ);
}
}
/* NNbr(t)
* return the same-sized quad tree node that is north of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::NNbr(Node *t) {
if(t == NULL) return NULL;
else if(t->parent == NULL) return NULL;
else if(isSouthEastChild(t)) return t->parent->ne; //one of the base cases: if t is se, t's north is in the same parent node
else if(isSouthWestChild(t)) return t->parent->nw; //one of the base cases: if t is sw, t's north is in the same parent node
//else, we have to travel in the tree to find a new node
else{
Node* n = NNbr(t->parent);
if(n == NULL) return NULL;
if(isLeaf(n)) return n; //t may not have the same sized neighbour (ie. may not have children, may be a leaf) - in that case, simply return the node we got back from recursion
//if the node we got back has children, return the correct one of the children:
if(isNorthEastChild(t)) return n->se;
else if(isNorthWestChild(t)) return n->sw;
}
}
/* SNbr(t)
* return the same-sized quad tree node that is south of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::SNbr(Node *t) {
if(t == NULL) {
return NULL;
}
else if(t->parent == NULL) {
return NULL;
}
else if(isNorthEastChild(t)) {
return t->parent->se;
}
else if(isNorthWestChild(t)){
return t->parent->sw;
}
else{
Node* n = SNbr(t->parent);
if(n == NULL){
return NULL;
}
if(isLeaf(n)){
return n;
}
if(isSouthEastChild(t)){
return n->ne;
}
else if(isSouthWestChild(t)) {
return n->nw;
}
}
}
/* ENbr(t)
* return the same-sized quad tree node that is east of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::ENbr(Node *t) {
if(t == NULL) return NULL;
else if(t->parent == NULL) return NULL;
else if(isNorthWestChild(t)) return t->parent->ne;
else if(isSouthWestChild(t)) return t->parent->se;
else{
Node* n = ENbr(t->parent);
if(n == NULL){
return NULL;
}
if(isLeaf(n)){
return n;
}
if(isNorthEastChild(t)) return n->nw;
else if(isSouthEastChild(t)) return n->sw;
}
}
/* WNbr(t)
* return the same-sized quad tree node that is west of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::WNbr(Node *t) {
if(t == NULL) return NULL;
else if(t->parent == NULL) return NULL;
else if(isNorthEastChild(t)) return t->parent->nw;
else if(isSouthEastChild(t)) return t->parent->sw;
else{
Node* n = WNbr(t->parent);
if(n == NULL){
return NULL;
}
if(isLeaf(n)){
return n;
}
if(isNorthWestChild(t)) return n->ne;
else if(isSouthWestChild(t)) return n->se;
}
}
bool QTree::write(string const & fileName){
PNG out_im(im.width(), im.height());
findLeaves(root, out_im);
// include the following line to write the image to file.
return(out_im.writeToFile(fileName));
}
void QTree::findLeaves(Node* n, PNG& newIm){
if(isLeaf(n) == true){
colorSquare(n, newIm); //color each leaf in the new image (the compressed image that will be the output)
} else {
findLeaves(n->nw, newIm);
findLeaves(n->ne, newIm);
findLeaves(n->sw, newIm);
findLeaves(n->se, newIm);
}
}
void QTree::colorSquare(Node* n, PNG& newIm){
pair<int,int> upleft = n->upLeft;
int startX = upleft.first;
int startY = upleft.second;
int endX = startX + n->size;
int endY = startY + n->size;
for(int x = startX; x < endX; x++){
for(int y = startY; y < endY; y++){
*(newIm.getPixel(x, y)) = n->avg; //color all the pixels in the node with the average color of the node
}
}
//if drawFrame == true, then color the borders with the frameColor
//note that there are 4 borders of a square:) 1st loop overrides the color of the horizontal borders, 2nd loop overrides vertical
if(drawFrame == true){
pair<int,int> upleft = n->upLeft;
int startX = upleft.first;
int startY = upleft.second;
int endX = startX + n->size - 1;
int endY = startY + n->size - 1;
for(int x = startX; x < endX + 1; x++){
*(newIm.getPixel(x, startY)) = frameColor;
*(newIm.getPixel(x, endY)) = frameColor;
}
for(int y = startY; y < endY + 1; y++){
*(newIm.getPixel(startX, y)) = frameColor;
*(newIm.getPixel(endX, y)) = frameColor;
}
}
//note that we do the border coloring for each square (each node!), not just for the whole image!
}
//non-rec clear
void QTree::clear() {
clearNodes(root);
}
//rec clear
void QTree::clearNodes(Node* &orig_n){
if(orig_n != NULL){
clearNodes(orig_n->nw);
clearNodes(orig_n->ne);
clearNodes(orig_n->sw);
clearNodes(orig_n->se);
delete orig_n;
orig_n = NULL;
}
}
//non-rec copy
void QTree::copy(const QTree & orig) {
numLeaf = orig.numLeaf;
im = orig.im;
leafBound = orig.leafBound;
balanced = orig.balanced;
drawFrame = orig.drawFrame;
frameColor = orig.frameColor;
root = copyNodes(orig.root);
}
//rec copy; returns a new root pointer, and also creates new pointers to every node in the original image
QTree::Node* QTree::copyNodes(const Node* orig_n){
Node* n = NULL;
if(orig_n != NULL){
n = new Node(this->im, orig_n->upLeft, orig_n->size, orig_n->parent);
n->nw = copyNodes(orig_n->nw);
n->ne = copyNodes(orig_n->ne);
n->sw = copyNodes(orig_n->sw);
n->se = copyNodes(orig_n->se);
}
return n;
}