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
| void compressionLZW(char * nomfichier)
{
FILE * Comp;
FILE * Curs;
char * seq=malloc(sizeof(char));
char * attente=malloc(sizeof(char));
char lu;
cell* Dico[100]={NULL};
int code=259;
//code==256=>fin de compression
//code==257=>raz
//code=258=>nbbit++
int code_attente;
int c,taille;
char * nomfichierlzw;
int nbbit;
nomfichierlzw=(char *)malloc((strlen(nomfichier)+3)*sizeof(char));
strcpy(nomfichierlzw,nomfichier);
strcat(nomfichierlzw,"lzw");
Comp=fopen(nomfichierlzw,"w");
Curs=fopen(nomfichier,"r");
attente[0]='\0';
seq[0]='\0';
while (fread(&lu,sizeof(char),1,Curs)==1)
{
if (code == 65536) {
ecrire_x_bit(257,nbbit,Comp);
free(Dico);
code=259;
}
free(seq);
seq=malloc((strlen(attente)+2)*sizeof(char));
strcpy(seq,attente);;
free(attente);
strncat(seq,&lu,1);
c=apprise(seq,Dico);
nbbit=calculer_nbbit(code);
if (c!=-1)
//si on connais la sequence, on la met en attente.
attente=malloc((strlen(seq)+1)*sizeof(char));
strcpy(attente,seq);
code_attente=c;
}
else
{
//sinon on l'apprend.
apprend_seq(seq,code,Hach(seq),Dico);
ecrire_x_bit(code_attente,nbbit,Comp);
code++;
if (nbbit != calculer_nbbit(code))
{ecrire_x_bit(258,nbbit,Comp);
nbbit=calculer_nbbit(code);
}
attente=malloc(sizeof(char)+1);
strcpy(attente,&lu);
attente[1]='\0';
code_attente=(unsigned char)lu;
}
}
//a la fin, il reste encore le dernier caractere lu en attente
ecrire_x_bit(code_attente,nbbit,Comp);
ecrire_x_bit(256,nbbit,Comp);
fclose(Comp);
fclose(Curs);
} |
Partager