Example #1
0
		public int deflateEnd()
		{
			if (this.dstate == null)
			{
				return -2;
			}
			int num = this.dstate.deflateEnd();
			this.dstate = null;
			return num;
		}
Example #2
0
        public int deflateEnd()
        {
            if (dstate == null)
            {
                return(Z_STREAM_ERROR);
            }
            int ret = dstate.deflateEnd();

            dstate = null;
            return(ret);
        }
Example #3
0
        public int deflateEnd()
        {
            if (dstate == null)
            {
                return(-2);
            }
            int result = dstate.deflateEnd();

            dstate = null;
            return(result);
        }
Example #4
0
 public int deflateInit(int level, int bits)
 {
     dstate = new Deflate();
     return(dstate.deflateInit(this, level, bits));
 }
Example #5
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);
		}
Example #6
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--;
				}
			}
		}
Example #7
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);
        }
Example #8
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--;
                }
            }
        }
Example #9
0
		public int deflateEnd()
		{
			if (dstate == null)
				return Z_STREAM_ERROR;
			int ret = dstate.deflateEnd();
			dstate = null;
			return ret;
		}
Example #10
0
		public int deflateInit(int level, int bits)
		{
			dstate = new Deflate();
			return dstate.deflateInit(this, level, bits);
		}
Example #11
0
        internal void build_tree(Deflate s)
        {
            int i;
            int num;
            int num1;

            short[] dynTree    = this.dyn_tree;
            short[] staticTree = this.stat_desc.static_tree;
            int     statDesc   = this.stat_desc.elems;
            int     num2       = -1;

            s.heap_len = 0;
            s.heap_max = Tree.HEAP_SIZE;
            for (i = 0; i < statDesc; i++)
            {
                if (dynTree[i * 2] == 0)
                {
                    dynTree[i * 2 + 1] = 0;
                }
                else
                {
                    Deflate deflate = s;
                    int     heapLen = deflate.heap_len + 1;
                    int     num3    = heapLen;
                    deflate.heap_len = heapLen;
                    int num4 = i;
                    num2         = num4;
                    s.heap[num3] = num4;
                    s.depth[i]   = 0;
                }
            }
            while (s.heap_len < 2)
            {
                int[]   numArray = s.heap;
                Deflate deflate1 = s;
                int     heapLen1 = deflate1.heap_len + 1;
                int     num5     = heapLen1;
                deflate1.heap_len = heapLen1;
                int num6 = num5;
                if (num2 < 2)
                {
                    num1 = num2 + 1;
                    num2 = num1;
                }
                else
                {
                    num1 = 0;
                }
                int num7 = num1;
                numArray[num6]   = num1;
                num              = num7;
                dynTree[num * 2] = 1;
                s.depth[num]     = 0;
                Deflate optLen = s;
                optLen.opt_len = optLen.opt_len - 1;
                if (staticTree == null)
                {
                    continue;
                }
                Deflate staticLen = s;
                staticLen.static_len = staticLen.static_len - staticTree[num * 2 + 1];
            }
            this.max_code = num2;
            for (i = s.heap_len / 2; i >= 1; i--)
            {
                s.pqdownheap(dynTree, i);
            }
            num = statDesc;
            do
            {
                i = s.heap[1];
                int[]   numArray1 = s.heap;
                int[]   numArray2 = s.heap;
                Deflate deflate2  = s;
                int     heapLen2  = deflate2.heap_len;
                int     num8      = heapLen2;
                deflate2.heap_len = heapLen2 - 1;
                numArray1[1]      = numArray2[num8];
                s.pqdownheap(dynTree, 1);
                int     num9     = s.heap[1];
                Deflate deflate3 = s;
                int     heapMax  = deflate3.heap_max - 1;
                int     num10    = heapMax;
                deflate3.heap_max = heapMax;
                s.heap[num10]     = i;
                Deflate deflate4 = s;
                int     heapMax1 = deflate4.heap_max - 1;
                int     num11    = heapMax1;
                deflate4.heap_max = heapMax1;
                s.heap[num11]     = num9;
                dynTree[num * 2]  = (short)(dynTree[i * 2] + dynTree[num9 * 2]);
                s.depth[num]      = (byte)(Math.Max(s.depth[i], s.depth[num9]) + 1);
                short num12 = (short)num;
                short num13 = num12;
                dynTree[num9 * 2 + 1] = num12;
                dynTree[i * 2 + 1]    = num13;
                int num14 = num;
                num       = num14 + 1;
                s.heap[1] = num14;
                s.pqdownheap(dynTree, 1);
            }while (s.heap_len >= 2);
            Deflate deflate5 = s;
            int     heapMax2 = deflate5.heap_max - 1;
            int     num15    = heapMax2;

            deflate5.heap_max = heapMax2;
            s.heap[num15]     = s.heap[1];
            this.gen_bitlen(s);
            Tree.gen_codes(dynTree, num2, s.bl_count);
        }
Example #12
0
        internal void gen_bitlen(Deflate s)
        {
            int j;
            int blCount;
            int i;

            short[] dynTree    = this.dyn_tree;
            short[] staticTree = this.stat_desc.static_tree;
            int[]   extraBits  = this.stat_desc.extra_bits;
            int     extraBase  = this.stat_desc.extra_base;
            int     maxLength  = this.stat_desc.max_length;
            int     num        = 0;

            for (i = 0; i <= 15; i++)
            {
                s.bl_count[i] = 0;
            }
            dynTree[s.heap[s.heap_max] * 2 + 1] = 0;
            for (j = s.heap_max + 1; j < Tree.HEAP_SIZE; j++)
            {
                blCount = s.heap[j];
                i       = dynTree[dynTree[blCount * 2 + 1] * 2 + 1] + 1;
                if (i > maxLength)
                {
                    i = maxLength;
                    num++;
                }
                dynTree[blCount * 2 + 1] = (short)i;
                if (blCount <= this.max_code)
                {
                    s.bl_count[i] = (short)(s.bl_count[i] + 1);
                    int num1 = 0;
                    if (blCount >= extraBase)
                    {
                        num1 = extraBits[blCount - extraBase];
                    }
                    short   num2   = dynTree[blCount * 2];
                    Deflate optLen = s;
                    optLen.opt_len = optLen.opt_len + num2 * (i + num1);
                    if (staticTree != null)
                    {
                        Deflate staticLen = s;
                        staticLen.static_len = staticLen.static_len + num2 * (staticTree[blCount * 2 + 1] + num1);
                    }
                }
            }
            if (num == 0)
            {
                return;
            }
            do
            {
                i = maxLength - 1;
                while (s.bl_count[i] == 0)
                {
                    i--;
                }
                s.bl_count[i]         = (short)(s.bl_count[i] - 1);
                s.bl_count[i + 1]     = (short)(s.bl_count[i + 1] + 2);
                s.bl_count[maxLength] = (short)(s.bl_count[maxLength] - 1);
                num = num - 2;
            }while (num > 0);
            for (i = maxLength; i != 0; i--)
            {
                blCount = s.bl_count[i];
                while (blCount != 0)
                {
                    int num3 = j - 1;
                    j = num3;
                    int num4 = s.heap[num3];
                    if (num4 > this.max_code)
                    {
                        continue;
                    }
                    if (dynTree[num4 * 2 + 1] != i)
                    {
                        s.opt_len             = (int)((long)s.opt_len + ((long)i - (long)dynTree[num4 * 2 + 1]) * (long)dynTree[num4 * 2]);
                        dynTree[num4 * 2 + 1] = (short)i;
                    }
                    blCount--;
                }
            }
        }
Example #13
0
        internal void gen_bitlen(Deflate s)
        {
            short[] array       = dyn_tree;
            short[] static_tree = stat_desc.static_tree;
            int[]   extra_bits  = stat_desc.extra_bits;
            int     extra_base  = stat_desc.extra_base;
            int     max_length  = 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 < HEAP_SIZE; j++)
            {
                int num2 = s.heap[j];
                int i    = array[array[num2 * 2 + 1] * 2 + 1] + 1;
                if (i > max_length)
                {
                    i = max_length;
                    num++;
                }
                array[num2 * 2 + 1] = (short)i;
                if (num2 <= max_code)
                {
                    s.bl_count[i]++;
                    int num3 = 0;
                    if (num2 >= extra_base)
                    {
                        num3 = extra_bits[num2 - extra_base];
                    }
                    short num4 = array[num2 * 2];
                    s.opt_len += num4 * (i + num3);
                    if (static_tree != null)
                    {
                        s.static_len += num4 * (static_tree[num2 * 2 + 1] + num3);
                    }
                }
            }
            if (num == 0)
            {
                return;
            }
            do
            {
                int i = max_length - 1;
                while (s.bl_count[i] == 0)
                {
                    i--;
                }
                s.bl_count[i]--;
                s.bl_count[i + 1] = (short)(s.bl_count[i + 1] + 2);
                s.bl_count[max_length]--;
                num -= 2;
            }while (num > 0);
            for (int i = max_length; i != 0; i--)
            {
                int num2 = s.bl_count[i];
                while (num2 != 0)
                {
                    int num5 = s.heap[--j];
                    if (num5 <= max_code)
                    {
                        if (array[num5 * 2 + 1] != i)
                        {
                            s.opt_len           = (int)(s.opt_len + ((long)i - (long)array[num5 * 2 + 1]) * array[num5 * 2]);
                            array[num5 * 2 + 1] = (short)i;
                        }
                        num2--;
                    }
                }
            }
        }