Bonjour,
Ca fait plusieur jour que je bloque sur ce probleme j'avance mais mal...
en fait le but et d'arriver a compresser une image par la methode d'un arbre a 4 branche...
mon algo de parcourt est bon, mais je vois pas trop comment utiliser les fonction contenu dans fbit.c donné plus bas, il doit y avoir plusieur chose qui m'echappe, j'arrive a recrée l'image qui est compresser mais je travail sur l'image mais pas comme il faut...

Fbit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
typedef struct {
   unsigned char tampon;
   char * mode;
   int offset;
   FILE * fid;
} FBitBuffer;
 
FBitBuffer * fopen_bits(char * fichier, char * mode);
void fclose_bits(FBitBuffer * buffer);
void fwrite_bits(int val,int nbits,FBitBuffer* buffer);
int fread_bits(int nbits,FBitBuffer* buffer);
 
FBitBuffer * fopen_bits(char * fichier,char * mode) {
  FBitBuffer * buffer;
  buffer = (FBitBuffer*) malloc(sizeof(FBitBuffer));
  buffer->tampon = 0;
  buffer->offset=0;
  buffer->fid = fopen(fichier,mode);
  if (!buffer->fid) {
     free(buffer);
     return NULL;
  }
  buffer->mode = (char*) malloc(strlen(mode)+1);
  strcpy(buffer->mode,mode);
  return buffer;
}
 
void fclose_bits(FBitBuffer * buffer) {
   if (!strcmp(buffer->mode,"w") && buffer->offset) {
      fwrite(&(buffer->tampon),1,1,buffer->fid);
   }
   fclose(buffer->fid);
   free(buffer->mode);
   free(buffer);
}
 
void fwrite_bits(int val,int nbits,FBitBuffer* buffer) {
   int reste;
   if (buffer->offset+nbits<8) {
      buffer->tampon += (val)<<(buffer->offset);
      buffer->offset+=nbits;
   }
   else
   {
      reste = (val)>>(8-buffer->offset);
      buffer->tampon += ((val)&((1<<(8-buffer->offset))-1))<<(buffer->offset);
      fwrite(&(buffer->tampon),1,1,buffer->fid);
      nbits -= (8-buffer->offset);
      buffer->tampon = 0;
      buffer->offset = 0;
      fwrite_bits(reste,nbits,buffer);
   }
}
 
int fread_bits(int nbits,FBitBuffer* buffer) {
   int reste,sup;
   int bitslus;
   int resultat;
   if (nbits==0) {
      return 0;
   }
   if (buffer->offset==0) {
      fread(&(buffer->tampon),1,1,buffer->fid);
   }
   if (buffer->offset+nbits<8) {
      resultat =  (buffer->tampon>>buffer->offset)&((1<<nbits)-1);
      buffer->offset+=nbits;
      return resultat;
   }
   else
   {
      reste = (buffer->tampon>>buffer->offset)&((1<<(8-buffer->offset))-1);
      nbits -= (8-buffer->offset);
      bitslus = (8-buffer->offset);
      buffer->tampon = 0;
      buffer->offset = 0;
      sup = fread_bits(nbits,buffer)<<bitslus;
      return reste+sup;
   }
}
une partie de pgm.c :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
typedef struct {
   int hauteur;
   int largeur;
   unsigned char * pixels;
} Image;
 
int pgm_image_write(Image * image,char * fichier,int bin);
Image * pgm_image_read(char * fichier);
mon programme principal ou j'ai essayer d'utiliser les buffer mais sans resultat :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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);
	}
 }
}
Désolé pour la place que ca prend ^^;
Merci d'avance pour votre aide