4 typedef unsigned long int t_freq;
6 typedef struct t_freqnode {
7 unsigned short int symbol;
9 struct t_freqnode *lchild;
10 struct t_freqnode *rchild;
13 typedef struct t_code {
14 unsigned long int code;
15 unsigned char codelength;
18 void putbit(char bit, char restart, char flush, FILE *fp)
20 static unsigned long int bits_buffer = 0;
21 static unsigned char bits_used = 0;
23 /* me obligan a tirar el output */
24 if ((flush == 1) && (bits_used > 0)) {
25 fwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
30 /* me indican que comienza un nuevo output */
35 /* inserto el bit en el buffer */
36 bits_buffer = bits_buffer << 1;
40 /* lleno el buffer, escribo */
41 if (bits_used == 32) {
42 fwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
49 void cpynode(HUFFNODE *node1, HUFFNODE *node2)
51 node1->symbol = node2->symbol;
52 node1->freq = node2->freq;
53 node1->lchild = node2->lchild;
54 node1->rchild = node2->rchild;
57 int compnode(HUFFNODE *node1, HUFFNODE *node2)
59 if (node1->freq < node2->freq) return 1;
60 if (node1->freq > node2->freq) return -1;
64 HUFFNODE *buildlist(t_freq *freqtable, int *nonzerofreqs)
66 int i,j = 0,nonzero = 0;
69 /* Calculo cuantas frequencias > 0 hay y creo la tabla */
70 for (i = 0; i < 256; ++i) if (freqtable[i] > 0) nonzero++;
71 inputlist = (HUFFNODE*)malloc(sizeof(HUFFNODE)*nonzero);
73 /* Cargo la inputlist del huffman solo con freqs > 0 */
74 for (i = 0; i < 256; ++i)
75 if (freqtable[i] > 0) {
76 inputlist[j].symbol = i;
77 inputlist[j].freq = freqtable[i];
78 inputlist[j].lchild = NULL;
79 inputlist[j].rchild = NULL;
83 *nonzerofreqs = nonzero;
87 int rescalefreq(t_freq *freqtable)
92 /* Divido por la mitad las frecuencias, asegurando de no perder */
93 /* frequencias en 1, por ello le sumo 1 antes de partir */
94 for (i = 0; i < 256; i++) {
95 freqtable[i] = (freqtable[i] << 2) | 1;
96 totalfreq += freqtable[i];
102 int scanfreq(char *inputfile, t_freq *freqtable)
109 /* Inicializamos la tabla de frecuencias */
110 for (i = 0; i < 256; ++i) freqtable[i] = 0;
112 /* Abrimos el file */
113 if ((fp = fopen(inputfile,"rb")) == NULL) return 0;
115 /* Contamos las frecuencias */
117 if (symbol == EOF) continue;
122 /* Si llegue al tope de freq acumulada, halve em */
123 if (sumfreq == 14930352)
124 sumfreq = rescalefreq(freqtable);
131 void printcodes(CODE *codetable,t_freq *freqtable)
134 unsigned short int auxcode;
137 for (i = 0; i < 256; ++i) {
138 if (codetable[i].codelength > 0) {
139 auxcode = codetable[i].code;
140 printf("Symbol:%i Freq: %li Code:",i,freqtable[i]);
141 for (j = codetable[i].codelength-1; j >= 0; --j) {
142 auxcode = codetable[i].code;
143 auxcode = auxcode >> j;
147 printf(" Length:%i\n",codetable[i].codelength);
152 void zerocodes(CODE *table)
156 /* Inicializo los codigos prefijos */
157 for (i = 0; i < 256; ++i) {
159 table[i].codelength = 0;
163 void buildcodes(CODE *table, HUFFNODE *node, int level, int code)
165 if (node->symbol < 256) {
166 /* Guardo el codigo en la tabla */
167 table[node->symbol].code = code;
168 table[node->symbol].codelength = level;
172 buildcodes(table,node->lchild,level+1,code);
174 buildcodes(table,node->rchild,level+1,code);
178 HUFFNODE *buildtree(HUFFNODE *list, int listcount)
180 HUFFNODE *lastsymbol = list+(listcount-1);
181 HUFFNODE *node1,*node2,*fictnode;
183 /* Ordenamos inicialmente la inputlist para tomar las dos freqs min */
184 while (lastsymbol > list) {
185 /* Ordeno la lista por frecuencia descendente */
186 qsort(list,listcount,sizeof(HUFFNODE),compnode);
187 /* Tomo los ultimos dos elementos, generando dos nodos del arbol */
188 node1 = (HUFFNODE*)malloc(sizeof(HUFFNODE));
189 node2 = (HUFFNODE*)malloc(sizeof(HUFFNODE));
190 cpynode(node1,lastsymbol-1);
191 cpynode(node2,lastsymbol);
193 /* Nodo ficticio con la suma de las probs y los ptros a childs */
194 lastsymbol->symbol = 256;
195 lastsymbol->freq = node1->freq + node2->freq;
196 lastsymbol->lchild = node1;
197 lastsymbol->rchild = node2;
201 /* Devuelvo el puntero a la raiz del arbol de huffman */
205 int encode(CODE *table, char* inputfile, char *outputfile) {
207 FILE *fpsource,*fpdest;
212 /* Abrimos el file */
213 if ((fpsource = fopen(inputfile,"rb")) == NULL) return 0;
214 if ((fpdest = fopen(outputfile,"wb")) == NULL) return 0;
216 while (!feof(fpsource)) {
217 /* Levanto un symbolo (byte) */
218 symbol = fgetc(fpsource);
219 if (symbol == EOF) continue;
221 /* Cargamos el codigo y lo emitimos */
222 symbolcode = table[symbol];
223 for (i = symbolcode.codelength; i > 0; --i) {
224 bit = (symbolcode.code >> (i-1)) & 1;
225 putbit(bit,0,0,fpdest);
229 /* Hacemos un flush de lo que haya quedado en el buffer de salida */
230 putbit(0,0,1,fpdest);
236 int main(int argc, char* argv[])
239 t_freq *freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
242 CODE *codetable = (CODE*)malloc(sizeof(CODE)*256);
245 if (argc == 1) return -1;
247 /* Armamos la tabla de frecuencias */
248 if (!scanfreq(argv[1],freqtable)) return -1;
250 /* Armo el input list y genero el arbol de huffman */
251 inputlist = buildlist(freqtable, &freqcount);
252 codetree = buildtree(inputlist,freqcount);
253 /* Armo la tabla de codigos prefijos para el encoder */
254 zerocodes(codetable);
255 buildcodes(codetable,codetree,0,0);
256 printcodes(codetable,freqtable);
257 encode(codetable,argv[1],"output.dat");