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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
unsigned char code[100];
struct NOEUD
{
int poids;
char c;
struct NOEUD *fg,*fd;
};
struct NOEUD *creernoeud(int poids,char c,struct NOEUD *fg,struct NOEUD *fd)
{
struct NOEUD *noeud;
noeud=(struct NOEUD*)malloc(sizeof(struct NOEUD));
noeud->poids=poids;
noeud->c=c;
noeud->fg=fg;
noeud->fd=fd;
return noeud;
}
struct NOEUD *fusionner(struct NOEUD *fg,struct NOEUD *fd)
{
return creernoeud(fg->poids+fd->poids,0,fg,fd);
}
unsigned char conv(char *txt,int ibit)
{
unsigned char c=0; // l'octet qui sera renvoyé
unsigned char bit=0x1; // bit de poids le plus faible ; par la suite, 'bit' contiendra le bit de poids courant
int i;
for( i=ibit+7 ; i>=ibit ; --i )
{
if( txt[i]=='1') // on suppose que txt ne contient que des '0' ou des '1'
c |= bit; // on ajoute le bit courant
bit = bit<<1; // on décale d'un cran
//affichage du calcul:
//printf("txt[i]=%c i=%d bit=%d c=%d\n",txt[i],i,bit,c);
//system("pause");
//
}
//printf("\nc defin=%d",c);
return c;
}
void analyser(char fichier[],int Occ[])
{
FILE *f;
int i,c;
for(i=0;i<256;i++) Occ[i]=0;
f=fopen(fichier,"rb");
while(1)
{
c=fgetc(f);
if (c==EOF) break;
Occ[c]++;
//if(c==128) printf("c=%d\tocc=%d",c,Occ[c]);
}
fclose(f);
}
struct NOEUD *extrairemin(struct NOEUD *T[],int n)
{
int k,i;
struct NOEUD *tmp;
k=-1;
for(i=0;i<n;i++)
if (T[i]!=NULL)
{
if (k==-1) k=i;
if (T[i]->poids<T[k]->poids) k=i;
}
if (k==-1) return NULL;
tmp=T[k];
T[k]=T[n-1];
return tmp;
}
// fabrication du code binaire
struct NOEUD *codage(int Occ[])
{
int n,i,j;
struct NOEUD *tmp1,*tmp2,*T[256];
j=0;
for(i=0;i<256;i++)
if (Occ[i]!=0)
{
T[j]=creernoeud(Occ[i],i,NULL,NULL);
j++;
}
n=j;
for(i=0;i<n-1;i++)
{
tmp1=extrairemin(T,n-i);
tmp2=extrairemin(T,n-i-1);
T[n-i-2]=fusionner(tmp1,tmp2);
}
return T[0];
}
void afficher(struct NOEUD *arbre,int n)
{
int i;
for(i=0;i<n;i++) printf("-");
if (arbre->fg==NULL && arbre->fd==NULL)
{
code[n]='\0';
printf("> \'%d\' (poids=%d) (code=%s)\n",arbre->c,arbre->poids,code);
} else
{
printf("> pere (poids=%d)\n",arbre->poids);
code[n]='0';
afficher(arbre->fg,n+1);
code[n]='1';
afficher(arbre->fd,n+1);
}
}
int creerdico(struct NOEUD *arbre,char *dico[256],int n)
{
if (arbre->fg==NULL && arbre->fd==NULL)
{
code[n]='\0';
dico[arbre->c]=malloc(3000);
strcpy(dico[arbre->c],code);
printf("c=%d code=%s\n",arbre->c,dico[arbre->c]);
} else
{
code[n]='0';
creerdico(arbre->fg,dico,n+1);
code[n]='1';
creerdico(arbre->fd,dico,n+1);
}
}
void creerfilebin(char *dico[256],char filein[],char fileout[],int Occ[],int taille)
{
FILE *fin,*fout;
int i,j,c_out,aux;
char t[50];
j=0;
char c;
fin=fopen(filein,"rb");
fout=fopen(fileout,"wb");
while(1)
{
c=fgetc(fin);
if (c==EOF && taille!=0) break;
if (c==EOF && taille>0)
{
for(i=8;i>8-taille;i--) t[i]=0;
j=8;
taille=0;
}
if(j<8)
{
for(i=0;i<strlen(dico[c]);i++)
{
t[j]=dico[c][i];
//code inseré dans la tableau:
//printf("%c",dico[c][i]);
j++;
}
}
else
{
c_out=conv(t,0);
printf("%d",c_out);
//system("pause");
fprintf(fout,"%c",c_out);
for(i=0;i<j-8;i++) t[i]=t[i]+8;
j=j-8;
}
}
fclose(fin);
fclose(fout);
}
int taillef(const char *file)
{
int taille;
FILE *f = fopen(file,"rb");
if(!f) return(-1);
fseek(f, 0, 2);
taille = (int)ftell(f);
fclose(f);
return(taille);
}
int main()
{
char filein[200],fileout[200],*dico[256];
unsigned int Occ[256],t,taille_out_bin,taille_out_bin_add,i;
struct NOEUD *arbre;
taille_out_bin=0;
printf("Fichier a compresser :");
scanf("%s",filein);
printf("Compresser dans :");
scanf("%s",fileout);
t=time(0);
analyser(filein,Occ);
arbre=codage(Occ);
for(i=0;i<256;i++)
{
if(Occ[i]!=0)
{
printf("%d\t%d\t\n",i,Occ[i]);
}
}
system("pause");
creerdico(arbre,dico,0);
printf("%c\t%d\tn",128,Occ[128]);
for(i=0;i<256;i++)
{
if(Occ[i]!=0)
{
printf("%d\t%d\t\n",i,Occ[i]);
}
}
system("pause");
system("cls");
afficher(arbre,0);
printf("carac\toccur\tcode bin\n");
//affichage des caractere + occurrences et
//calcule la taille du fichier de sortie sans arrondie a l'octet suivant(en bit)
for(i=0;i<256;i++)
{
if(Occ[i]!=0)
{
printf("%d\t%d\t%s\n",i,Occ[i],dico[i]);
taille_out_bin+=Occ[i]*strlen(dico[i]);
}
}
//bit a ajouter aux fichier pour faire un arrondi a l'octet
taille_out_bin_add=8-(taille_out_bin%8);
//cree le fichier binaire compressé
creerfilebin(dico,filein,fileout,Occ,taille_out_bin_add);
printf("\n Duree : %ds\n",time(0)-t);
return 0;
} |
Partager