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
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "pgm.h"
#include "fbit.h"
#define SEUIL 10
typedef struct
{
int x1;int y1;
int x2;int y2;
float moyenne;
double variance;
}Bloc;
typedef struct quad
{
Bloc bloc;
struct quad * premier;
struct quad * deuxieme;
struct quad * troisieme;
struct quad * quatrieme;
} Quadtree;
Quadtree * coder(Image * img,Quadtree * arbre,FBitBuffer * flux);
void decoder(Quadtree * arbre,FBitBuffer * flux);
int main (void)
{
int choix;
int val;
FBitBuffer * flux;
Image * img;
Quadtree * arbre;
img=pgm_image_read("./peppers.pgm");
flux=fopen_bits("quad.quad","w");
fwrite_bits(img->hauteur, 8, flux);
fwrite_bits(img->largeur, 8, flux);
arbre=(Quadtree*) malloc(sizeof(Quadtree));
arbre->bloc.x1=0;arbre->bloc.y1=0;
arbre->bloc.x2=img->largeur;arbre->bloc.y2=img->hauteur;
arbre->bloc.moyenne=0;arbre->bloc.variance=0;
// fid=fopen("z.quad","w");
arbre=coder(img,arbre,flux);
decoder(arbre,flux);
// val=pgm_image_write(img,"./coder2.pgm",0);
pgm_image_write(img, "test.pgm", 0);
fclose_bits(flux);
}
return (0);
}
Quadtree * coder(Image * img,Quadtree * arbre,FBitBuffer * flux)
{
int x1,y1,x2,y2,i,j,largeur,hauteur,tmp,compteur;
float tmp1x2,tmp1y2,tmp2x1,tmp2y2,moy;
compteur=0;moy=0;
x1=arbre->bloc.x1;
y1=arbre->bloc.y1;
x2=arbre->bloc.x2;
y2=arbre->bloc.y2;
arbre->bloc.moyenne=0;
arbre->bloc.variance=0;
largeur=x2-x1;
hauteur=y2-y1;
// printf("x1 : %d y1 : %d x2 %d y2 %d largeur %d hauteur %d\n",x1,y1,x2,y2,largeur,hauteur);
//initialisation de la moyenne
for (i=y1;i<y2;i++)
{
for(j=x1;j<x2;j++)
{
moy=(float)(moy+(img->pixels[j+i*(img->largeur)]));
// printf("%d [j+i*(img->largeur)] %d cpt: %d i: %d j: %d moy %.2f\n",j+i*(img->largeur),(img->pixels[j+i*(img->largeur)]),compteur,i,j,moy);
compteur++;
}
}
moy=((float)(moy/((largeur)*(hauteur))));
(arbre->bloc.moyenne)=moy;
printf("bloc moy : %f \n",(arbre->bloc.moyenne));
//initialisation de la variance
for (i=y1;i<y2;i++)
{
for(j=x1;j<x2;j++)
{
arbre->bloc.variance=(float)((img->pixels[j+i*(img->largeur)]-arbre->bloc.moyenne)*(img->pixels[j+i*(img->largeur)]-arbre->bloc.moyenne)+arbre->bloc.variance);
}
}
arbre->bloc.variance=(float)sqrt(arbre->bloc.variance/((float)(largeur)*(float)(hauteur)));
printf("variance : %f, diviseur var %d, SEUIL %d \n",arbre->bloc.variance,(largeur*hauteur),SEUIL);
if (arbre->bloc.variance>SEUIL)
{
puts("test 1er");
fread_bits(16,flux);
arbre->premier=(Quadtree*) malloc(sizeof(Quadtree));
arbre->premier->bloc.x1=arbre->bloc.x1;
arbre->premier->bloc.y1=arbre->bloc.y1;
tmp1x2=((float)((arbre->bloc.x1)+(arbre->bloc.x2))/2.0);
tmp=(((arbre->bloc.x1)+(arbre->bloc.x2))/2);
tmp1x2=(tmp1x2-(float)tmp);
// printf("in 1er arbre->premier->bloc.x1=%d arbre->bloc.x2=%d\n",(arbre->premier->bloc.x1),(arbre->bloc.x2));
// printf("in 1er arbre->premier->bloc.y1=%d arbre->bloc.y2=%d\n",(arbre->premier->bloc.y1),(arbre->bloc.y2));
// printf("test ! tmp : %d et tmp1x2 : %.1f\n",tmp,tmp1x2);
tmp1x2=tmp;
// printf("tmp1x2 : %f tmp2x1 : %f \n",tmp1x2,tmp2x1);
arbre->premier->bloc.x2=(int)(tmp1x2);
tmp1y2=((float)(((arbre->bloc.y1)+(arbre->bloc.y2))/2.0));
// printf("1test ! tmp : %d et tmp1y2 : %.1f y1:%d y2:%d\n",tmp,tmp1y2,arbre->bloc.y1,arbre->bloc.y2);
tmp=((((arbre->bloc.y1)+(arbre->bloc.y2))/2.0));
// printf("2test ! tmp : %d et tmp1y2 : %.1f y1:%d y2:%d\n",tmp,tmp1y2,arbre->bloc.y1,arbre->bloc.y2);
tmp1y2=(tmp1y2-(float)tmp);
// printf("3test ! tmp : %d et tmp1y2 : %.1f y1:%d y2:%d\n",tmp,tmp1y2,arbre->bloc.y1,arbre->bloc.y2);
tmp1y2=tmp;
// printf("tmp1y2 : %f tmp2y2 : %f \n",tmp1y2,tmp2y2);
arbre->premier->bloc.y2=(int)(tmp1y2);
coder(img,(arbre->premier),flux);
puts("test 2eme");
arbre->deuxieme=(Quadtree*) malloc(sizeof(Quadtree));
arbre->deuxieme->bloc.x1=(int)(tmp1x2);
arbre->deuxieme->bloc.y1=arbre->bloc.y1;
arbre->deuxieme->bloc.x2=arbre->bloc.x2;
arbre->deuxieme->bloc.y2=(int)(tmp1y2);
coder(img,(arbre->deuxieme),flux);
puts("test 3eme");
arbre->troisieme=(Quadtree*) malloc(sizeof(Quadtree));
arbre->troisieme->bloc.x1=arbre->bloc.x1;
arbre->troisieme->bloc.y1=(int)(tmp1y2);
arbre->troisieme->bloc.x2=(int)(tmp1x2);
arbre->troisieme->bloc.y2=arbre->bloc.y2;
coder(img,(arbre->troisieme),flux);
puts("test 4eme");
arbre->quatrieme=(Quadtree*) malloc(sizeof(Quadtree));
arbre->quatrieme->bloc.x1=(int)(tmp1x2);
arbre->quatrieme->bloc.y1=(int)(tmp1y2);
arbre->quatrieme->bloc.x2=arbre->bloc.x2;
arbre->quatrieme->bloc.y2=arbre->bloc.y2;
coder(img,(arbre->quatrieme),flux);
}
else
{
/* puts("in else");
for(i=y1;i<y2;i++)
{
for(j=x1;j<x2;j++)
{
codage sur 16 bits
pointeur pas null -> 0
1 arrivé a une feuille, et recuperer la moyenne
img->pixels[j+i*(img->largeur)]=arbre->bloc.moyenne;
printf("W j+i*(img->largeur) %d\n",j+i*(img->largeur));
}
}*/
}
return(arbre);
}
void decoder(Quadtree * arbre,FBitBuffer * flux)
{
puts("decoder");
// FREADBITS 16bits
// lire les 16 premiers bits
// cree une structure image avec un tableau de pixel
if (arbre!=NULL)
{
if (arbre->premier=NULL)
{
fwrite_bits(0,1,flux);
fwrite_bits(arbre->bloc.moyenne,8,flux);
}
else
{
fwrite_bits(1, 1, flux);
decoder(arbre->premier,flux);
decoder(arbre->deuxieme,flux);
decoder(arbre->troisieme,flux);
decoder(arbre->quatrieme,flux);
}
}
} |
Partager