2 // implements canonical Huffman
16 static void sort (Ttree *tree, int lo, int up)
25 { while (tree[j].freq > temp.freq) j--;
27 while (i<j && tree[i].freq <= temp.freq) i++;
31 if (i-lo < up-i) { sort(tree,lo,i-1); lo = i+1; }
32 else { sort(tree,i+1,up); up = i-1; }
36 static void setdepths (Ttree *tree, uint node, int depth)
38 { if (tree[node].ch1 == -1) // leaf
39 { tree[node].h.depth = depth;
42 setdepths (tree,tree[node].ch1,depth+1);
43 setdepths (tree,tree[node].ch2,depth+1);
47 THuff createHuff (uint *freq, uint lim, uint sorted)
53 // remove zero frequencies
57 int onlyOneSymbol = (lim ==0);
59 // tree = malloc((2*(lim+1)-1)*sizeof(Ttree));
60 /*** BEG FARI... para solucionar caso en que s�lo hubiese un �nico s�mbolo a codificar ****/
62 tree = (Ttree *)malloc(3*sizeof(Ttree)); //root, the valid node (pos 0) and the "zeroNode" (pos 1)
66 tree = (Ttree *)malloc((2*(lim+1)-1)*sizeof(Ttree));
67 /*** END FARI... para solucionar caso en que s�lo hubiese un �nico s�mbolo a codificar ****/
69 //fprintf(stderr,"\n **** CALLED CREATE_HUFF WITH lim = %ld, freq[0]=%ld",lim,freq[0]);
73 { tree[j].freq = freq[i];
76 //fprintf(stderr,"\n freq[%d] = [%d], para s�mbolo %d, now j=%ld",i,freq[i],tree[j-1].symb,j);
80 /*** BEG FARI... para solucionar caso en que s�lo hubiese un �nico s�mbolo a codificar ****/
83 tree[j++].symb = 1; //<--- !!!!!!!!! (incrementa j !!!)
85 //H.total -=1 * 1; //we will add 1*1 at the end of this function.
88 //fprintf(stderr,"\n");
90 /*** END FARI... para solucionar caso en que s�lo hubiese un �nico s�mbolo a codificar ****/
95 // now run Huffman algorithm
96 if (!sorted) sort (tree,0,lim);
98 for (i=0;i<=lim;i++) {
99 //fprintf(stderr,"\n XX freq[%d] = [%d], para symbolo %d",i,tree[i].freq,tree[i].symb);
100 tree[i].h.prev = i+1;
101 tree[i].ch1 = tree[i].ch2 = -1;
103 tree[lim].h.prev = -1;
104 // last = next node to process, ptr = search point, fre = next free cell
105 // leaves are in 0..lim in decreasing freq order
106 // internal nodes are in lim+1.. 2*lim, created in incr. fre order
107 last=0; ptr = 0; fre = lim+1;
109 { tree[fre].ch1 = last;
110 last = tree[last].h.prev;
111 tree[fre].ch2 = last;
112 tree[fre].freq = tree[tree[fre].ch1].freq+tree[tree[fre].ch2].freq;
113 while ((tree[ptr].h.prev != -1) &&
114 (tree[tree[ptr].h.prev].freq <= tree[fre].freq))
115 ptr = tree[ptr].h.prev;
116 tree[fre].h.prev = tree[ptr].h.prev;
117 tree[ptr].h.prev = fre;
118 last = tree[last].h.prev;
121 // now assign depths recursively
122 setdepths (tree,2*lim,0);
123 H.s.spos = (uint *)malloc ((H.max+1)*sizeof(uint));
124 for (i=0;i<=H.max;i++) H.s.spos[i] = ~0;
125 H.num = (uint *)malloc ((lim+1)*sizeof(uint)); // max possible depth
128 { H.s.spos[tree[i].symb] = i;
129 while (tree[i].h.depth > d)
130 { H.num[d] = i+1; d++; }
134 for (d=H.depth;d>0;d--) H.num[d] = H.num[d-1] - H.num[d];
135 H.num[0] = (lim == 0);
136 H.num = (uint *) realloc(H.num,(H.depth+1)*sizeof(uint));
142 if (onlyOneSymbol){ H.total += freq[tree[0].symb] * tree[0].h.depth;}
144 for (i=0;i<=lim;i++) {
145 H.total += freq[tree[i].symb] * tree[i].h.depth;
146 //fprintf(stderr,"\n ****tota[%d] = %d x %d =%d",i,freq[tree[i].symb],tree[i].h.depth,H.total);
151 //fprintf(stderr,"\n **** CALL ENDS CREATE_HUFF WITH lim = %ld, freq[0]=%ld, Huffsize=%ld",lim,freq[0],H.total);
156 int encodeHuff (THuff H, uint symb, uint *stream, uint ptr)
161 pos = H.s.spos[symb];
164 while (pos >= H.num[d])
165 { code = (code + H.num[d]) >> 1;
169 if (d > W) { bitzero2(stream,ptr,d-W); ptr += d-W; d = W; }
171 { if ((code >> d) & 1) bitset(stream,ptr);
172 else bitclean(stream,ptr);
178 void printCodeHuff (THuff H, uint symb)
183 pos = H.s.spos[symb];
187 // fprintf(stderr,"\n H.depth= %ld and pos is %ld\n",H.depth,pos);
188 while (pos >= H.num[d])
189 { code = (code + H.num[d]) >> 1;
193 if (d > W) {fprintf(stderr,"code larger than W"); d=W;}
197 { if ((code >> d) & 1)
199 else fprintf(stderr,"0");
205 int decodeHuff (THuff *H, uint *symb, uint *stream, uint ptr)
211 while (pos < H->fst[d])
212 { pos = (pos << 1) | bitget(stream,ptr);
215 *symb = H->s.symb[H->num[d]+pos-H->fst[d]];
222 /* { uint pos; // This "improved" code is actually slower!
227 wrd = *stream >> off;
230 while (pos < H.fst[d])
231 { pos = (pos << 1) | (wrd & 1);
232 d++; wrd >>= 1; off++;
233 if (off == W) { wrd = *++stream; off = 0; }
235 *symb = H.s.symb[H.num[d]+pos-H.fst[d]];
239 void saveHuff2 (THuff H, FILE *f)
241 { uint *symb = (uint *)malloc((H.lim+1)*sizeof(uint));
243 for (i=0;i<=H.max;i++)
244 if (H.s.spos[i] != ~0) symb[H.s.spos[i]] = i;
245 fwrite (&H.max,sizeof(uint),1,f);
246 fwrite (&H.lim,sizeof(uint),1,f);
247 fwrite (&H.depth,sizeof(uint),1,f);
248 fwrite (symb,sizeof(uint),H.lim+1,f);
249 fwrite (H.num,sizeof(uint),H.depth+1,f);
253 uint sizeHuff2 (THuff H)
255 { return (4 +(H.lim+1)+2*(H.depth+1))*sizeof(uint);
258 uint sizeHuffDisk (THuff H)
260 { return ( sizeof(THuff) + ((H.lim+1)+(H.depth+1))*sizeof(uint) );
263 void freeHuff2 (THuff H)
265 { free (H.s.spos); free (H.num); free (H.fst);
269 THuff loadHuff2 (FILE *f, int enc) //enc (0/1)-> do you only want to perform encoding ??
275 fread (&H.max,sizeof(uint),1,f);
276 fread (&H.lim,sizeof(uint),1,f);
277 fread (&H.depth,sizeof(uint),1,f);
278 symb = (uint *) malloc ((H.lim+1)*sizeof(uint));
279 fread (symb,sizeof(uint),H.lim+1,f);
281 { H.s.spos = (uint *) malloc ((H.max+1)*sizeof(uint));
282 for (i=0;i<=H.max;i++) H.s.spos[i] = ~0;
283 for (i=0;i<=H.lim;i++) H.s.spos[symb[i]] = i;
286 else H.s.symb = symb;
288 H.num = (uint *) malloc ((H.depth+1)*sizeof(uint));
289 fread (H.num,sizeof(uint),H.depth+1,f);
291 { H.fst = (uint *) malloc ((H.depth+1)*sizeof(uint));
292 H.fst[H.depth] = 0; dold = 0;
293 for (d=H.depth-1;d>=0;d--)
295 H.fst[d] = (H.fst[d+1]+dact) >> 1;
308 /***************************************************************/
309 void prepareToDecode(THuff *H)
310 //***///**// //by fari !!
312 { uint *symb = (uint *) malloc((H->lim+1)*sizeof(uint));
316 for (i=0;i<=H->max;i++)
317 if (H->s.spos[i] != ~0)
318 symb[H->s.spos[i]] = i;
320 for (i=0;i<=H->lim;i++)
321 H->s.symb[i] = symb[i];
323 //H.num = malloc ((H.depth+1)*sizeof(uint));
325 H->fst = (uint *)malloc ((H->depth+1)*sizeof(uint));
326 H->fst[H->depth] = 0; dold = 0;
327 for (d=H->depth-1;d>=0;d--)
328 { dact = H->num[d+1];
329 H->fst[d] = (H->fst[d+1]+dact) >> 1;
340 void saveHuffAfterDecode (THuff H, FILE *f)
342 fwrite (&H.max,sizeof(uint),1,f);
343 fwrite (&H.lim,sizeof(uint),1,f);
344 fwrite (&H.depth,sizeof(uint),1,f);
345 fwrite (H.s.symb,sizeof(uint),H.lim+1,f);
347 fwrite (H.fst,sizeof(uint),H.depth+1,f);
348 fwrite (H.num,sizeof(uint),H.depth+1,f);
352 THuff loadHuffAfterDecode (FILE *f, int enc) //enc (0/1)-> do you only want to perform encoding ??
355 // int i,d,dold,dact;
357 // fread (&H.max,sizeof(uint),1,f);
358 // fread (&H.lim,sizeof(uint),1,f);
359 // fread (&H.depth,sizeof(uint),1,f);
361 // H.s.symb = malloc ((H.lim+1)*sizeof(uint));
362 // fread (H.s.symb,sizeof(uint),H.lim+1,f);
364 // H.fst = malloc ((H.depth+1)*sizeof(uint));
365 // fread (H.fst,sizeof(uint),H.depth+1,f);
367 // H.num = malloc ((H.depth+1)*sizeof(uint));
368 // fread (H.num,sizeof(uint),H.depth+1,f);
375 void loadHuffAfterDecode2 (THuff *H, FILE *f, int enc) //enc (0/1)-> do you only want to perform encoding ??
380 fread (&H->max,sizeof(uint),1,f);
381 fread (&H->lim,sizeof(uint),1,f);
382 fread (&H->depth,sizeof(uint),1,f);
384 H->s.symb = (uint *) malloc ((H->lim+1)*sizeof(uint));
385 fread (H->s.symb,sizeof(uint),H->lim+1,f);
387 H->fst = (uint *) malloc ((H->depth+1)*sizeof(uint));
388 fread (H->fst,sizeof(uint),H->depth+1,f);
390 H->num = (uint *) malloc ((H->depth+1)*sizeof(uint));
391 fread (H->num,sizeof(uint),H->depth+1,f);