diff --git a/.gitignore b/.gitignore index 64c857e696e8e2211a71053619f7e6ab2cd7eac5..7772ce5adf0a2f9aa93beab485fd0c9b44906ea4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ test tests Exe exe -.vscode \ No newline at end of file +.vscode +*.pgm \ No newline at end of file diff --git a/Matrix.c b/Matrix.c index baa03b4014ac5cd93ed6d0e06154d1cf544e75b0..820cd2595bf12da799fea159c584f7c40ea5a09c 100644 --- a/Matrix.c +++ b/Matrix.c @@ -8,19 +8,19 @@ #include "Matrix.h" // Alloc memory for matrix -error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) +error_code matrix_alloc(matrix *mat, int32_t col, int32_t lin) { - mat->data = malloc(m * sizeof(int32_t*)); - mat->col = m; - mat->lin = n; + mat->data = malloc(lin * sizeof(int32_t*)); + mat->col = col; + mat->lin = lin; if(mat->data == NULL){ return UNINITIALIZED; } - for(int i=0;i<m;i+=1) + for(int i=0;i<col;i+=1) { - mat->data[i] = malloc(n * sizeof(int32_t)); + mat->data[i] = malloc(lin * sizeof(int32_t)); } if(mat->data == NULL){ @@ -70,16 +70,16 @@ error_code matrix_destroy(matrix *mat){ } // Init matrix from array of int32 -error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n,int32_t data[]) +error_code matrix_init_from_array(matrix *mat, int32_t lin, int32_t col,int32_t data[]) { // if (mat->data != NULL){ // matrix_destroy(mat); // } - matrix_alloc(mat, m,n); + matrix_alloc(mat, lin,col); matrix_init(mat); - for(int i=0,k=0;i<m;i+=1) + for(int i=0,k=0;i<lin;i+=1) { - for(int j=0;j<n;j+=1,k+=1) + for(int j=0;j<col;j+=1,k+=1) { mat->data[i][j] = data[k]; } diff --git a/Matrix.h b/Matrix.h index 4f158cd8c20481a01186bd2dcc895ea084894327..717d4c91a6bc67b8431dda944ec7ae08eae7a5b3 100644 --- a/Matrix.h +++ b/Matrix.h @@ -20,7 +20,7 @@ typedef struct _matrix { bool matrix_is_equal(matrix mat1, matrix mat2); error_code matrix_init(matrix *mat); -error_code matrix_alloc(matrix *mat, int32_t m, int32_t n); +error_code matrix_alloc(matrix *mat, int32_t lin, int32_t col); error_code matrix_destroy(matrix *mat); error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n,int32_t data[]); error_code matrix_to_array(matrix mat,int32_t *table); diff --git a/main.c b/main.c index 0e330a951667e043c477e72b7eabcd3f46eae733..682a15d97cef8bdd71199df4276e528cb4776780 100644 --- a/main.c +++ b/main.c @@ -14,16 +14,36 @@ int main() pgm_read_from_file(p,"buzz.pgm"); uint32_t val = quadtree_get_depth_from_image_size(p->pixels.col); printf("Depth : %u\n",val); + + + // matrix_print(p->pixels); node* tree = quadtree_tree_create(val); - matrix_print(p->pixels); + // matrix_print(p->pixels); quadtree_matrix2tree(&p->pixels,tree); - quadtree_print(tree,1,"|"); + pgm* pp = malloc(1 * sizeof(pgm)); + pp->max = p->max; + matrix_alloc(&pp->pixels,p->pixels.lin,p->pixels.col); + // matrix mat; + // matrix_alloc(&mat,p->pixels.lin,p->pixels.col); + // matrix_init(&mat); + + quadtree_tree2matrix(tree,&pp->pixels); + // // matrix_print(mat); + + // // quadtree_print(tree,1,"|"); + + // pgm *pp = malloc(1 * sizeof(pgm)); + // pp->max = 256; + // pp->pixels = mat; + pgm_write_from_file(pp,"Test.pgm"); matrix_destroy(&p->pixels); - quadtree_tree_destroy(&tree); + matrix_destroy(&pp->pixels); + quadtree_tree_destroy_clean(&tree); free(p); + free(pp); return 0; } \ No newline at end of file diff --git a/quadtree.c b/quadtree.c index 178184df73f4f71ad730379353d567c7ae71d06f..61bd55ddb269c601a7265161387153f476d39fe9 100644 --- a/quadtree.c +++ b/quadtree.c @@ -64,7 +64,7 @@ void quadtree_print(node *arbre, int32_t decal, char *sep) { printf("%s", sep); } - printf("%d\n", arbre->info); + printf("%d\n", (uint32_t)arbre->data); decal++; if (!quadtree_is_leaf(arbre)) { @@ -94,7 +94,7 @@ void quadtree_sum(node *arbre) } for (int32_t i = 0; i < CHILDREN; i++) { - arbre->info += arbre->child[i]->info; + arbre->data += arbre->child[i]->data; } } } @@ -106,13 +106,29 @@ node *quadtree_position(int32_t li, int32_t col, node *a, int32_t d) { int32_t ligne = (li>>d) & 1; // Permet de sélectionne le bit à considérer en fonction de la profondeur; Exemple: 10 >> 1 = 1 int32_t colonne = (col>>d) & 1; //Exemple: 10 >> 1 = 1; - int32_t index = ligne<<1 | colonne; //Exemple: 1<<1 = 10 | 1(col) = 11=>3 + int32_t index = (ligne<<1) | colonne; // Exemple: 1<<1 = 10 | 1(col) = 11=>3 + crt = crt->child[index]; d--; } while (!quadtree_is_leaf(crt)); return crt; } +double quadtree_position_value(int32_t li, int32_t col, node *a, int32_t d) +{ + node *crt = a; + do + { + int32_t ligne = (li>>d) & 1; // Permet de sélectionne le bit à considérer en fonction de la profondeur; Exemple: 10 >> 1 = 1 + int32_t colonne = (col>>d) & 1; //Exemple: 10 >> 1 = 1; + int32_t index = (ligne<<1) | colonne; // Exemple: 1<<1 = 10 | 1(col) = 11=>3 + + crt = crt->child[index]; + d--; + } while (!quadtree_is_leaf(crt)); + return crt->data; +} + void quadtree_matrix2tree(matrix *mat, node *arbre) { int32_t d = quadtree_depth(arbre) - 1; @@ -121,11 +137,36 @@ void quadtree_matrix2tree(matrix *mat, node *arbre) for (int32_t co = 0; co < mat->col; co++) { node *crt = quadtree_position(li, co, arbre, d); - crt->info = mat->data[li][co]; + crt->data = mat->data[li][co]; } } } +void quadtree_tree2matrix(node *tree, matrix *mat) +{ + int32_t d = quadtree_depth(tree) - 1; + for (int32_t li = 0; li < mat->lin; li++) + { + for (int32_t co = 0; co < mat->col; co++) + { + mat->data[li][co] = quadtree_position_value(li, co, tree, d); + } + } +} + +void quadtree_vertical_symetry(node *tree) +{ + // Inversé les colones +} + +void quadtree_horizontal_symetry(node *tree) +{ +} + +void quadtree_central_symetry(node *tree) +{ +} + void quadtree_tree_destroy(node **tree) { node *a = *tree; @@ -140,4 +181,22 @@ void quadtree_tree_destroy(node **tree) quadtree_tree_destroy(&a->child[i]); } } +} + +void quadtree_tree_destroy_clean(node **tree) +{ + if(NULL != *tree) + { + if(!quadtree_is_leaf(*tree)) + { + for (uint32_t i = 0; i < CHILDREN; i+=1) + { + quadtree_tree_destroy_clean(&(*tree)->child[i]); + } + } + else + { + free(*tree); + } + } } \ No newline at end of file diff --git a/quadtree.h b/quadtree.h index 1fdf349a5d848cfa84f1776de5041ff1f12346a1..96910805d9458262adf20665df23b60c7e7de1b5 100644 --- a/quadtree.h +++ b/quadtree.h @@ -9,8 +9,9 @@ #define CHILDREN 4 -typedef struct _node { - int info; +typedef struct _node { + double data; + double average_2; struct _node* child[CHILDREN]; } node; @@ -36,4 +37,8 @@ node *quadtree_position(int32_t li, int32_t col, node *a, int32_t d); void quadtree_matrix2tree(matrix *mat, node *arbre); -void quadtree_tree_destroy(node **tree); \ No newline at end of file +void quadtree_tree_destroy(node **tree); + +void quadtree_tree2matrix(node *tree,matrix* mat); + +void quadtree_tree_destroy_clean(node **tree); \ No newline at end of file