/// <summary> /// 初始化 deflation 状态。 /// </summary> /// <param name="wantRfc1950Header">是否使用 Rfc1950 文件头。</param> /// <returns>如果正常返回 ZlibState.Success 。</returns> private ZlibState InitializeDeflateInternal(bool wantRfc1950Header) { Thrower.ThrowZlibExceptionIf(IState != null, "在 InitializeInflate() 后不能调用 InitializeDeflate() 。"); DState = new DeflateManager(); DState.WantRfc1950HeaderBytes = wantRfc1950Header; return(DState.Initialize(this, CompressLevel, WindowBits, Strategy)); }
public int EndDeflate() { if (this.dstate == null) { throw new ZlibException("No Deflate State!"); } this.dstate = null; return(0); }
/// <summary> /// End a deflation session. /// </summary> /// <remarks> /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed. /// </remarks> /// <returns>Z_OK if all goes well.</returns> public int EndDeflate() { if (dstate == null) { throw new ZlibException("No Deflate State!"); } //int ret = dstate.End(); dstate = null; return(ZlibConstants.Z_OK); //ret; }
private int _InternalInitializeDeflate(bool wantRfc1950Header) { if (this.istate != null) { throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate()."); } this.dstate = new DeflateManager(); this.dstate.WantRfc1950HeaderBytes = wantRfc1950Header; return(this.dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy)); }
/// <summary> /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, /// the specified number of window bits, and the explicit flag governing whether to emit an RFC1950 header byte pair. /// </summary> /// <param name="level">The compression level for the codec.</param> /// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param> /// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param> /// <returns>Z_OK if all goes well.</returns> public int InitializeDeflate(CompressionLevel level, int bits, bool wantRfc1950Header) { if (istate != null) { throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate()."); } dstate = new DeflateManager(); dstate.WantRfc1950HeaderBytes = wantRfc1950Header; return(dstate.Initialize(this, level, bits)); }
/// <summary> /// End a deflation session. /// </summary> /// <remarks> /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed. /// </remarks> /// <returns>Z_OK if all goes well.</returns> public int EndDeflate() { if (this.dstate == null) { throw new ZlibException("No Deflate State!"); } this.dstate = null; return(ZlibConstants.Z_OK); // ret; }
/// <summary> /// End a deflation session. /// </summary> /// <remarks> /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed. /// </remarks> /// <returns>Z_OK if all goes well.</returns> public int EndDeflate() { if (dstate == null) { throw new ZlibException("No Deflate State!"); } // TODO: dinoch Tue, 03 Nov 2009 15:39 (test this) //int ret = dstate.End(); dstate = null; return(ZlibConstants.Z_OK); //ret; }
/// <summary> /// End a deflation session. /// </summary> /// <remarks> /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed. /// </remarks> /// <returns>Z_OK if all goes well.</returns> public int EndDeflate() { if (dstate == null) { throw new ZlibException("No Deflate State!"); } int ret = dstate.End(); dstate = null; return(ret); }
private int InternalInitializeDeflate(bool wantRfc1950Header) { if (istate != null) { throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate()."); } dstate = new DeflateManager { WantRfc1950HeaderBytes = wantRfc1950Header }; return(dstate.Initialize(this, CompressLevel, WindowBits, Strategy)); }
internal void BuildTree(DeflateManager s) { short[] tree = this.DynTree; short[] stree = this.StaticTree.TreeCodes; int elems = this.StaticTree.Elems; int n; // iterate over heap elements int maxCode = -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.HeapLen = 0; s.HeapMax = HeapSize; for (n = 0; n < elems; n++) { if (tree[n * 2] != 0) { s.Heap[++s.HeapLen] = maxCode = 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.HeapLen < 2) { node = s.Heap[++s.HeapLen] = maxCode < 2 ? ++maxCode : 0; tree[node * 2] = 1; s.Depth[node] = 0; s.OptLen--; if (stree != null) { s.StaticLen -= stree[node * 2 + 1]; } // node is 0 or 1 so it does not have extra bits } this.MaxCode = maxCode; // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, establish sub-heaps of increasing // lengths: for (n = s.HeapLen / 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.HeapLen--]; s.Pqdownheap(tree, 1); int m = s.Heap[1]; // iterate over heap elements s.Heap[--s.HeapMax] = n; // keep the nodes sorted by frequency s.Heap[--s.HeapMax] = m; // Create a new node father of n and m tree[node * 2] = unchecked((short)(tree[n * 2] + tree[m * 2])); s.Depth[node] = (sbyte)(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.HeapLen >= 2); s.Heap[--s.HeapMax] = s.Heap[1]; // At this point, the fields freq and dad are set. We can now generate the bit lengths. this.GenBitlen(s); // The field len is now set, we can generate the bit codes GenCodes(tree, maxCode, s.BlCount); }
internal void BuildTree(DeflateManager s) { short[] tree = this.DynTree; short[] stree = this.StaticTree.TreeCodes; int elems = this.StaticTree.Elems; int n; // iterate over heap elements int maxCode = -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.HeapLen = 0; s.HeapMax = HeapSize; for (n = 0; n < elems; n++) { if (tree[n * 2] != 0) { s.Heap[++s.HeapLen] = maxCode = 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.HeapLen < 2) { node = s.Heap[++s.HeapLen] = maxCode < 2 ? ++maxCode : 0; tree[node * 2] = 1; s.Depth[node] = 0; s.OptLen--; if (stree != null) { s.StaticLen -= stree[node * 2 + 1]; } // node is 0 or 1 so it does not have extra bits } this.MaxCode = maxCode; // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, establish sub-heaps of increasing // lengths: for (n = s.HeapLen / 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.HeapLen--]; s.Pqdownheap(tree, 1); int m = s.Heap[1]; // iterate over heap elements s.Heap[--s.HeapMax] = n; // keep the nodes sorted by frequency s.Heap[--s.HeapMax] = m; // Create a new node father of n and m tree[node * 2] = unchecked ((short)(tree[n * 2] + tree[m * 2])); s.Depth[node] = (sbyte)(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.HeapLen >= 2); s.Heap[--s.HeapMax] = s.Heap[1]; // At this point, the fields freq and dad are set. We can now generate the bit lengths. this.GenBitlen(s); // The field len is now set, we can generate the bit codes GenCodes(tree, maxCode, s.BlCount); }
internal StaticTree staticTree; // 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(DeflateManager s) { short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; int[] extra = staticTree.extraBits; int base_Renamed = staticTree.extraBase; int max_length = staticTree.maxLength; 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 <= InternalConstants.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--; } } }
/// <summary>End a deflation session.</summary> /// <remarks>Call this after making a series of one or more calls to Deflate(). All buffers are flushed.</remarks> /// <returns>Z_OK if all goes well.</returns> internal int EndDeflate() { if (this.Dstate == null) { throw new ZlibException("No Deflate State!"); } // TODO: dinoch Tue, 03 Nov 2009 15:39 (test this) // int ret = dstate.End(); this.Dstate = null; return ZlibConstants.Zok; // ret; }
/// <summary> /// 结束 deflation 状态。 /// </summary> /// <remarks> /// 在 Deflate() 后调用。 /// </remarks> /// <returns>如果正常返回 ZlibState.Success 。</returns> public ZlibState EndDeflate() { Thrower.ThrowZlibExceptionIf(DState == null, "没有初始化 Deflate 状态。"); DState = null; return(ZlibState.Success); }
// 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(DeflateManager s) { short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; int elems = staticTree.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] = unchecked((short) (tree[n * 2] + tree[m * 2])); s.depth[node] = (sbyte) (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); }
// 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(DeflateManager s) { short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; int[] extra = staticTree.extraBits; int base_Renamed = staticTree.extraBase; int max_length = staticTree.maxLength; 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 <= InternalConstants.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--; } } }
private int _InternalInitializeDeflate(bool wantRfc1950Header) { if (istate != null) throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate()."); dstate = new DeflateManager(); dstate.WantRfc1950HeaderBytes = wantRfc1950Header; return dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy); }
/// <summary> /// End a deflation session. /// </summary> /// <remarks> /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed. /// </remarks> /// <returns>Z_OK if all goes well.</returns> public int EndDeflate() { if (dstate == null) throw new ZlibException("No Deflate State!"); // TODO: dinoch Tue, 03 Nov 2009 15:39 (test this) //int ret = dstate.End(); dstate = null; return ZlibConstants.Z_OK; //ret; }
/// <param name="s"> /// </param> private void GenBitlen(DeflateManager s) { short[] tree = this.DynTree; short[] stree = this.StaticTree.TreeCodes; int[] extra = this.StaticTree.ExtraBits; int baseRenamed = this.StaticTree.ExtraBase; int maxLength = this.StaticTree.MaxLength; int h; // heap index int n; // iterate over the tree elements int bits; // bit length int overflow = 0; // number of elements with bit length too large for (bits = 0; bits <= InternalConstants.MaxBits; bits++) { s.BlCount[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.HeapMax] * 2 + 1] = 0; // root of the heap for (h = s.HeapMax + 1; h < HeapSize; h++) { n = s.Heap[h]; bits = tree[tree[n * 2 + 1] * 2 + 1] + 1; if (bits > maxLength) { bits = maxLength; overflow++; } tree[n * 2 + 1] = (short)bits; // We overwrite tree[n*2+1] which is no longer needed if (n > this.MaxCode) { continue; // not a leaf node } s.BlCount[bits]++; int xbits = 0; // extra bits if (n >= baseRenamed) { xbits = extra[n - baseRenamed]; } short f = tree[n * 2]; // frequency s.OptLen += f * (bits + xbits); if (stree != null) { s.StaticLen += 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 = maxLength - 1; while (s.BlCount[bits] == 0) { bits--; } s.BlCount[bits]--; // move one leaf down the tree s.BlCount[bits + 1] = (short)(s.BlCount[bits + 1] + 2); // move one overflow item as its brother s.BlCount[maxLength]--; // 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 = maxLength; bits != 0; bits--) { n = s.BlCount[bits]; while (n != 0) { int m = s.Heap[--h]; // iterate over the tree elements if (m > this.MaxCode) { continue; } if (tree[m * 2 + 1] != bits) { s.OptLen = (int)(s.OptLen + (bits - (long)tree[m * 2 + 1]) * tree[m * 2]); tree[m * 2 + 1] = (short)bits; } n--; } } }
// 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(DeflateManager s) { short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; int elems = staticTree.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] = unchecked ((short)(tree[n * 2] + tree[m * 2])); s.depth[node] = (sbyte)(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); }
/// <param name="s"> /// </param> private void GenBitlen(DeflateManager s) { short[] tree = this.DynTree; short[] stree = this.StaticTree.TreeCodes; int[] extra = this.StaticTree.ExtraBits; int baseRenamed = this.StaticTree.ExtraBase; int maxLength = this.StaticTree.MaxLength; int h; // heap index int n; // iterate over the tree elements int bits; // bit length int overflow = 0; // number of elements with bit length too large for (bits = 0; bits <= InternalConstants.MaxBits; bits++) { s.BlCount[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.HeapMax] * 2 + 1] = 0; // root of the heap for (h = s.HeapMax + 1; h < HeapSize; h++) { n = s.Heap[h]; bits = tree[tree[n * 2 + 1] * 2 + 1] + 1; if (bits > maxLength) { bits = maxLength; overflow++; } tree[n * 2 + 1] = (short)bits; // We overwrite tree[n*2+1] which is no longer needed if (n > this.MaxCode) { continue; // not a leaf node } s.BlCount[bits]++; int xbits = 0; // extra bits if (n >= baseRenamed) { xbits = extra[n - baseRenamed]; } short f = tree[n * 2]; // frequency s.OptLen += f * (bits + xbits); if (stree != null) { s.StaticLen += 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 = maxLength - 1; while (s.BlCount[bits] == 0) { bits--; } s.BlCount[bits]--; // move one leaf down the tree s.BlCount[bits + 1] = (short)(s.BlCount[bits + 1] + 2); // move one overflow item as its brother s.BlCount[maxLength]--; // 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 = maxLength; bits != 0; bits--) { n = s.BlCount[bits]; while (n != 0) { int m = s.Heap[--h]; // iterate over the tree elements if (m > this.MaxCode) { continue; } if (tree[m * 2 + 1] != bits) { s.OptLen = (int)(s.OptLen + (bits - (long)tree[m * 2 + 1]) * tree[m * 2]); tree[m * 2 + 1] = (short)bits; } n--; } } }