2 // implements canonical Huffman
7 //THuff createHuff (uint *fst, uint *num, uint depth) {
16 void printCodeHuffDec (THuffDec H, uint symb)
21 //pos = H.s.spos[symb];
26 // fprintf(stderr,"\n H.depth= %ld and pos is %ld\n",H.depth,pos);
27 while (pos >= H.num[d])
28 { code = (code + H.num[d]) >> 1;
32 if (d > W) {fprintf(stderr,"code larger than W"); d=W;}
36 { if ((code >> d) & 1)
38 else fprintf(stderr,"0");
44 //Decodes a code starting in position ptr from stream. Returns the ranking in the
46 int decodeHuffDec (THuffDec *H, uint *symb, uint *stream, uint ptr)
51 while (pos < H->fst[d])
52 { pos = (pos << 1) | bitget(stream,ptr);
55 *symb = H->num[d]+pos-H->fst[d];
61 // //Decodes a code starting in position ptr from stream. Returns the ranking in the
62 // //vector of symbols.
63 // int decodeHuffDecVariantWord (uint *fst , uint *symb, uint *stream, uint ptr, uint depth)
68 // while ((d< depth) && (pos < fst[d*2]) )
69 // { pos = (pos << 1) | bitget(stream,ptr);
79 // //(*symb) = (depth==d) ? pos : fst[d*2+1] + pos - fst[d*2];
86 // the bytes used by HuffDecman struct
87 uint sizeHuffDec (THuffDec H)
88 { return (1+ 2*(H.depth+1))*sizeof(uint);
92 void freeHuffDec (THuffDec H)
94 { free (H.fst); free (H.num);
98 THuffDec loadHuffDecAfterDecode (FILE *f, int enc) //enc (0/1)-> do you only want to perform encoding ??
101 // int i,d,dold,dact;
103 // fread (&H.max,sizeof(uint),1,f);
104 // fread (&H.lim,sizeof(uint),1,f);
105 // fread (&H.depth,sizeof(uint),1,f);
107 // H.s.symb = malloc ((H.lim+1)*sizeof(uint));
108 // fread (H.s.symb,sizeof(uint),H.lim+1,f);
110 // H.fst = malloc ((H.depth+1)*sizeof(uint));
111 // fread (H.fst,sizeof(uint),H.depth+1,f);
113 // H.num = malloc ((H.depth+1)*sizeof(uint));
114 // fread (H.num,sizeof(uint),H.depth+1,f);
120 ////THuffDec loadHuffDec (FILE *f, int enc)
125 //// int i,d,dold,dact;
126 //// fread (&H.max,sizeof(uint),1,f);
127 //// fread (&H.lim,sizeof(uint),1,f);
128 //// fread (&H.depth,sizeof(uint),1,f);
129 //// symb = malloc ((H.lim+1)*sizeof(uint));
130 //// fread (symb,sizeof(uint),H.lim+1,f);
132 //// { H.s.spos = malloc ((H.max+1)*sizeof(uint));
133 //// for (i=0;i<=H.max;i++) H.s.spos[i] = ~0;
134 //// for (i=0;i<=H.lim;i++) H.s.spos[symb[i]] = i;
137 //// else H.s.symb = symb;
138 //// H.num = malloc ((H.depth+1)*sizeof(uint));
139 //// fread (H.num,sizeof(uint),H.depth+1,f);
141 //// { H.fst = malloc ((H.depth+1)*sizeof(uint));
142 //// H.fst[H.depth] = 0; dold = 0;
143 //// for (d=H.depth-1;d>=0;d--)
144 //// { dact = H.num[d+1];
145 //// H.fst[d] = (H.fst[d+1]+dact) >> 1;
146 //// H.num[d+1] = dold;
149 //// H.num[0] = dold;
155 void loadHuffDecAfterDecode2 (THuffDec *H, FILE *f, int enc) //enc (0/1)-> do you only want to perform encoding ??
160 // fread (&H->max,sizeof(uint),1,f);
161 // fread (&H->lim,sizeof(uint),1,f);
162 fread (&H->depth,sizeof(uint),1,f);
164 // H->s.symb = malloc ((H->lim+1)*sizeof(uint));
165 // fread (H->s.symb,sizeof(uint),H->lim+1,f);
167 H->fst = (uint *) malloc ((H->depth+1)*sizeof(uint));
168 fread (H->fst,sizeof(uint),H->depth+1,f);
170 H->num = (uint *) malloc ((H->depth+1)*sizeof(uint));
171 fread (H->num,sizeof(uint),H->depth+1,f);