Esempio n. 1
0
        public int deflateEnd()
        {
            if (this.dstate == null)
            {
                return(-2);
            }
            int arg_1D_0 = this.dstate.deflateEnd();

            this.dstate = null;
            return(arg_1D_0);
        }
Esempio n. 2
0
        public int deflateEnd()
        {
            if (this.dstate == null)
            {
                return(-2);
            }
            int result = this.dstate.deflateEnd();

            this.dstate = null;
            return(result);
        }
        public int deflateEnd()
        {
            if (dstate == null)
            {
                return(Z_STREAM_ERROR);
            }
            int ret = dstate.deflateEnd();

            dstate = null;
            return(ret);
        }
Esempio n. 4
0
        internal void pqdownheap(short[] tree, int k)
        {
            int num1 = this.heap[k];

            for (int num2 = k << 1; num2 <= this.heap_len; num2 = num2 << 1)
            {
                if ((num2 < this.heap_len) && Deflate.smaller(tree, this.heap[num2 + 1], this.heap[num2], this.depth))
                {
                    num2++;
                }
                if (Deflate.smaller(tree, num1, this.heap[num2], this.depth))
                {
                    break;
                }
                this.heap[k] = this.heap[num2];
                k            = num2;
            }
            this.heap[k] = num1;
        }
Esempio n. 5
0
        internal void pqdownheap(short[] tree, int k)
        {
            int num = this.heap[k];

            for (int i = k << 1; i <= this.heap_len; i <<= 1)
            {
                if (i < this.heap_len && Deflate.smaller(tree, this.heap[i + 1], this.heap[i], this.depth))
                {
                    i++;
                }
                if (Deflate.smaller(tree, num, this.heap[i], this.depth))
                {
                    break;
                }
                this.heap[k] = this.heap[i];
                k            = i;
            }
            this.heap[k] = num;
        }
Esempio n. 6
0
        internal void gen_bitlen(Deflate s)
        {
            short[] array       = this.dyn_tree;
            short[] static_tree = this.stat_desc.static_tree;
            int[]   extra_bits  = this.stat_desc.extra_bits;
            int     extra_base  = this.stat_desc.extra_base;
            int     max_length  = this.stat_desc.max_length;
            int     num         = 0;

            for (int i = 0; i <= 15; i++)
            {
                s.bl_count[i] = 0;
            }
            array[s.heap[s.heap_max] * 2 + 1] = 0;
            int j;

            for (j = s.heap_max + 1; j < Tree.HEAP_SIZE; j++)
            {
                int num2 = s.heap[j];
                int i    = (int)(array[(int)(array[num2 * 2 + 1] * 2 + 1)] + 1);
                if (i > max_length)
                {
                    i = max_length;
                    num++;
                }
                array[num2 * 2 + 1] = (short)i;
                if (num2 <= this.max_code)
                {
                    short[] expr_C8_cp_0 = s.bl_count;
                    int     expr_C8_cp_1 = i;
                    expr_C8_cp_0[expr_C8_cp_1] += 1;
                    int num3 = 0;
                    if (num2 >= extra_base)
                    {
                        num3 = extra_bits[num2 - extra_base];
                    }
                    short num4 = array[num2 * 2];
                    s.opt_len += (int)num4 * (i + num3);
                    if (static_tree != null)
                    {
                        s.static_len += (int)num4 * ((int)static_tree[num2 * 2 + 1] + num3);
                    }
                }
            }
            if (num == 0)
            {
                return;
            }
            do
            {
                int i = max_length - 1;
                while (s.bl_count[i] == 0)
                {
                    i--;
                }
                short[] expr_156_cp_0 = s.bl_count;
                int     expr_156_cp_1 = i;
                expr_156_cp_0[expr_156_cp_1] -= 1;
                s.bl_count[i + 1]             = (short)(s.bl_count[i + 1] + 2);
                short[] expr_182_cp_0 = s.bl_count;
                int     expr_182_cp_1 = max_length;
                expr_182_cp_0[expr_182_cp_1] -= 1;
                num -= 2;
            }while (num > 0);
            for (int i = max_length; i != 0; i--)
            {
                int num2 = (int)s.bl_count[i];
                while (num2 != 0)
                {
                    int num5 = s.heap[--j];
                    if (num5 <= this.max_code)
                    {
                        if ((int)array[num5 * 2 + 1] != i)
                        {
                            s.opt_len           = (int)((long)s.opt_len + ((long)i - (long)array[num5 * 2 + 1]) * (long)array[num5 * 2]);
                            array[num5 * 2 + 1] = (short)i;
                        }
                        num2--;
                    }
                }
            }
        }
Esempio n. 7
0
        internal void gen_bitlen(Deflate s)
        {
            int num4;

            short[] numArray1 = this.dyn_tree;
            short[] numArray2 = this.stat_desc.static_tree;
            int[]   numArray3 = this.stat_desc.extra_bits;
            int     num1      = this.stat_desc.extra_base;
            int     num2      = this.stat_desc.max_length;
            int     num6      = 0;
            int     num5      = 0;

            while (num5 <= 15)
            {
                s.bl_count[num5] = 0;
                num5++;
            }
            numArray1[(s.heap[s.heap_max] * 2) + 1] = 0;
            int num3 = s.heap_max + 1;

            while (num3 < Tree.HEAP_SIZE)
            {
                num4 = s.heap[num3];
                num5 = numArray1[(numArray1[(num4 * 2) + 1] * 2) + 1] + 1;
                if (num5 > num2)
                {
                    num5 = num2;
                    num6++;
                }
                numArray1[(num4 * 2) + 1] = (short)num5;
                if (num4 <= this.max_code)
                {
                    s.bl_count[num5] = (short)(s.bl_count[num5] + 1);
                    int num7 = 0;
                    if (num4 >= num1)
                    {
                        num7 = numArray3[num4 - num1];
                    }
                    short num8 = numArray1[num4 * 2];
                    s.opt_len += num8 * (num5 + num7);
                    if (numArray2 != null)
                    {
                        s.static_len += num8 * (numArray2[(num4 * 2) + 1] + num7);
                    }
                }
                num3++;
            }
            if (num6 != 0)
            {
                do
                {
                    num5 = num2 - 1;
                    while (s.bl_count[num5] == 0)
                    {
                        num5--;
                    }
                    s.bl_count[num5]     = (short)(s.bl_count[num5] - 1);
                    s.bl_count[num5 + 1] = (short)(s.bl_count[num5 + 1] + 2);
                    s.bl_count[num2]     = (short)(s.bl_count[num2] - 1);
                    num6 -= 2;
                }while (num6 > 0);
                for (num5 = num2; num5 != 0; num5--)
                {
                    num4 = s.bl_count[num5];
                    while (num4 != 0)
                    {
                        int num9 = s.heap[--num3];
                        if (num9 <= this.max_code)
                        {
                            if (numArray1[(num9 * 2) + 1] != num5)
                            {
                                s.opt_len += (num5 - numArray1[(num9 * 2) + 1]) * numArray1[num9 * 2];
                                numArray1[(num9 * 2) + 1] = (short)num5;
                            }
                            num4--;
                        }
                    }
                }
            }
        }
Esempio n. 8
0
		public int deflateEnd()
		{
			if (dstate == null)
				return Z_STREAM_ERROR;
			var ret = dstate.deflateEnd();
			dstate = null;
			return ret;
		}
Esempio n. 9
0
        internal void build_tree(Deflate s)
        {
            short[] array       = this.dyn_tree;
            short[] static_tree = this.stat_desc.static_tree;
            int     elems       = this.stat_desc.elems;
            int     num         = -1;

            s.heap_len = 0;
            s.heap_max = Tree.HEAP_SIZE;
            int num2;

            for (int i = 0; i < elems; i++)
            {
                if (array[i * 2] != 0)
                {
                    int[] arg_5D_0 = s.heap;
                    num2       = s.heap_len + 1;
                    s.heap_len = num2;
                    num        = (arg_5D_0[num2] = i);
                    s.depth[i] = 0;
                }
                else
                {
                    array[i * 2 + 1] = 0;
                }
            }
            int num3;

            while (s.heap_len < 2)
            {
                int[] arg_A7_0 = s.heap;
                num2            = s.heap_len + 1;
                s.heap_len      = num2;
                num3            = (arg_A7_0[num2] = ((num < 2) ? (++num) : 0));
                array[num3 * 2] = 1;
                s.depth[num3]   = 0;
                s.opt_len--;
                if (static_tree != null)
                {
                    s.static_len -= (int)static_tree[num3 * 2 + 1];
                }
            }
            this.max_code = num;
            for (int i = s.heap_len / 2; i >= 1; i--)
            {
                s.pqdownheap(array, i);
            }
            num3 = elems;
            do
            {
                int   i         = s.heap[1];
                int[] arg_13D_0 = s.heap;
                int   arg_13D_1 = 1;
                int[] arg_13C_0 = s.heap;
                num2                 = s.heap_len;
                s.heap_len           = num2 - 1;
                arg_13D_0[arg_13D_1] = arg_13C_0[num2];
                s.pqdownheap(array, 1);
                int   num4      = s.heap[1];
                int[] arg_16B_0 = s.heap;
                num2            = s.heap_max - 1;
                s.heap_max      = num2;
                arg_16B_0[num2] = i;
                int[] arg_188_0 = s.heap;
                num2             = s.heap_max - 1;
                s.heap_max       = num2;
                arg_188_0[num2]  = num4;
                array[num3 * 2]  = (short)(array[i * 2] + array[num4 * 2]);
                s.depth[num3]    = (byte)(Math.Max(s.depth[i], s.depth[num4]) + 1);
                array[i * 2 + 1] = (array[num4 * 2 + 1] = (short)num3);
                s.heap[1]        = num3++;
                s.pqdownheap(array, 1);
            }while (s.heap_len >= 2);
            int[] arg_21A_0 = s.heap;
            num2            = s.heap_max - 1;
            s.heap_max      = num2;
            arg_21A_0[num2] = s.heap[1];
            this.gen_bitlen(s);
            Tree.gen_codes(array, num, s.bl_count);
        }
Esempio n. 10
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.
        internal void  build_tree(Deflate s)
        {
            short[] tree = dyn_tree;
            short[] stree = stat_desc.static_tree;
            int     elems = 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[1]. 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 * 2] != 0)
                {
                    s.heap[++s.heap_len] = max_code = n;
                    s.depth[n]           = 0;
                }
                else
                {
                    tree[n * 2 + 1] = 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 * 2] = 1;
                s.depth[node]  = 0;
                s.opt_len--;
                if (stree != null)
                {
                    s.static_len -= stree[node * 2 + 1];
                }
                // node is 0 or 1 so it does not have extra bits
            }
            this.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--)
            {
                s.pqdownheap(tree, n);
            }

            // Construct the Huffman tree by repeatedly combining the least two
            // frequent nodes.

            node = elems;             // next internal node of the tree
            do
            {
                // n = node of least frequency
                n         = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(tree, 1);
                m = s.heap[1];                 // 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 * 2]  = (short)(tree[n * 2] + tree[m * 2]);
                s.depth[node]   = (byte)(System.Math.Max((byte)s.depth[n], (byte)s.depth[m]) + 1);
                tree[n * 2 + 1] = tree[m * 2 + 1] = (short)node;

                // and insert the new node in the heap
                s.heap[1] = node++;
                s.pqdownheap(tree, 1);
            }while (s.heap_len >= 2);

            s.heap[--s.heap_max] = s.heap[1];

            // At this point, the fields freq and dad are set. We can now
            // generate the bit lengths.

            gen_bitlen(s);

            // The field len is now set, we can generate the bit codes
            gen_codes(tree, max_code, s.bl_count);
        }
Esempio n. 11
0
		public int deflateInit(int level, int bits)
		{
			dstate = new Deflate();
			return dstate.deflateInit(this, level, bits);
		}
Esempio n. 12
0
File: cs.cs Progetto: J5lx/luminous
        // 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.
        internal void gen_bitlen(Deflate s)
        {
            short[] tree = dyn_tree;
                        short[] stree = stat_desc.static_tree;
                        int[] extra = stat_desc.extra_bits;
                        int base_Renamed = stat_desc.extra_base;
                        int max_length = stat_desc.max_length;
                        int h; // heap index
                        int n, m; // iterate over the tree elements
                        int bits; // bit length
                        int xbits; // extra bits
                        short 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] * 2 + 1] = 0; // root of the heap

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

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

                                s.bl_count[bits]++;
                                xbits = 0;
                                if (n >= base_Renamed)
                                        xbits = extra[n - base_Renamed];
                                f = tree[n * 2];
                                s.opt_len += f * (bits + xbits);
                                if (stree != null)
                                        s.static_len += f * (stree[n * 2 + 1] + 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 (s.bl_count[bits] == 0)
                                        bits--;
                                s.bl_count[bits]--; // move one leaf down the tree
                                s.bl_count[bits + 1] = (short) (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);

                        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 (tree[m * 2 + 1] != bits)
                                        {
                                                s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
                                                tree[m * 2 + 1] = (short) bits;
                                        }
                                        n--;
                                }
                        }
        }
Esempio n. 13
0
        internal StaticTree stat_desc; // the corresponding static tree

        // 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.
        internal void  gen_bitlen(Deflate s)
        {
            short[] tree         = dyn_tree;
            short[] stree        = stat_desc.static_tree;
            int[]   extra        = stat_desc.extra_bits;
            int     base_Renamed = stat_desc.extra_base;
            int     max_length   = stat_desc.max_length;
            int     h;            // heap index
            int     n, m;         // iterate over the tree elements
            int     bits;         // bit length
            int     xbits;        // extra bits
            short   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] * 2 + 1] = 0;             // root of the heap

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

                if (n > max_code)
                {
                    continue;                     // not a leaf node
                }
                s.bl_count[bits]++;
                xbits = 0;
                if (n >= base_Renamed)
                {
                    xbits = extra[n - base_Renamed];
                }
                f          = tree[n * 2];
                s.opt_len += f * (bits + xbits);
                if (stree != null)
                {
                    s.static_len += f * (stree[n * 2 + 1] + 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 (s.bl_count[bits] == 0)
                {
                    bits--;
                }
                s.bl_count[bits]--;                                       // move one leaf down the tree
                s.bl_count[bits + 1] = (short)(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);

            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 (tree[m * 2 + 1] != bits)
                    {
                        s.opt_len       = (int)(s.opt_len + ((long)bits - (long)tree[m * 2 + 1]) * (long)tree[m * 2]);
                        tree[m * 2 + 1] = (short)bits;
                    }
                    n--;
                }
            }
        }
 public int deflateInit(int level, int bits)
 {
     dstate = new Deflate();
     return(dstate.deflateInit(this, level, bits));
 }
Esempio n. 15
0
 internal void gen_bitlen(Deflate s)
 {
     int num4;
     short[] numArray4;
     IntPtr ptr;
     short[] numArray = this.dyn_tree;
     short[] numArray2 = this.stat_desc.static_tree;
     int[] numArray3 = this.stat_desc.extra_bits;
     int num = this.stat_desc.extra_base;
     int num2 = this.stat_desc.max_length;
     int num9 = 0;
     int index = 0;
     while (index <= 15)
     {
         s.bl_count[index] = 0;
         index++;
     }
     numArray[(s.heap[s.heap_max] * 2) + 1] = 0;
     int num3 = s.heap_max + 1;
     while (num3 < HEAP_SIZE)
     {
         num4 = s.heap[num3];
         index = numArray[(numArray[(num4 * 2) + 1] * 2) + 1] + 1;
         if (index > num2)
         {
             index = num2;
             num9++;
         }
         numArray[(num4 * 2) + 1] = (short) index;
         if (num4 <= this.max_code)
         {
             (numArray4 = s.bl_count)[(int) (ptr = (IntPtr) index)] = (short) (numArray4[(int) ptr] + 1);
             int num7 = 0;
             if (num4 >= num)
             {
                 num7 = numArray3[num4 - num];
             }
             short num8 = numArray[num4 * 2];
             s.opt_len += num8 * (index + num7);
             if (numArray2 != null)
             {
                 s.static_len += num8 * (numArray2[(num4 * 2) + 1] + num7);
             }
         }
         num3++;
     }
     if (num9 != 0)
     {
         do
         {
             index = num2 - 1;
             while (s.bl_count[index] == 0)
             {
                 index--;
             }
             (numArray4 = s.bl_count)[(int) (ptr = (IntPtr) index)] = (short) (numArray4[(int) ptr] - 1);
             s.bl_count[index + 1] = (short) (s.bl_count[index + 1] + 2);
             (numArray4 = s.bl_count)[(int) (ptr = (IntPtr) num2)] = (short) (numArray4[(int) ptr] - 1);
             num9 -= 2;
         }
         while (num9 > 0);
         for (index = num2; index != 0; index--)
         {
             num4 = s.bl_count[index];
             while (num4 != 0)
             {
                 int num5 = s.heap[--num3];
                 if (num5 <= this.max_code)
                 {
                     if (numArray[(num5 * 2) + 1] != index)
                     {
                         s.opt_len += (index - numArray[(num5 * 2) + 1]) * numArray[num5 * 2];
                         numArray[(num5 * 2) + 1] = (short) index;
                     }
                     num4--;
                 }
             }
         }
     }
 }
Esempio n. 16
0
        internal void build_tree(Deflate s)
        {
            int num4;

            short[] numArray1 = this.dyn_tree;
            short[] numArray2 = this.stat_desc.static_tree;
            int     num1      = this.stat_desc.elems;
            int     num3      = -1;

            s.heap_len = 0;
            s.heap_max = Tree.HEAP_SIZE;
            int num2 = 0;

            while (num2 < num1)
            {
                if (numArray1[num2 * 2] != 0)
                {
                    s.heap[++s.heap_len] = num3 = num2;
                    s.depth[num2]        = 0;
                }
                else
                {
                    numArray1[(num2 * 2) + 1] = 0;
                }
                num2++;
            }
            while (s.heap_len < 2)
            {
                num4 = s.heap[++s.heap_len] = (num3 < 2) ? ++num3 : 0;
                numArray1[num4 * 2] = 1;
                s.depth[num4]       = 0;
                s.opt_len--;
                if (numArray2 != null)
                {
                    s.static_len -= numArray2[(num4 * 2) + 1];
                }
            }
            this.max_code = num3;
            num2          = s.heap_len / 2;
            while (num2 >= 1)
            {
                s.pqdownheap(numArray1, num2);
                num2--;
            }
            num4 = num1;
            do
            {
                num2      = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(numArray1, 1);
                int num5 = s.heap[1];
                s.heap[--s.heap_max]      = num2;
                s.heap[--s.heap_max]      = num5;
                numArray1[num4 * 2]       = (short)(numArray1[num2 * 2] + numArray1[num5 * 2]);
                s.depth[num4]             = (byte)(Math.Max(s.depth[num2], s.depth[num5]) + 1);
                numArray1[(num2 * 2) + 1] = numArray1[(num5 * 2) + 1] = (short)num4;
                s.heap[1] = num4++;
                s.pqdownheap(numArray1, 1);
            }while (s.heap_len >= 2);
            s.heap[--s.heap_max] = s.heap[1];
            this.gen_bitlen(s);
            Tree.gen_codes(numArray1, num3, s.bl_count);
        }
Esempio n. 17
0
 internal void gen_bitlen(Deflate s)
 {
     int num4;
     short[] numArray1 = this.dyn_tree;
     short[] numArray2 = this.stat_desc.static_tree;
     int[] numArray3 = this.stat_desc.extra_bits;
     int num1 = this.stat_desc.extra_base;
     int num2 = this.stat_desc.max_length;
     int num6 = 0;
     int num5 = 0;
     while (num5 <= 15)
     {
         s.bl_count[num5] = 0;
         num5++;
     }
     numArray1[(s.heap[s.heap_max] * 2) + 1] = 0;
     int num3 = s.heap_max + 1;
     while (num3 < Tree.HEAP_SIZE)
     {
         num4 = s.heap[num3];
         num5 = numArray1[(numArray1[(num4 * 2) + 1] * 2) + 1] + 1;
         if (num5 > num2)
         {
             num5 = num2;
             num6++;
         }
         numArray1[(num4 * 2) + 1] = (short) num5;
         if (num4 <= this.max_code)
         {
             s.bl_count[num5] = (short) (s.bl_count[num5] + 1);
             int num7 = 0;
             if (num4 >= num1)
             {
                 num7 = numArray3[num4 - num1];
             }
             short num8 = numArray1[num4 * 2];
             s.opt_len += num8 * (num5 + num7);
             if (numArray2 != null)
             {
                 s.static_len += num8 * (numArray2[(num4 * 2) + 1] + num7);
             }
         }
         num3++;
     }
     if (num6 != 0)
     {
         do
         {
             num5 = num2 - 1;
             while (s.bl_count[num5] == 0)
             {
                 num5--;
             }
             s.bl_count[num5] = (short) (s.bl_count[num5] - 1);
             s.bl_count[num5 + 1] = (short) (s.bl_count[num5 + 1] + 2);
             s.bl_count[num2] = (short) (s.bl_count[num2] - 1);
             num6 -= 2;
         }
         while (num6 > 0);
         for (num5 = num2; num5 != 0; num5--)
         {
             num4 = s.bl_count[num5];
             while (num4 != 0)
             {
                 int num9 = s.heap[--num3];
                 if (num9 <= this.max_code)
                 {
                     if (numArray1[(num9 * 2) + 1] != num5)
                     {
                         s.opt_len += (num5 - numArray1[(num9 * 2) + 1]) * numArray1[num9 * 2];
                         numArray1[(num9 * 2) + 1] = (short) num5;
                     }
                     num4--;
                 }
             }
         }
     }
 }
Esempio n. 18
0
 internal void build_tree(Deflate s)
 {
     int num4;
     short[] numArray1 = this.dyn_tree;
     short[] numArray2 = this.stat_desc.static_tree;
     int num1 = this.stat_desc.elems;
     int num3 = -1;
     s.heap_len = 0;
     s.heap_max = Tree.HEAP_SIZE;
     int num2 = 0;
     while (num2 < num1)
     {
         if (numArray1[num2 * 2] != 0)
         {
             s.heap[++s.heap_len] = num3 = num2;
             s.depth[num2] = 0;
         }
         else
         {
             numArray1[(num2 * 2) + 1] = 0;
         }
         num2++;
     }
     while (s.heap_len < 2)
     {
         num4 = s.heap[++s.heap_len] = (num3 < 2) ? ++num3 : 0;
         numArray1[num4 * 2] = 1;
         s.depth[num4] = 0;
         s.opt_len--;
         if (numArray2 != null)
         {
             s.static_len -= numArray2[(num4 * 2) + 1];
         }
     }
     this.max_code = num3;
     num2 = s.heap_len / 2;
     while (num2 >= 1)
     {
         s.pqdownheap(numArray1, num2);
         num2--;
     }
     num4 = num1;
     do
     {
         num2 = s.heap[1];
         s.heap[1] = s.heap[s.heap_len--];
         s.pqdownheap(numArray1, 1);
         int num5 = s.heap[1];
         s.heap[--s.heap_max] = num2;
         s.heap[--s.heap_max] = num5;
         numArray1[num4 * 2] = (short) (numArray1[num2 * 2] + numArray1[num5 * 2]);
         s.depth[num4] = (byte) (Math.Max(s.depth[num2], s.depth[num5]) + 1);
         numArray1[(num2 * 2) + 1] = numArray1[(num5 * 2) + 1] = (short) num4;
         s.heap[1] = num4++;
         s.pqdownheap(numArray1, 1);
     }
     while (s.heap_len >= 2);
     s.heap[--s.heap_max] = s.heap[1];
     this.gen_bitlen(s);
     Tree.gen_codes(numArray1, num3, s.bl_count);
 }
Esempio n. 19
0
        internal void build_tree(Deflate s)
        {
            int num2;
            int num5;

            short[] tree      = this.dyn_tree;
            short[] numArray2 = this.stat_desc.static_tree;
            int     elems     = this.stat_desc.elems;
            int     num4      = -1;

            s.heap_len = 0;
            s.heap_max = HEAP_SIZE;
            for (num2 = 0; num2 < elems; num2++)
            {
                if (tree[num2 * 2] != 0)
                {
                    s.heap[++s.heap_len] = num4 = num2;
                    s.depth[num2]        = 0;
                }
                else
                {
                    tree[(num2 * 2) + 1] = 0;
                }
            }
            while (s.heap_len < 2)
            {
                num5           = s.heap[++s.heap_len] = (num4 < 2) ? ++num4 : 0;
                tree[num5 * 2] = 1;
                s.depth[num5]  = 0;
                s.opt_len--;
                if (numArray2 != null)
                {
                    s.static_len -= numArray2[(num5 * 2) + 1];
                }
            }
            this.max_code = num4;
            num2          = s.heap_len / 2;
            while (num2 >= 1)
            {
                s.pqdownheap(tree, num2);
                num2--;
            }
            num5 = elems;
            do
            {
                num2      = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(tree, 1);
                int index = s.heap[1];
                s.heap[--s.heap_max] = num2;
                s.heap[--s.heap_max] = index;
                tree[num5 * 2]       = (short)(tree[num2 * 2] + tree[index * 2]);
                s.depth[num5]        = (byte)(Math.Max(s.depth[num2], s.depth[index]) + 1);
                tree[(num2 * 2) + 1] = tree[(index * 2) + 1] = (short)num5;
                s.heap[1]            = num5++;
                s.pqdownheap(tree, 1);
            }while (s.heap_len >= 2);
            s.heap[--s.heap_max] = s.heap[1];
            this.gen_bitlen(s);
            gen_codes(tree, num4, s.bl_count);
        }
Esempio n. 20
0
        internal void gen_bitlen(Deflate s)
        {
            int num4;

            short[] numArray  = this.dyn_tree;
            short[] numArray2 = this.stat_desc.static_tree;
            int[]   numArray3 = this.stat_desc.extra_bits;
            int     num       = this.stat_desc.extra_base;
            int     index     = this.stat_desc.max_length;
            int     num9      = 0;
            int     num6      = 0;

            while (num6 <= 15)
            {
                s.bl_count[num6] = 0;
                num6++;
            }
            numArray[(s.heap[s.heap_max] * 2) + 1] = 0;
            int num3 = s.heap_max + 1;

            while (num3 < HEAP_SIZE)
            {
                num4 = s.heap[num3];
                num6 = numArray[(numArray[(num4 * 2) + 1] * 2) + 1] + 1;
                if (num6 > index)
                {
                    num6 = index;
                    num9++;
                }
                numArray[(num4 * 2) + 1] = (short)num6;
                if (num4 <= this.max_code)
                {
                    s.bl_count[num6] = (short)(s.bl_count[num6] + 1);
                    int num7 = 0;
                    if (num4 >= num)
                    {
                        num7 = numArray3[num4 - num];
                    }
                    short num8 = numArray[num4 * 2];
                    s.opt_len += num8 * (num6 + num7);
                    if (numArray2 != null)
                    {
                        s.static_len += num8 * (numArray2[(num4 * 2) + 1] + num7);
                    }
                }
                num3++;
            }
            if (num9 != 0)
            {
                do
                {
                    num6 = index - 1;
                    while (s.bl_count[num6] == 0)
                    {
                        num6--;
                    }
                    s.bl_count[num6]     = (short)(s.bl_count[num6] - 1);
                    s.bl_count[num6 + 1] = (short)(s.bl_count[num6 + 1] + 2);
                    s.bl_count[index]    = (short)(s.bl_count[index] - 1);
                    num9 -= 2;
                }while (num9 > 0);
                for (num6 = index; num6 != 0; num6--)
                {
                    num4 = s.bl_count[num6];
                    while (num4 != 0)
                    {
                        int num5 = s.heap[--num3];
                        if (num5 <= this.max_code)
                        {
                            if (numArray[(num5 * 2) + 1] != num6)
                            {
                                s.opt_len += (num6 - numArray[(num5 * 2) + 1]) * numArray[num5 * 2];
                                numArray[(num5 * 2) + 1] = (short)num6;
                            }
                            num4--;
                        }
                    }
                }
            }
        }
Esempio n. 21
0
File: cs.cs Progetto: J5lx/luminous
        // 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.
        internal void build_tree(Deflate s)
        {
            short[] tree = dyn_tree;
                        short[] stree = stat_desc.static_tree;
                        int elems = 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[1]. 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 * 2] != 0)
                                {
                                        s.heap[++s.heap_len] = max_code = n;
                                        s.depth[n] = 0;
                                }
                                else
                                {
                                        tree[n * 2 + 1] = 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 * 2] = 1;
                                s.depth[node] = 0;
                                s.opt_len--;
                                if (stree != null)
                                        s.static_len -= stree[node * 2 + 1];
                                // node is 0 or 1 so it does not have extra bits
                        }
                        this.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--)
                                s.pqdownheap(tree, n);

                        // Construct the Huffman tree by repeatedly combining the least two
                        // frequent nodes.

                        node = elems; // next internal node of the tree
                        do
                        {
                                // n = node of least frequency
                                n = s.heap[1];
                                s.heap[1] = s.heap[s.heap_len--];
                                s.pqdownheap(tree, 1);
                                m = s.heap[1]; // 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 * 2] = (short) (tree[n * 2] + tree[m * 2]);
                                s.depth[node] = (byte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
                                tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;

                                // and insert the new node in the heap
                                s.heap[1] = node++;
                                s.pqdownheap(tree, 1);
                        }
                        while (s.heap_len >= 2);

                        s.heap[--s.heap_max] = s.heap[1];

                        // At this point, the fields freq and dad are set. We can now
                        // generate the bit lengths.

                        gen_bitlen(s);

                        // The field len is now set, we can generate the bit codes
                        gen_codes(tree, max_code, s.bl_count);
        }
Esempio n. 22
0
 internal void build_tree(Deflate s)
 {
     int num2;
     int num5;
     short[] tree = this.dyn_tree;
     short[] numArray2 = this.stat_desc.static_tree;
     int elems = this.stat_desc.elems;
     int num4 = -1;
     s.heap_len = 0;
     s.heap_max = HEAP_SIZE;
     for (num2 = 0; num2 < elems; num2++)
     {
         if (tree[num2 * 2] != 0)
         {
             s.heap[++s.heap_len] = num4 = num2;
             s.depth[num2] = 0;
         }
         else
         {
             tree[(num2 * 2) + 1] = 0;
         }
     }
     while (s.heap_len < 2)
     {
         num5 = s.heap[++s.heap_len] = (num4 < 2) ? ++num4 : 0;
         tree[num5 * 2] = 1;
         s.depth[num5] = 0;
         s.opt_len--;
         if (numArray2 != null)
         {
             s.static_len -= numArray2[(num5 * 2) + 1];
         }
     }
     this.max_code = num4;
     num2 = s.heap_len / 2;
     while (num2 >= 1)
     {
         s.pqdownheap(tree, num2);
         num2--;
     }
     num5 = elems;
     do
     {
         num2 = s.heap[1];
         s.heap[1] = s.heap[s.heap_len--];
         s.pqdownheap(tree, 1);
         int index = s.heap[1];
         s.heap[--s.heap_max] = num2;
         s.heap[--s.heap_max] = index;
         tree[num5 * 2] = (short) (tree[num2 * 2] + tree[index * 2]);
         s.depth[num5] = (byte) (Math.Max(s.depth[num2], s.depth[index]) + 1);
         tree[(num2 * 2) + 1] = tree[(index * 2) + 1] = (short) num5;
         s.heap[1] = num5++;
         s.pqdownheap(tree, 1);
     }
     while (s.heap_len >= 2);
     s.heap[--s.heap_max] = s.heap[1];
     this.gen_bitlen(s);
     gen_codes(tree, num4, s.bl_count);
 }