=========================================================================== * 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. */
(tree, max_code, bl_count)
| 583 | * zero code length. |
| 584 | */ |
| 585 | local void gen_codes(tree, max_code, bl_count) |
| 586 | ct_data* tree; /* the tree to decorate */ |
| 587 | int max_code; /* largest code with non zero frequency */ |
| 588 | ushf* bl_count; /* number of codes at each bit length */ |
| 589 | { |
| 590 | ush next_code[MAX_BITS + 1]; /* next code value for each bit length */ |
| 591 | unsigned code = 0; /* running code value */ |
| 592 | int bits; /* bit index */ |
| 593 | int n; /* code index */ |
| 594 | |
| 595 | /* The distribution counts are first used to generate the code values |
| 596 | * without bit reversal. |
| 597 | */ |
| 598 | for (bits = 1; bits <= MAX_BITS; bits++) |
| 599 | { |
| 600 | code = (code + bl_count[bits - 1]) << 1; |
| 601 | next_code[bits] = (ush)code; |
| 602 | } |
| 603 | /* Check that the bit counts in bl_count are consistent. The last code |
| 604 | * must be all ones. |
| 605 | */ |
| 606 | Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1, |
| 607 | "inconsistent bit counts"); |
| 608 | Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); |
| 609 | |
| 610 | for (n = 0; n <= max_code; n++) |
| 611 | { |
| 612 | int len = tree[n].Len; |
| 613 | if (len == 0) continue; |
| 614 | /* Now reverse the bits */ |
| 615 | tree[n].Code = (ush)bi_reverse(next_code[len]++, len); |
| 616 | |
| 617 | Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", |
| 618 | n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /* =========================================================================== |
| 623 | * Construct one Huffman tree and assigns the code bit strings and lengths. |
no test coverage detected