// =========================================================================== // Compute the optimal bit lengths for a tree and update the total bit length // for the current block. // IN assertion: the fields freq and dad are set, heap[heap_max] and // above are the tree nodes sorted by increasing frequency. // OUT assertions: the field len is set to the optimal bit length, the // array bl_count contains the frequencies for each bit length. // The length opt_len is updated; static_len is also updated if stree is // not null. // desc: the tree descriptor static void gen_bitlen(deflate_state s, ref tree_desc desc) { ct_data[] tree=desc.dyn_tree; int max_code=desc.max_code; ct_data[] stree=desc.stat_desc.static_tree; int[] extra=desc.stat_desc.extra_bits; int @base=desc.stat_desc.extra_base; int max_length=desc.stat_desc.max_length; int h; // heap index int n, m; // iterate over the tree elements int bits; // bit length int xbits; // extra bits ushort f; // frequency int overflow=0; // number of elements with bit length too large for(bits=0; bits<=MAX_BITS; bits++) s.bl_count[bits]=0; // In a first pass, compute the optimal bit lengths (which may // overflow in the case of the bit length tree). tree[s.heap[s.heap_max]].Len=0; // root of the heap for(h=s.heap_max+1; h<HEAP_SIZE; h++) { n=s.heap[h]; bits=tree[tree[n].Dad].Len+1; if(bits>max_length) { bits=max_length; overflow++; } tree[n].Len=(ushort)bits; // We overwrite tree[n].Dad which is no longer needed if(n>max_code) continue; // not a leaf node s.bl_count[bits]++; xbits=0; if(n>=@base) xbits=extra[n-@base]; f=tree[n].Freq; s.opt_len+=(uint)(f*(bits+xbits)); if(stree!=null) s.static_len+=(uint)(f*(stree[n].Len+xbits)); } if(overflow==0) return; //Trace((stderr,"\nbit length overflow\n")); // This happens for example on obj2 and pic of the Calgary corpus // Find the first bit length which could increase: do { bits=max_length-1; while(s.bl_count[bits]==0) bits--; s.bl_count[bits]--; // move one leaf down the tree s.bl_count[bits+1]+=2; // move one overflow item as its brother s.bl_count[max_length]--; // The brother of the overflow item also moves one step up, // but this does not affect bl_count[max_length] overflow-=2; } while(overflow>0); // Now recompute all bit lengths, scanning in increasing frequency. // h is still equal to HEAP_SIZE. (It is simpler to reconstruct all // lengths instead of fixing only the wrong ones. This idea is taken // from 'ar' written by Haruhiko Okumura.) for(bits=max_length; bits!=0; bits--) { n=s.bl_count[bits]; while(n!=0) { m=s.heap[--h]; if(m>max_code) continue; if((uint)tree[m].Len!=(uint)bits) { //Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); s.opt_len+=((uint)bits-tree[m].Len)*tree[m].Freq; tree[m].Len=(ushort)bits; } n--; } } }
// =========================================================================== // Construct one Huffman tree and assigns the code bit strings and lengths. // Update the total bit length for the current block. // IN assertion: the field freq is set for all tree elements. // OUT assertions: the fields len and code are set to the optimal bit length // and corresponding code. The length opt_len is updated; static_len is // also updated if stree is not null. The field max_code is set. // desc: the tree descriptor static void build_tree(deflate_state s, ref tree_desc desc) { ct_data[] tree=desc.dyn_tree; ct_data[] stree=desc.stat_desc.static_tree; int elems=desc.stat_desc.elems; int n, m; // iterate over heap elements int max_code=-1; // largest code with non zero frequency int node; // new node being created // Construct the initial heap, with least frequent element in // heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. // heap[0] is not used. s.heap_len=0; s.heap_max=HEAP_SIZE; for(n=0; n<elems; n++) { if(tree[n].Freq!=0) { s.heap[++(s.heap_len)]=max_code=n; s.depth[n]=0; } else tree[n].Len=0; } // The pkzip format requires that at least one distance code exists, // and that at least one bit should be sent even if there is only one // possible code. So to avoid special checks later on we force at least // two codes of non zero frequency. while(s.heap_len<2) { node=s.heap[++(s.heap_len)]=(max_code<2?++max_code:0); tree[node].Freq=1; s.depth[node]=0; s.opt_len--; if(stree!=null) s.static_len-=stree[node].Len; // node is 0 or 1 so it does not have extra bits } desc.max_code=max_code; // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, // establish sub-heaps of increasing lengths: for(n=s.heap_len/2; n>=1; n--) pqdownheap(s, tree, n); // Construct the Huffman tree by repeatedly combining the least two // frequent nodes. node=elems; // next internal node of the tree do { //was pqremove(s, tree, n); // n = node of least frequency n=s.heap[SMALLEST]; s.heap[SMALLEST]=s.heap[s.heap_len--]; pqdownheap(s, tree, SMALLEST); m=s.heap[SMALLEST]; // m = node of next least frequency s.heap[--(s.heap_max)]=n; // keep the nodes sorted by frequency s.heap[--(s.heap_max)]=m; // Create a new node father of n and m tree[node].Freq=(ushort)(tree[n].Freq+tree[m].Freq); s.depth[node]=(byte)((s.depth[n]>=s.depth[m]?s.depth[n]:s.depth[m])+1); tree[n].Dad=tree[m].Dad=(ushort)node; // and insert the new node in the heap s.heap[SMALLEST]=node++; pqdownheap(s, tree, SMALLEST); } while(s.heap_len>=2); s.heap[--(s.heap_max)]=s.heap[SMALLEST]; // At this point, the fields freq and dad are set. We can now // generate the bit lengths. gen_bitlen(s, ref desc); // The field len is now set, we can generate the bit codes gen_codes(tree, max_code, s.bl_count); }
public static_tree_desc stat_desc; // the corresponding static tree public tree_desc(tree_desc desc) { dyn_tree=desc.dyn_tree; max_code=desc.max_code; stat_desc=desc.stat_desc; }