Exemplo n.º 1
0
    /* ==========================================================================
    *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.
     */
    void build_tree(DeflateTreeDesc desc)
    {
        // the tree descriptor
        DeflateCT[] tree	=desc.dyn_tree;
        DeflateCT[] stree	=desc.static_tree;
        int elems			=desc.elems;
        int n,m;		// iterate over heap elements
        int max_code=-1;	// largest code with non zero frequency
        int node=elems;	// next internal node of the tree

        /* 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.
         */
        heap_len=0;
        heap_max=HEAP_SIZE;

        for(n=0;n<elems;n++){
            if(tree[n].fc!=0){
                heap[++heap_len]=max_code=n;
                depth[n]=0;
            }else{
                tree[n].dl=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(heap_len<2){
            int xnew=heap[++heap_len]=(max_code<2?++max_code:0);
            tree[xnew].fc=1;
            depth[xnew]=0;
            opt_len--;
            if(stree!=null)static_len-=stree[xnew].dl;
            // new 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=heap_len>>1;n>=1;n--)pqdownheap(tree,n);

        /* Construct the Huffman tree by repeatedly combining the least two
        *frequent nodes.
         */
        do{
            n=heap[SMALLEST];
            heap[SMALLEST]=heap[heap_len--];
            pqdownheap(tree,SMALLEST);

            m=heap[SMALLEST]; // m=node of next least frequency

            // keep the nodes sorted by frequency
            heap[--heap_max]=n;
            heap[--heap_max]=m;

            // Create a new node father of n and m
            tree[node].fc=tree[n].fc+tree[m].fc;
        //	depth[node]=(char)(MAX(depth[n],depth[m])+1);
            if(depth[n]>depth[m]+1){
                depth[node]=depth[n];
            }else{
                depth[node]=depth[m]+1;
            }
            tree[n].dl=tree[m].dl=node;

            // and insert the new node in the heap
            heap[SMALLEST]=node++;
            pqdownheap(tree,SMALLEST);

        }while(heap_len>=2);

        heap[--heap_max]=heap[SMALLEST];

        /* At this point,the fields freq and dad are set. We can now
        *generate the bit lengths.
         */
        gen_bitlen(desc);

        // The field len is now set,we can generate the bit codes
        gen_codes(tree,max_code);
    }
Exemplo n.º 2
0
    void deflate_start(int level)
    {
        if(level==0)
            level=DEFAULT_LEVEL;
        else if(level<1)
            level=1;
        else if(level>9)
            level=9;

        this.compr_level=level;
        this.initflag=false;
        this.eofile=false;
        if(outbuf!=null)return;

        this.free_queue=this.qhead=this.qtail=null;
        this.outbuf=new byte[OUTBUFSIZ];
        this.win=new byte[window_size];
        this.d_buf=new int[DIST_BUFSIZE];
        this.l_buf=new int[INBUFSIZ+INBUF_EXTRA];
        this.prev=new int[1<<BITS];

        this.dyn_ltree=new_ctarray(HEAP_SIZE);
        this.dyn_dtree=new_ctarray(2*D_CODES+1);
        this.static_ltree=new_ctarray(L_CODES+2);
        this.static_dtree=new_ctarray(D_CODES);
        this.bl_tree=new_ctarray(2*BL_CODES+1);

        this.l_desc=new DeflateTreeDesc();
        this.d_desc=new DeflateTreeDesc();
        this.bl_desc=new DeflateTreeDesc();
        this.bl_count=new int[MAX_BITS+1];
        this.heap=new int[2*L_CODES+1];
        this.depth=new int[2*L_CODES+1];
        this.length_code=new int[MAX_MATCH-MIN_MATCH+1];
        this.dist_code=new int[512];
        this.base_length=new int[LENGTH_CODES];
        this.base_dist=new int[D_CODES];
        this.flag_buf=new int[LIT_BUFSIZE/8];
    }
Exemplo n.º 3
0
    /* ==========================================================================
    *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.
     */
    void gen_bitlen(DeflateTreeDesc desc)
    {
        // the tree descriptor
        DeflateCT[] tree	=desc.dyn_tree;
        int[] extra			=desc.extra_bits;
        int @base			=desc.extra_base;
        int max_code		=desc.max_code;
        int max_length		=desc.max_length;
        DeflateCT[] stree	=desc.static_tree;
        int h;		// heap index
        int n,m;		// iterate over the tree elements
        int bits;		// bit length
        int xbits;		// extra bits
        int f;		// frequency
        int overflow=0;	// number of elements with bit length too large

        for(bits=0;bits<=MAX_BITS;bits++)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[heap[heap_max]].dl=0;// root of the heap

        for(h=heap_max+1;h<HEAP_SIZE;h++){
            n=heap[h];
            bits=tree[tree[n].dl].dl+1;
            if(bits>max_length){
                bits=max_length;
                overflow++;
            }
            tree[n].dl=bits;
            // We overwrite tree[n].dl which is no longer needed

            if(n>max_code)continue;// not a leaf node

            bl_count[bits]++;
            xbits=0;
            if(n>=@base)xbits=extra[n-@base];
            f=tree[n].fc;
            opt_len+=f*(bits+xbits);
            if(stree!=null)static_len+=f*(stree[n].dl+xbits);
        }
        if(overflow==0)return;

        // 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(bl_count[bits]==0)bits--;
            bl_count[bits]--;		// move one leaf down the tree
            bl_count[bits+1]+=2;	// move one overflow item as its brother
            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=bl_count[bits];
            while(n!=0){
                m=heap[--h];
                if(m>max_code)continue;
                if(tree[m].dl!=bits){
                    opt_len+=(bits-tree[m].dl)*tree[m].fc;
                    tree[m].fc=bits;
                }
                n--;
            }
        }
    }