Debian Codegraph @

converter/other/fiasco/codec/bintree.c

raw
1 /*
2 * bintree.c: Bintree model of WFA tree
3 *
4 * Written by: Ullrich Hafner
5 *
6 * This file is part of FIASCO (Fractal Image And Sequence COdec)
7 * Copyright (C) 1994-2000 Ullrich Hafner
8 */
9
10 /*
11 * $Date: 2000/06/14 20:50:51 $
12 * $Author: hafner $
13 * $Revision: 5.1 $
14 * $State: Exp $
15 */
16
17 #include "config.h"
18
19 #include "types.h"
20 #include "macros.h"
21 #include "error.h"
22
23 #include <math.h>
24
25 #include "bintree.h"
26 #include "misc.h"
27 #include "cwfa.h"
28
29 /*****************************************************************************
30
31 public code
32
33 *****************************************************************************/
34
35 void
36 tree_update (bool_t child, unsigned level, tree_t *model)
37 /*
38 * Update tree model of given 'level'.
39 *
40 * No return value.
41 *
42 * Side effects:
43 * tree model is changed.
44 */
45 {
46 if (!child)
47 model->total [level]++;
48 else
49 {
50 model->counts [level]++;
51 model->total [level]++;
52 }
53 }
54
55 real_t
56 tree_bits (bool_t child, unsigned level, const tree_t *model)
57 /*
58 * Compute number of bits needed for coding element 'child' of the bintree.
59 * For each 'level' a different context is used.
60 *
61 * Return value:
62 * # bits
63 */
64 {
65 real_t prob = model->counts [level] / (real_t) model->total [level];
66
67 return child ? - log2 (prob) : - log2 (1 - prob);
68 }
69
70 void
71 init_tree_model (tree_t *tree_model)
72 /*
73 * Initialize the model for the tree.
74 * Symbol RANGE is synonymous with the '0' symbol and
75 * symbol NO_RANGE is synonymous with the '1' symbol of the binary coder.
76 * The 'count' array counts the number of NO_RANGE symbols.
77 *
78 * No return value.
79 */
80 {
81 unsigned level;
82 unsigned counts_0 [MAXLEVEL] = {20, 17, 15, 10, 5, 4, 3,
83 2, 1, 1, 1, 1, 1, 1, 1,
84 1, 1, 1, 1 , 1, 1, 1};
85 unsigned counts_1 [MAXLEVEL] = {1 , 1, 1, 1, 1, 1, 1,
86 1, 1, 2, 3, 5, 10, 15, 20,
87 25, 30, 35, 60, 60, 60, 60};
88
89 for (level = 0; level < MAXLEVEL ; level++)
90 {
91 tree_model->counts [level] = counts_1 [level];
92 tree_model->total [level] = counts_0 [level] + counts_1 [level];
93 }
94 }