File size: 5,174 Bytes
7c6501b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "suppl.h"

//-----------------------IMG MANIPULATION FUNC-----------------------------//

// reads the .ppm file and stores the data in 2D matrix form
pixels **read(int *height, int *width, char *file)
{
    FILE *f;
    char magic_num[3];
    int color;
    int i;

    f = fopen(file, "rb");

    fscanf(f, "%s", magic_num);
    if (magic_num[0] != 'P' || magic_num[1] != '6')
    {
        printf("Invalid Image Format!\n");
        exit;
    }
    fscanf(f, "%d ", width);
    fscanf(f, "%d\n", height);
    fscanf(f, "%d", &color);

    char garbage;
    fread(&garbage, sizeof(char), 1, f);

    pixels **r = (pixels **)malloc(sizeof(pixels *) * (*height));

    for (i = 0; i < (*height); i++)
    {
        r[i] = malloc(sizeof(pixels) * (*width));
        fread(r[i], sizeof(pixels), (*width), f);
    }

    fclose(f);
    return r;
}

// gives the output file in .ppm format
void outputFile(pixels **mat, char *file, int size)
{

    FILE *f = fopen(file, "w");

    fprintf(f, "P6\n");
    fprintf(f, "%d %d\n", size, size);
    fprintf(f, "255\n");

    int i;
    for (i = 0; i < size; i++)
    {
        fwrite(mat[i], sizeof(pixels), size, f);
    }
    fclose(f);
}

int getMean(pixels **matrix, qtNode **t, int x, int y, int size)
{
    int i, j;
    unsigned long long int blue = 0, green = 0, red = 0, mean = 0;

    (*t) = malloc(sizeof(qtNode));
    (*t)->area = size * size;

    for (i = y; i < y + size; i++)
        for (j = x; j < x + size; j++)
        {
            blue = blue + matrix[i][j].blue;
            green = green + matrix[i][j].green;
            red = red + matrix[i][j].red;
        }

    blue = blue / ((*t)->area);
    red = red / ((*t)->area);
    green = green / ((*t)->area);

    (*t)->p.blue = blue;
    (*t)->p.red = red;
    (*t)->p.green = green;

    // Compute score

    for (i = y; i < y + size; i++)
    {
        for (j = x; j < x + size; j++)
        {
            mean = mean + ((red - matrix[i][j].red) * (red - matrix[i][j].red)) + ((green - matrix[i][j].green) * (green - matrix[i][j].green)) + ((blue - matrix[i][j].blue) * (blue - matrix[i][j].blue));
        }
    }

    mean = mean / (3 * (*t)->area);

    return mean;
}

//--------------------------QUAD TREE FUNCTIONS------------------------------//

// stores the quad tree info in array

void traversal(qtNode *node, qtNode **vector[], unsigned int *index)
{
    if (node != NULL)
    {
        if ((*index) > 0)
            (*vector) = realloc((*vector), sizeof(qtNode *) * ((*index) + 1));

        (*vector)[(*index)] = node;
        node->index = (*index);
        (*index)++;

        traversal(node->topLeft, vector, index);
        traversal(node->topRight, vector, index);
        traversal(node->bottomRight, vector, index);
        traversal(node->bottomLeft, vector, index);
    }
    else
        return;
}

// transfers the contents to qtInfo
void copyToArr(qtNode **vp, qtInfo **v, int index)
{

    unsigned int i;

    for (i = 0; i < index; i++)
    {
        (*v)[i].red = vp[i]->p.red;
        (*v)[i].blue = vp[i]->p.blue;
        (*v)[i].green = vp[i]->p.green;
        (*v)[i].area = vp[i]->area;

        if (vp[i]->topLeft != NULL)
            (*v)[i].topLeft = vp[i]->topLeft->index;
        else
            (*v)[i].topLeft = -1;

        if (vp[i]->topRight != NULL)
            (*v)[i].topRight = vp[i]->topRight->index;
        else
            (*v)[i].topRight = -1;

        if (vp[i]->bottomRight != NULL)
            (*v)[i].bottomRight = vp[i]->bottomRight->index;
        else
            (*v)[i].bottomRight = -1;

        if (vp[i]->bottomLeft != NULL)
            (*v)[i].bottomLeft = vp[i]->bottomLeft->index;
        else
            (*v)[i].bottomLeft = -1;
    }
}

// reads the data from matrix
void readTree(qtInfo *vec, qtNode **node, int i)
{
    (*node) = malloc(sizeof(qtNode));

    (*node)->p.red = vec[i].red;
    (*node)->p.blue = vec[i].blue;
    (*node)->p.green = vec[i].green;
    (*node)->area = vec[i].area;
    (*node)->index = i;

    if (vec[i].topLeft != -1 && vec[i].topRight != -1 && vec[i].bottomLeft != -1 && vec[i].bottomRight != -1)
    {
        readTree(vec, &(*node)->topLeft, vec[i].topLeft);
        readTree(vec, &(*node)->topRight, vec[i].topRight);
        readTree(vec, &(*node)->bottomLeft, vec[i].bottomLeft);
        readTree(vec, &(*node)->bottomRight, vec[i].bottomRight);
    }
    else
    {
        (*node)->topLeft = NULL;
        (*node)->topRight = NULL;
        (*node)->bottomLeft = NULL;
        (*node)->bottomRight = NULL;
    }
}

void destroyTree(qtNode** t)
{
    if(!t)
    {
        destroyTree(&(*t)->topLeft);
        destroyTree(&(*t)->topRight);
        destroyTree(&(*t)->bottomLeft);
        destroyTree(&(*t)->bottomRight);
    }
    free(*t);
}

//-----------------------------OTHER FUNCS---------------------------------//
int min(int a,int b)
{
    if(a>b)
    {
        return a;
    }
    else
    {
        return b;
    }
}