Skip to content
Snippets Groups Projects
Commit ef4782fa authored by thib's avatar thib
Browse files

tree <-> file

parent 2342f229
Branches
No related tags found
No related merge requests found
cc=gcc cc=gcc
CFLAGS=-Wextra -Wall -g -fsanitize=address CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak
LFLAGS=-fsanitize=address -lm LFLAGS= -lm
# -fsanitize=leak mac
main: main.x main: main.x
./main.x ./main.x
......
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
// matrix pixels; // matrix pixels;
// } pgm; // } pgm;
void pgm_create(pgm *p,int max ,matrix mx){
p->max=max;
p->pixels=mx;
}
pgm_error pmg_read_from_file(pgm *p, char *filename) { pgm_error pmg_read_from_file(pgm *p, char *filename) {
if (NULL == filename || NULL == p) { if (NULL == filename || NULL == p) {
return failure; return failure;
......
This diff is collapsed.
File deleted
File deleted
#include "PGM.h"
#include "quadTree.h" #include "quadTree.h"
int main() { int main() {
pgm p; node* tree=tree_from_file("pgm/buzz_octets.pgm");
pmg_read_from_file(&p, "buzz_octets.pgm"); tree_to_file(tree, "test2.pgm");
node* tree=create_tree(2);
while (!leaf(tree->child[0]))
{
printf("not leaf\n");
/* code */
tree=tree->child[0];
}
// printf("%f val %d\n",tree->child[0]->child[0]->ave,depth(tree)-1);
// position(3,3,trkee,4);
// matrix2tree(&p.pixels,tree);
// destroy_tree(tree);
// pmg_write_to_file(&p,"buzz.pgm");
// matrix_destroy(&p.pixels); tree_destroy(tree);
} }
\ No newline at end of file
Source diff could not be displayed: it is too large. Options to address this: view the blob.
#include "quadTree.h" #include "quadTree.h"
#include "matrix.h"
#include <limits.h>
#include <math.h> #include <math.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// typedef struct _node // typedef struct _node
...@@ -8,126 +11,178 @@ ...@@ -8,126 +11,178 @@
// double ave_sq; // moyenne des carrés des valeurs des enfants // double ave_sq; // moyenne des carrés des valeurs des enfants
// struct _node *child[CHILDREN]; // struct _node *child[CHILDREN];
// } node; // } node;
bool leaf(node *nd) { return NULL == nd->child[0]; }
node *create_node() { // utils
node *nd = malloc(sizeof(node)); bool is_leaf(node *nd) { return (NULL == nd->child[0]); }
int max(int x, int y) { return (x >= y ? x : y); }
int array_max(int size, int tab[size]) {
int m = INT_MIN;
for (int i = 0; i < size; i++) {
m = max(m, tab[i]);
}
return m;
}
int tree_max(node *tree) {
int m = INT_MIN;
if (!is_leaf(tree)) {
for (int i = 0; i < CHILDREN; i++) { for (int i = 0; i < CHILDREN; i++) {
nd->child[i] = calloc(1, sizeof(node)); m = max(m, tree->child[i]->value);
// if(nd->child[i]!=NULL)printf("wtf"); m = max(m, tree_max(tree->child[i]));
}
} }
nd->ave = 0; return m;
nd->ave_sq = 0;
return nd;
} }
node *create_tree(int depth) { int depth(node *a) {
node *a = create_node(); int p[CHILDREN] = {0};
if (depth > 0) { if (is_leaf(a)) {
return 0;
} else {
for (int i = 0; i < CHILDREN; i++) { for (int i = 0; i < CHILDREN; i++) {
a->child[i] = create_node(); p[i] = 1 + depth(a->child[i]);
a->child[i] = create_tree(depth - 1);
} }
return array_max(CHILDREN, p);
} }
return a;
} }
void destroy_tree(node *tree) { void print(node *arbre, int decal, char *sep) {
if (!leaf(tree)) { if (NULL != arbre) {
for (int i = 0; i < decal; i++) {
printf("%s", sep);
}
printf("%d\n", arbre->value);
decal++;
if (!is_leaf(arbre)) {
for (int i = 0; i < CHILDREN; i++) { for (int i = 0; i < CHILDREN; i++) {
destroy_tree(tree->child[i]); print(arbre->child[i], decal, sep);
free(tree); }
} }
} }
} }
void swap(node *a, node *b) { // Fonction utile pour les symétries
node tmp = *a; void swap(void **ptr1, void **ptr2) {
*a = *b; void *tmp = *ptr1;
*b = tmp; *ptr1 = *ptr2;
*ptr2 = tmp;
} }
void symetry(node *arbre, int bit) { void sum(node *arbre) {
if (!leaf(arbre)) { if (!is_leaf(arbre)) {
for (int i = 0; i < CHILDREN; i++) { for (int i = 0; i < CHILDREN; i++) {
if (i < (i ^ bit)) { sum(arbre->child[i]);
swap(arbre->child[i], arbre->child[i ^ bit]);
}
} }
for (int i = 0; i < CHILDREN; i++) { for (int i = 0; i < CHILDREN; i++) {
symetry(arbre->child[i], bit); arbre->value += arbre->child[i]->value;
} }
} }
} }
node *position(int li, int col, node *a, int d) { node *tree_create(int depth) {
node *a = calloc(1, sizeof(node));
if(leaf(a)){ if (depth > 0) {
printf("is leaf %d\n",d); for (int i = 0; i < CHILDREN; i++) {
a->child[i] = tree_create(depth - 1);
a->value = 0;
}
}
return a; return a;
} }
printf("not leaf %d\n",d);
node *crt = a;
void tree_destroy(node *tree) {
if (!is_leaf(tree)) {
for (int i = 0; i < CHILDREN; i++) {
tree_destroy(tree->child[i]);
}
}
free(tree);
}
// void symetry(node *arbre, int bit) {
// if (!leaf(arbre)) {
// for (int i = 0; i < CHILDREN; i++) {
// if (i < (i ^ bit)) {
// swap(arbre->child[i], arbre->child[i ^ bit]);
// }
// }
// for (int i = 0; i < CHILDREN; i++) {
// symetry(arbre->child[i], bit);
// }
// }
// }
node *position(int li, int col, node *a, int d) {
node *crt = a;
do {
int size = pow(2, d); int size = pow(2, d);
int half = size / 2; int half = size / 2;
int i; int i;
li %= size;
col %= size;
if (li < half && col < half) { if (li < half && col < half) {
i = 0; i = 0;
}else if(li<half&&col>half) } else if (li < half && col >= half) {
{
i = 1; i = 1;
} } else if (li >= half && col < half) {
else if(li>half&&col<half)
{
i = 2; i = 2;
} } else if (li >= half && col >= half) {
else if(li>half&&col>half)
{
i = 3; i = 3;
} }
// printf("%d \n\n\n",i);
crt = crt->child[i]; crt = crt->child[i];
crt=position(li%2,col%2,crt,d-1); d--;
} while (!is_leaf(crt));
return crt; return crt;
} }
// à compléter: tant qu’on n’est pas sur une feuille, déplacer // à compléter: tant qu’on n’est pas sur une feuille, déplacer
// <crt> sur un enfant selon valeur du dième bit de <li> et <col> // <crt> sur un enfant selon valeur du dième bit de <li> et <col>
void matrix2tree(matrix *mat, node *arbre) { void matrix2tree(matrix *mat, node *arbre) {
int d = depth(arbre) - 1; int d = depth(arbre);
for (int m = 0; m < mat->m; m++) { for (int m = 0; m < mat->m; m++) {
for (int n = 0; n < mat->n; n++) { for (int n = 0; n < mat->n; n++) {
node *crt = position(m, n, arbre, d); node *crt = position(m, n, arbre, d);
crt->ave = mat->data[m][n]; crt->value = mat->data[m][n];
crt->ave_sq = (crt->ave) * (crt->ave); // crt->ave = mat->data[m][n];
// crt->ave_sq = (crt->ave) * (crt->ave);
} }
} }
} }
int maxf(int a, int b) { void tree2matrix(node *arbre, matrix *mat) {
if (a > b) { int d = depth(arbre);
b = a; for (int m = 0; m < mat->m; m++) {
} for (int n = 0; n < mat->n; n++) {
return b; node *crt = position(m, n, arbre, d);
mat->data[m][n] = crt->value;
// crt->ave = mat->data[m][n];
// crt->ave_sq = (crt->ave) * (crt->ave);
} }
int depth(node *nd) {
int p[CHILDREN];
memset(p, 0, CHILDREN * sizeof(int));
if (leaf(nd)) {
return 0;
} else {
for (int i = 0; i < CHILDREN; i++) {
p[i] = 1 + depth(nd->child[i]);
} }
int m = -1;
for (int i = 0; i < CHILDREN; i++) {
m = maxf(m, p[i]);
} }
return m;
node *tree_from_file(char *filename) {
pgm p;
pmg_read_from_file(&p, filename);
node *tree = tree_create((int)log2(p.pixels.m));
matrix2tree(&p.pixels, tree);
matrix_destroy(&p.pixels);
return tree;
} }
void tree_to_file(node *tree, char *filename) {
matrix mx;
int size = pow(2, depth(tree));
int max = tree_max(tree);
matrix_alloc(&mx, size, size);
tree2matrix(tree, &mx);
pgm out;
out.max = max;
out.pixels = mx;
pmg_write_to_file(&out, filename);
matrix_destroy(&mx);
} }
\ No newline at end of file
...@@ -7,11 +7,14 @@ ...@@ -7,11 +7,14 @@
// #include <malloc.h> // #include <malloc.h>
#include "matrix.h" #include "matrix.h"
#include <string.h> #include <string.h>
#include <math.h>
#include "PGM.h"
#define CHILDREN 4 #define CHILDREN 4
typedef struct _node typedef struct _node
{ {
int value;
double ave; // moyenne des valeurs des enfants double ave; // moyenne des valeurs des enfants
double ave_sq; // moyenne des carrés des valeurs des enfants double ave_sq; // moyenne des carrés des valeurs des enfants
struct _node *child[CHILDREN]; struct _node *child[CHILDREN];
...@@ -19,16 +22,25 @@ typedef struct _node ...@@ -19,16 +22,25 @@ typedef struct _node
// typedef node* arbre; // typedef node* arbre;
// retourne un pointeur sur un arbre complet de profondeur <depth> bool is_leaf(node *nd);
node *create_tree(int depth);
void destroy_tree(node* tree);
// retourne la profondeur d'un arbre <nd> node *tree_create(int depth);
int depth(node *nd);
// retourne un pointeur sur une feuille d'un arbre <a> de profondeur void tree_destroy(node *tree);
// <d> qui correspond à une position <li>,<co> dans une image 2d2
node *position(int li, int co, node *a, int d); int depth(node *a);
void print(node *arbre, int decal, char *sep);
// Fonction utile pour les symétries
void swap(void **ptr1, void **ptr2);
void sum(node *arbre);
node *position(int li, int col, node *a, int d);
// à compléter: tant qu’on n’est pas sur une feuille, déplacer
// <crt> sur un enfant selon valeur du dième bit de <li> et <col>
// transfère les données d'une image <mat> dans un arbre <arbre> // transfère les données d'une image <mat> dans un arbre <arbre>
void matrix2tree(matrix *mat, node *arbre); void matrix2tree(matrix *mat, node *arbre);
...@@ -52,6 +64,9 @@ void compress(node *arbre, double seuil); ...@@ -52,6 +64,9 @@ void compress(node *arbre, double seuil);
bool leaf(node *nd); bool leaf(node *nd);
node *tree_from_file(char *filename);
void tree_to_file(node *tree, char *filename);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment