Beispiel #1
0
        public void BuildTree(Deflater deflater)
        {
            short[] dynamicTree    = this.dynamicTree;
            short[] staticTreeData = this.staticTree.StaticTreeData;
            int     maxElements    = this.staticTree.MaxElements;
            int     maxCode        = -1;

            deflater.heapLen = 0;
            deflater.heapMax = TreeConstants.HeapSize;
            for (int index = 0; index < maxElements; ++index)
            {
                if (dynamicTree[index * 2] != (short)0)
                {
                    deflater.heap[++deflater.heapLen] = maxCode = index;
                    deflater.depth[index]             = (byte)0;
                }
                else
                {
                    dynamicTree[index * 2 + 1] = (short)0;
                }
            }
            while (deflater.heapLen < 2)
            {
                int[] heap   = deflater.heap;
                int   index1 = ++deflater.heapLen;
                int   num1;
                if (maxCode >= 2)
                {
                    num1 = 0;
                }
                else
                {
                    maxCode = num1 = maxCode + 1;
                }
                int num2 = num1;
                heap[index1] = num1;
                int index2 = num2;
                dynamicTree[index2 * 2] = (short)1;
                deflater.depth[index2]  = (byte)0;
                --deflater.opt_len;
                if (staticTreeData != null)
                {
                    deflater.static_len -= (int)staticTreeData[index2 * 2 + 1];
                }
            }
            this.maxCode = maxCode;
            for (int k = deflater.heapLen / 2; k >= 1; --k)
            {
                deflater.RestoreHeapDown(dynamicTree, k);
            }
            int index3 = maxElements;

            do
            {
                int index1 = deflater.heap[1];
                deflater.heap[1] = deflater.heap[deflater.heapLen--];
                deflater.RestoreHeapDown(dynamicTree, 1);
                int index2 = deflater.heap[1];
                deflater.heap[--deflater.heapMax] = index1;
                deflater.heap[--deflater.heapMax] = index2;
                dynamicTree[index3 * 2]           = (short)((int)dynamicTree[index1 * 2] + (int)dynamicTree[index2 * 2]);
                deflater.depth[index3]            = (byte)((uint)Math.Max(deflater.depth[index1], deflater.depth[index2]) + 1U);
                dynamicTree[index1 * 2 + 1]       = dynamicTree[index2 * 2 + 1] = (short)index3;
                deflater.heap[1] = index3++;
                deflater.RestoreHeapDown(dynamicTree, 1);
            }while (deflater.heapLen >= 2);
            deflater.heap[--deflater.heapMax] = deflater.heap[1];
            this.GenerateBitLengths(deflater);
            CompressionTree.GenerateCodes(dynamicTree, maxCode, deflater.bl_count);
        }