function gen_bitlen(s, desc)
	//    deflate_state *s;
	//    tree_desc *desc;    /* the tree descriptor */
	{
	  var tree            = desc.dyn_tree;
	  var max_code        = desc.max_code;
	  var stree           = desc.stat_desc.static_tree;
	  var has_stree       = desc.stat_desc.has_stree;
	  var extra           = desc.stat_desc.extra_bits;
	  var base            = desc.stat_desc.extra_base;
	  var max_length      = desc.stat_desc.max_length;
	  var h;              /* heap index */
	  var n, m;           /* iterate over the tree elements */
	  var bits;           /* bit length */
	  var xbits;          /* extra bits */
	  var f;              /* frequency */
	  var 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]/*.Len*/ = 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]/*.Dad*/ * 2 + 1]/*.Len*/ + 1;
	    if (bits > max_length) {
	      bits = max_length;
	      overflow++;
	    }
	    tree[n*2 + 1]/*.Len*/ = bits;
	    /* We overwrite tree[n].Dad which is no longer needed */
	
	    if (n > max_code) { continue; } /* not a leaf node */
	
	    s.bl_count[bits]++;
	    xbits = 0;
	    if (n >= base) {
	      xbits = extra[n-base];
	    }
	    f = tree[n * 2]/*.Freq*/;
	    s.opt_len += f * (bits + xbits);
	    if (has_stree) {
	      s.static_len += f * (stree[n*2 + 1]/*.Len*/ + xbits);
	    }
	  }
	  if (overflow === 0) { return; }
	
	  // Trace((stderr,"\nbit length overflow\n"));
	  /* 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] += 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);
	
	  /* Now recompute all bit lengths, scanning in increasing frequency.
	   * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
	   * lengths instead of fixing only the wrong ones. This idea is taken
	   * from 'ar' written by Haruhiko Okumura.)
	   */
	  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]/*.Len*/ !== bits) {
	        // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
	        s.opt_len += (bits - tree[m*2 + 1]/*.Len*/)*tree[m*2]/*.Freq*/;
	        tree[m*2 + 1]/*.Len*/ = bits;
	      }
	      n--;
	    }
	  }
	}
/* ===========================================================================
	 * 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.
	 */
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.
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 .
function gen_bitlen(s, desc)
	{
	  var tree            = desc.dyn_tree;
	  var max_code        = desc.max_code;
	  var stree           = desc.stat_desc.static_tree;
	  var has_stree       = desc.stat_desc.has_stree;
	  var extra           = desc.stat_desc.extra_bits;
	  var base            = desc.stat_desc.extra_base;
	  var max_length      = desc.stat_desc.max_length;
	  var h;              
	  var n, m;           
	  var bits;           
	  var xbits;          
	  var f;              
	  var 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] = bits;
	    if (n > max_code) { continue; } 
	    s.bl_count[bits]++;
	    xbits = 0;
	    if (n >= base) {
	      xbits = extra[n-base];
	    }
	    f = tree[n * 2];
	    s.opt_len += f * (bits + xbits);
	    if (has_stree) {
	      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 += (bits - tree[m*2 + 1])*tree[m*2];
	        tree[m*2 + 1] = bits;
	      }
	      n--;
	    }
	  }
	}
function gen_bitlen ( s , desc ) { var tree = desc . dyn_tree ; var max_code = desc . max_code ; var stree = desc . stat_desc . static_tree ; var has_stree = desc . stat_desc . has_stree ; var extra = desc . stat_desc . extra_bits ; var base = desc . stat_desc . extra_base ; var max_length = desc . stat_desc . max_length ; var h ; var n , m ; var bits ; var xbits ; var f ; var 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 ] = bits ; if ( n > max_code ) { continue ; } s . bl_count [ bits ] ++ ; xbits = 0 ; if ( n >= base ) { xbits = extra [ n - base ] ; } f = tree [ n * 2 ] ; s . opt_len += f * ( bits + xbits ) ; if ( has_stree ) { 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 += ( bits - tree [ m * 2 + 1 ] ) * tree [ m * 2 ] ; tree [ m * 2 + 1 ] = bits ; } n -- ; } } }
Compute the optimal bit lengths for a tree and update the total bit length
 for the current block.
Compute the optimal bit lengths for a tree and update the total bit length for the current block .
//    deflate_state *s; //    tree_desc *desc;    /* the tree descriptor */ /* heap index */ /* iterate over the tree elements */ /* bit length */ /* extra bits */ /* frequency */ /* number of elements with bit length too large */ /* In a first pass, compute the optimal bit lengths (which may
	   * overflow in the case of the bit length tree).
	   */ /*.Len*/ /* root of the heap */ /*.Dad*/ /*.Len*/ /*.Len*/ /* We overwrite tree[n].Dad which is no longer needed */ /* not a leaf node */ /*.Freq*/ /*.Len*/ // Trace((stderr,"\nbit length overflow\n")); /* This happens for example on obj2 and pic of the Calgary corpus */ /* Find the first bit length which could increase: */ /* move one leaf down the tree */ /* move one overflow item as its brother */ /* The brother of the overflow item also moves one step up,
	     * but this does not affect bl_count[max_length]
	     */ /* Now recompute all bit lengths, scanning in increasing frequency.
	   * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
	   * lengths instead of fixing only the wrong ones. This idea is taken
	   * from 'ar' written by Haruhiko Okumura.)
	   */ /*.Len*/ // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); /*.Len*/ /*.Freq*/ /*.Len*/