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
| # include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <stddef.h>
# include <math.h>
# define N 256
# define M 8
/*Les maillons "sommes" ont pour caractere le code ASCII 0 : caractere NULL*/
typedef struct noeud *ARBRE;
struct noeud
{
unsigned char e;
int frequence;
ARBRE fg, fd;
};
typedef struct
{
unsigned char taille_code;
unsigned char code[M];
}CODE;
CODE *c;
/*Fonction recursive calculant le chemin binaire d'un caractere, c (pointeur sur une struct CODE) variable globale*/
void chemin ( unsigned char e, ARBRE t, unsigned char prefixe[M], unsigned char taille_pref ){
int i;
CODE* tmp;
tmp = malloc(sizeof(CODE));
if ( (t->e == e) && (t->fg == NULL) && (t->fd == NULL) ){
tmp->taille_code = taille_pref;
for (i=0;i<taille_pref;i++)
tmp->code[i] = prefixe[i];
for (i=taille_pref;i<M;i++)/*initialise les cases non utilisées du tableau code à 0*/
tmp->code[i] = 0;
c = tmp;
}
else {
if ( t != NULL ){
if ( t->fg != NULL ) {
prefixe[taille_pref] = 0;
chemin ( e, t->fg, prefixe, taille_pref+1 );
}
if ( t->fd != NULL ) {
prefixe[taille_pref] = 1;
chemin ( e, t->fd, prefixe, taille_pref+1 );
}
}
}
}
main(){
unsigned char tampon[N], tampon2[N];
int tab_freq[N];
unsigned char prefixe[M],taille_pref;
int i, j, taille_file;
ARBRE file [N];
ARBRE t;
CODE *tab_codes[N];
unsigned char tab_codes_comp[2*N];
FILE *dest, *source;
/*Ouverture fichier source*/
printf("Emplacement du fichier à compresser: ");
scanf("%s",tampon);
source = fopen ( tampon, "rb" );
/*Ouverture fichier destination*/
printf("Nom fichier compressé : ");
scanf("%s",tampon2);
dest = fopen(tampon2,"wb");
frequence ( tampon, tab_freq );
fwrite ( tab_freq, 1024, 1, dest);
taille_file = construire_file(file,tab_freq);
t = huffman(file,taille_file);
/*Boucle qui remplit le tableau tab_codes avec : Caractere/taille_code/code*/
for(i=0;i<N;i++){
taille_pref = 0;
c = NULL;
chemin ( i, t, prefixe, taille_pref );/*le 1er appel de chemin (i=0) modifie tab_freq[0] !!! */
printf("%d\n",tab_freq[0]);
tab_codes[i] = c;
}
compression ( source, tab_codes );
bin_int ( fopen ( "~tmpcomp", "rb" ), dest );
fclose(dest);
fclose(source);
} |
Partager