Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/Tree.java
8650 views
/* -*-mode:java; c-basic-offset:2; -*- */1/*2Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28/*29* This program is based on zlib-1.1.3, so all credit should go authors30* Jean-loup Gailly([email protected]) and Mark Adler([email protected])31* and contributors of zlib.32*/3334package com.jcraft.jzlib;3536final class Tree{37static final private int MAX_BITS=15;38static final private int BL_CODES=19;39static final private int D_CODES=30;40static final private int LITERALS=256;41static final private int LENGTH_CODES=29;42static final private int L_CODES=(LITERALS+1+LENGTH_CODES);43static final private int HEAP_SIZE=(2*L_CODES+1);4445// Bit length codes must not exceed MAX_BL_BITS bits46static final int MAX_BL_BITS=7;4748// end of block literal code49static final int END_BLOCK=256;5051// repeat previous bit length 3-6 times (2 bits of repeat count)52static final int REP_3_6=16;5354// repeat a zero length 3-10 times (3 bits of repeat count)55static final int REPZ_3_10=17;5657// repeat a zero length 11-138 times (7 bits of repeat count)58static final int REPZ_11_138=18;5960// extra bits for each length code61static final int[] extra_lbits={620,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,063};6465// extra bits for each distance code66static final int[] extra_dbits={670,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,1368};6970// extra bits for each bit length code71static final int[] extra_blbits={720,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,773};7475static final byte[] bl_order={7616,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};777879// The lengths of the bit length codes are sent in order of decreasing80// probability, to avoid transmitting the lengths for unused bit81// length codes.8283static final int Buf_size=8*2;8485// see definition of array dist_code below86static final int DIST_CODE_LEN=512;8788static final byte[] _dist_code = {890, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,908, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,9110, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,9211, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,9312, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,9413, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,9513, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,9614, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,9714, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,9814, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,9915, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,10015, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,10115, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,10218, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,10323, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,10424, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,10526, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,10626, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,10727, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,10827, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,10928, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,11028, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,11128, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,11229, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,11329, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,11429, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29115};116117static final byte[] _length_code={1180, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,11913, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,12017, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,12119, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,12221, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,12322, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,12423, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,12524, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,12625, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,12725, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,12826, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,12926, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,13027, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28131};132133static final int[] base_length = {1340, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,13564, 80, 96, 112, 128, 160, 192, 224, 0136};137138static final int[] base_dist = {1390, 1, 2, 3, 4, 6, 8, 12, 16, 24,14032, 48, 64, 96, 128, 192, 256, 384, 512, 768,1411024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576142};143144// Mapping from a distance to a distance code. dist is the distance - 1 and145// must not have side effects. _dist_code[256] and _dist_code[257] are never146// used.147static int d_code(int dist){148return ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>>7)]);149}150151short[] dyn_tree; // the dynamic tree152int max_code; // largest code with non zero frequency153StaticTree stat_desc; // the corresponding static tree154155// Compute the optimal bit lengths for a tree and update the total bit length156// for the current block.157// IN assertion: the fields freq and dad are set, heap[heap_max] and158// above are the tree nodes sorted by increasing frequency.159// OUT assertions: the field len is set to the optimal bit length, the160// array bl_count contains the frequencies for each bit length.161// The length opt_len is updated; static_len is also updated if stree is162// not null.163void gen_bitlen(Deflate s){164short[] tree = dyn_tree;165short[] stree = stat_desc.static_tree;166int[] extra = stat_desc.extra_bits;167int base = stat_desc.extra_base;168int max_length = stat_desc.max_length;169int h; // heap index170int n, m; // iterate over the tree elements171int bits; // bit length172int xbits; // extra bits173short f; // frequency174int overflow = 0; // number of elements with bit length too large175176for (bits = 0; bits <= MAX_BITS; bits++) s.bl_count[bits] = 0;177178// In a first pass, compute the optimal bit lengths (which may179// overflow in the case of the bit length tree).180tree[s.heap[s.heap_max]*2+1] = 0; // root of the heap181182for(h=s.heap_max+1; h<HEAP_SIZE; h++){183n = s.heap[h];184bits = tree[tree[n*2+1]*2+1] + 1;185if (bits > max_length){ bits = max_length; overflow++; }186tree[n*2+1] = (short)bits;187// We overwrite tree[n*2+1] which is no longer needed188189if (n > max_code) continue; // not a leaf node190191s.bl_count[bits]++;192xbits = 0;193if (n >= base) xbits = extra[n-base];194f = tree[n*2];195s.opt_len += f * (bits + xbits);196if (stree!=null) s.static_len += f * (stree[n*2+1] + xbits);197}198if (overflow == 0) return;199200// This happens for example on obj2 and pic of the Calgary corpus201// Find the first bit length which could increase:202do {203bits = max_length-1;204while(s.bl_count[bits]==0) bits--;205s.bl_count[bits]--; // move one leaf down the tree206s.bl_count[bits+1]+=2; // move one overflow item as its brother207s.bl_count[max_length]--;208// The brother of the overflow item also moves one step up,209// but this does not affect bl_count[max_length]210overflow -= 2;211}212while (overflow > 0);213214for (bits = max_length; bits != 0; bits--) {215n = s.bl_count[bits];216while (n != 0) {217m = s.heap[--h];218if (m > max_code) continue;219if (tree[m*2+1] != bits) {220s.opt_len += ((long)bits - (long)tree[m*2+1])*(long)tree[m*2];221tree[m*2+1] = (short)bits;222}223n--;224}225}226}227228// Construct one Huffman tree and assigns the code bit strings and lengths.229// Update the total bit length for the current block.230// IN assertion: the field freq is set for all tree elements.231// OUT assertions: the fields len and code are set to the optimal bit length232// and corresponding code. The length opt_len is updated; static_len is233// also updated if stree is not null. The field max_code is set.234void build_tree(Deflate s){235short[] tree=dyn_tree;236short[] stree=stat_desc.static_tree;237int elems=stat_desc.elems;238int n, m; // iterate over heap elements239int max_code=-1; // largest code with non zero frequency240int node; // new node being created241242// Construct the initial heap, with least frequent element in243// heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].244// heap[0] is not used.245s.heap_len = 0;246s.heap_max = HEAP_SIZE;247248for(n=0; n<elems; n++) {249if(tree[n*2] != 0) {250s.heap[++s.heap_len] = max_code = n;251s.depth[n] = 0;252}253else{254tree[n*2+1] = 0;255}256}257258// The pkzip format requires that at least one distance code exists,259// and that at least one bit should be sent even if there is only one260// possible code. So to avoid special checks later on we force at least261// two codes of non zero frequency.262while (s.heap_len < 2) {263node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);264tree[node*2] = 1;265s.depth[node] = 0;266s.opt_len--; if (stree!=null) s.static_len -= stree[node*2+1];267// node is 0 or 1 so it does not have extra bits268}269this.max_code = max_code;270271// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,272// establish sub-heaps of increasing lengths:273274for(n=s.heap_len/2;n>=1; n--)275s.pqdownheap(tree, n);276277// Construct the Huffman tree by repeatedly combining the least two278// frequent nodes.279280node=elems; // next internal node of the tree281do{282// n = node of least frequency283n=s.heap[1];284s.heap[1]=s.heap[s.heap_len--];285s.pqdownheap(tree, 1);286m=s.heap[1]; // m = node of next least frequency287288s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency289s.heap[--s.heap_max] = m;290291// Create a new node father of n and m292tree[node*2] = (short)(tree[n*2] + tree[m*2]);293s.depth[node] = (byte)(Math.max(s.depth[n],s.depth[m])+1);294tree[n*2+1] = tree[m*2+1] = (short)node;295296// and insert the new node in the heap297s.heap[1] = node++;298s.pqdownheap(tree, 1);299}300while(s.heap_len>=2);301302s.heap[--s.heap_max] = s.heap[1];303304// At this point, the fields freq and dad are set. We can now305// generate the bit lengths.306307gen_bitlen(s);308309// The field len is now set, we can generate the bit codes310gen_codes(tree, max_code, s.bl_count, s.next_code);311}312313// Generate the codes for a given tree and bit counts (which need not be314// optimal).315// IN assertion: the array bl_count contains the bit length statistics for316// the given tree and the field len is set for all tree elements.317// OUT assertion: the field code is set for all tree elements of non318// zero code length.319private final static void gen_codes(320short[] tree, // the tree to decorate321int max_code, // largest code with non zero frequency322short[] bl_count, // number of codes at each bit length323short[] next_code){324short code = 0; // running code value325int bits; // bit index326int n; // code index327328// The distribution counts are first used to generate the code values329// without bit reversal.330next_code[0]=0;331for (bits = 1; bits <= MAX_BITS; bits++) {332next_code[bits] = code = (short)((code + bl_count[bits-1]) << 1);333}334335// Check that the bit counts in bl_count are consistent. The last code336// must be all ones.337//Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,338// "inconsistent bit counts");339//Tracev((stderr,"\ngen_codes: max_code %d ", max_code));340341for (n = 0; n <= max_code; n++) {342int len = tree[n*2+1];343if (len == 0) continue;344// Now reverse the bits345tree[n*2] = (short)(bi_reverse(next_code[len]++, len));346}347}348349// Reverse the first len bits of a code, using straightforward code (a faster350// method would use a table)351// IN assertion: 1 <= len <= 15352private final static int bi_reverse(353int code, // the value to invert354int len // its bit length355){356int res = 0;357do{358res|=code&1;359code>>>=1;360res<<=1;361}362while(--len>0);363return res>>>1;364}365}366367368369