rEFIt_UEFI/libeg/lodepng.cpp
/*
Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
numcodes, lengths and maxbitlen must already be filled in correctly. return
value is error.
*/
Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
numcodes, lengths and maxbitlen must already be filled in correctly. return
value is error.
Second step for the ... makeFromLengths and ... makeFromFrequencies functions . numcodes lengths and maxbitlen must already be filled in correctly . return value is error .
static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree) {
  unsigned* blcount;
  unsigned* nextcode;
  unsigned error = 0;
  unsigned bits, n;
  tree->codes = (unsigned*)lodepng_malloc(tree->numcodes * sizeof(unsigned));
  blcount = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
  nextcode = (unsigned*)lodepng_malloc((tree->maxbitlen + 1) * sizeof(unsigned));
  if(!tree->codes || !blcount || !nextcode) error = 83; 
  if(!error) {
    for(bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]];
    for(bits = 1; bits <= tree->maxbitlen; ++bits) {
      nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u;
    }
    for(n = 0; n != tree->numcodes; ++n) {
      if(tree->lengths[n] != 0) {
        tree->codes[n] = nextcode[tree->lengths[n]]++;
        tree->codes[n] &= ((1u << tree->lengths[n]) - 1u);
      }
    }
  }
  lodepng_free(blcount);
  lodepng_free(nextcode);
  if(!error) error = HuffmanTree_makeTable(tree);
  return error;
}
static unsigned HuffmanTree_makeFromLengths2 ( HuffmanTree * tree ) { unsigned * blcount ; unsigned * nextcode ; unsigned error = 0 ; unsigned bits , n ; tree -> codes = ( unsigned * ) lodepng_malloc ( tree -> numcodes * sizeof ( unsigned ) ) ; blcount = ( unsigned * ) lodepng_malloc ( ( tree -> maxbitlen + 1 ) * sizeof ( unsigned ) ) ; nextcode = ( unsigned * ) lodepng_malloc ( ( tree -> maxbitlen + 1 ) * sizeof ( unsigned ) ) ; if ( ! tree -> codes || ! blcount || ! nextcode ) error = 83 ; if ( ! error ) { for ( bits = 0 ; bits != tree -> numcodes ; ++ bits ) ++ blcount [ tree -> lengths [ bits ] ] ; for ( bits = 1 ; bits <= tree -> maxbitlen ; ++ bits ) { nextcode [ bits ] = ( nextcode [ bits - 1 ] + blcount [ bits - 1 ] ) << 1u ; } for ( n = 0 ; n != tree -> numcodes ; ++ n ) { if ( tree -> lengths [ n ] != 0 ) { tree -> codes [ n ] = nextcode [ tree -> lengths [ n ] ] ++ ; tree -> codes [ n ] &= ( ( 1u << tree -> lengths [ n ] ) - 1u ) ; } } } lodepng_free ( blcount ) ; lodepng_free ( nextcode ) ; if ( ! error ) error = HuffmanTree_makeTable ( tree ) ; return error ; }
Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
Second step for the ... makeFromLengths and ... makeFromFrequencies functions .
/*alloc fail*/ //   for(n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0; //already zero /*step 1: count number of instances of each code length*/ /*step 2: generate the nextcode values*/ /*step 3: generate all the codes*/ /*remove superfluous bits from the code*/