Пример #1
0
        public int deflateEnd()
        {
            if (dstate == null)
            {
                return(Z_STREAM_ERROR);
            }
            int ret = dstate.deflateEnd();

            dstate = null;
            return(ret);
        }
Пример #2
0
        public void build_tree(Deflate s)
        {
            short[] tree = dyn_tree;
            short[] stree = stat_desc.static_tree;
            int     elems = stat_desc.elems;
            int     n, m;
            int     max_code = -1;
            int     node;



            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;
                }
            }



            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];
                }
            }
            this.max_code = max_code;



            for (n = s.heap_len / 2; n >= 1; n--)
            {
                s.pqdownheap(tree, n);
            }



            node = elems;
            do
            {
                n         = s.heap[1];
                s.heap[1] = s.heap[s.heap_len--];
                s.pqdownheap(tree, 1);
                m = s.heap[1];

                s.heap[--s.heap_max] = n;
                s.heap[--s.heap_max] = m;


                tree[node * 2]  = (short)(tree[n * 2] + tree[m * 2]);
                s.depth[node]   = (byte)(System.Math.Max(s.depth[n], s.depth[m]) + 1);
                tree[n * 2 + 1] = tree[m * 2 + 1] = (short)node;


                s.heap[1] = node++;
                s.pqdownheap(tree, 1);
            }while(s.heap_len >= 2);

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



            gen_bitlen(s);


            gen_codes(tree, max_code, s.bl_count);
        }
Пример #3
0
 public int deflateInit(int level, int bits, bool nowrap)
 {
     dstate = new Deflate();
     return(dstate.deflateInit(this, level, nowrap?-bits:bits));
 }
Пример #4
0
        public void gen_bitlen(Deflate s)
        {
            short[] tree       = dyn_tree;
            short[] stree      = stat_desc.static_tree;
            int[]   extra      = stat_desc.extra_bits;
            int     based      = stat_desc.extra_base;
            int     max_length = stat_desc.max_length;
            int     h;
            int     n, m;
            int     bits;
            int     xbits;
            short   f;
            int     overflow = 0;

            for (bits = 0; bits <= MAX_BITS; bits++)
            {
                s.bl_count[bits] = 0;
            }



            tree[s.heap[s.heap_max] * 2 + 1] = 0;

            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;


                if (n > max_code)
                {
                    continue;
                }

                s.bl_count[bits]++;
                xbits = 0;
                if (n >= based)
                {
                    xbits = extra[n - based];
                }
                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;
            }



            do
            {
                bits = max_length - 1;
                while (s.bl_count[bits] == 0)
                {
                    bits--;
                }
                s.bl_count[bits]--;
                s.bl_count[bits + 1] += 2;
                s.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)(((long)bits - (long)tree[m * 2 + 1]) * (long)tree[m * 2]);
                        tree[m * 2 + 1] = (short)bits;
                    }
                    n--;
                }
            }
        }