* Generate the codes for a given tree and bit counts (which need not be * optimal). * IN assertion: the array bl_count contains the bit length statistics for * the given tree and the field len is set for all tree elements. * OUT assertion: the field code is set for all tree elements of non * zero code length. */
| 352 | * zero code length. |
| 353 | */ |
| 354 | void CodeTree::gen_codes (ct_data *tree, int max_code) |
| 355 | { |
| 356 | word16 next_code[MAX_BITS+1]; /* next code value for each bit length */ |
| 357 | word16 code = 0; /* running code value */ |
| 358 | unsigned int bits; /* bit index */ |
| 359 | int n; /* code index */ |
| 360 | |
| 361 | /* The distribution counts are first used to generate the code values |
| 362 | * without bit reversal. |
| 363 | */ |
| 364 | for (bits = 1; bits <= MAX_BITS; bits++) { |
| 365 | next_code[bits] = code = (code + bl_count[bits-1]) << 1; |
| 366 | } |
| 367 | /* Check that the bit counts in bl_count are consistent. The last code |
| 368 | * must be all ones. |
| 369 | */ |
| 370 | assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1); |
| 371 | // Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); |
| 372 | |
| 373 | for (n = 0; n <= max_code; n++) { |
| 374 | int len = tree[n].Len; |
| 375 | if (len == 0) continue; |
| 376 | /* Now reverse the bits */ |
| 377 | tree[n].Code = reverse(next_code[len]++, len); |
| 378 | |
| 379 | // Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", |
| 380 | // n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Construct one Huffman tree and assigns the code bit strings and lengths. |