about summary refs log tree commit diff
path: root/converter/other/fiasco/codec/bintree.c
blob: b050ab634617947882379ad9737a41e8d2cb266a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 *  bintree.c:		Bintree model of WFA tree	
 *
 *  Written by:		Ullrich Hafner
 *		
 *  This file is part of FIASCO (Fractal Image And Sequence COdec)
 *  Copyright (C) 1994-2000 Ullrich Hafner
 */

/*
 *  $Date: 2000/06/14 20:50:51 $
 *  $Author: hafner $
 *  $Revision: 5.1 $
 *  $State: Exp $
 */

#include "config.h"

#include "types.h"
#include "macros.h"
#include "error.h"

#include <math.h>

#include "bintree.h"
#include "misc.h"
#include "cwfa.h"

/*****************************************************************************

				public code
  
*****************************************************************************/

void
tree_update (bool_t child, unsigned level, tree_t *model)
/*
 *  Update tree model of given 'level'.
 *
 *  No return value.
 *
 *  Side effects:
 *	tree model is changed.
 */
{
   if (!child)
      model->total [level]++;
   else
   {
      model->counts [level]++;
      model->total [level]++;
   }
}

real_t
tree_bits (bool_t child, unsigned level, const tree_t *model)
/*
 *  Compute number of bits needed for coding element 'child' of the bintree.
 *  For each 'level' a different context is used.
 *
 *  Return value:
 *	# bits
 */
{
   real_t prob = model->counts [level] / (real_t) model->total [level];

   return child ? - log2 (prob) : - log2 (1 - prob);
}

void
init_tree_model (tree_t *tree_model)
/*
 *  Initialize the model for the tree.
 *  Symbol RANGE is synonymous with the '0' symbol and
 *  symbol NO_RANGE is synonymous with the '1' symbol of the binary coder.
 *  The 'count' array counts the number of NO_RANGE symbols.
 *
 *  No return value.
 */
{
   unsigned level;
   unsigned counts_0 [MAXLEVEL] = {20, 17, 15, 10, 5,  4,  3,
				   2,  1,  1,  1,  1,  1,  1,  1,
				   1,  1,  1,  1 , 1,  1,  1};
   unsigned counts_1 [MAXLEVEL] = {1 , 1,  1,  1,  1,  1,  1,
				   1,  1,  2,  3,  5,  10, 15, 20,
				   25, 30, 35, 60, 60, 60, 60};
   
   for (level = 0; level < MAXLEVEL ; level++) 
   {
      tree_model->counts [level] = counts_1 [level];
      tree_model->total [level]  = counts_0 [level] + counts_1 [level];
   }
}