Debian Codegraph @

converter/other/fiasco/output/tree.c

raw
1 /*
2 * tree.c: Output of bintree partitioning
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:31 $
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 "wfa.h"
24 #include "bit-io.h"
25 #include "arith.h"
26 #include "misc.h"
27
28 #include "tree.h"
29
30 /*****************************************************************************
31
32 prototypes
33
34 *****************************************************************************/
35
36 static void
37 encode_tree (bitfile_t *output, const byte_t *data, unsigned n_data,
38 unsigned scaling, u_word_t sum0, u_word_t sum1);
39
40 /*****************************************************************************
41
42 public code
43
44 *****************************************************************************/
45
46 void
47 write_tree (const wfa_t *wfa, bitfile_t *output)
48 /*
49 * Write bintree to stream 'output'.
50 * Traverse tree in breadth first order and save a '1' for each child
51 * and a '0' for each range image.
52 *
53 * No return value.
54 */
55 {
56 unsigned queue [MAXSTATES]; /* state numbers in BFO */
57 unsigned current; /* current node to process */
58 unsigned last; /* last node (update every new node) */
59 unsigned label; /* current label */
60 int into; /* next child */
61 byte_t *tree_string; /* bitstring to encode */
62 unsigned total = 0; /* number of ranges */
63 unsigned bits = bits_processed (output); /* number of bits */
64
65 /*
66 * Traverse tree in breadth first order. Use a queue to store
67 * the children of each node ('last' is the next free queue element).
68 * The first element ('current') of this queue will get the new parent
69 * node.
70 */
71 tree_string = Calloc (MAXSTATES * MAXLABELS, sizeof (byte_t));
72 queue [0] = wfa->root_state;
73 for (last = 1, current = 0; current < last; current++)
74 for (label = 0; label < MAXLABELS; label++)
75 if (!isrange (into = wfa->tree [queue[current]][label])) /* child ? */
76 {
77 queue [last++] = into;
78 tree_string [total++] = 1;
79 }
80 else /* or range ? */
81 tree_string [total++] = 0;
82
83 if (total != (wfa->states - wfa->basis_states) * MAXLABELS)
84 error ("total [%d] != (states - basis_states) * 2 [%d]", total,
85 (wfa->states - wfa->basis_states) * MAXLABELS);
86
87 {
88 unsigned scale = total / 20 ;
89
90 encode_tree (output, tree_string, total, scale, 1, 11);
91 }
92
93 Free (tree_string);
94
95 debug_message ("tree: %5d bits. (%5d symbols => %5.2f bps)",
96 bits_processed (output) - bits, total,
97 total > 0 ? ((bits_processed (output) - bits)
98 / (double) total) : 0);
99 }
100
101 /*****************************************************************************
102
103 private code
104
105 *****************************************************************************/
106
107 static void
108 encode_tree (bitfile_t *output, const byte_t *data, unsigned n_data,
109 unsigned scaling, u_word_t sum0, u_word_t sum1)
110 /*
111 * Encode bintree data with adaptive binary arithmetic coding.
112 * Write 'n_data' output symbols stored in 'data' to stream 'output'.
113 * Rescale probability model after every 'scaling' symbols.
114 * Initial counts are given by 'sum0' and 'sum1'.
115 *
116 * No return value.
117 */
118 {
119 u_word_t low; /* Start of the current code range */
120 u_word_t high; /* End of the current code range */
121 u_word_t underflow; /* Number of underflow bits pending */
122 unsigned n; /* Data counter */
123
124 low = 0;
125 high = 0xffff;
126 underflow = 0;
127
128 for (n = n_data; n; n--)
129 {
130 unsigned range; /* Current interval range */
131
132 if (!*data++)
133 {
134 /*
135 * encode a '0'
136 */
137 range = (high - low) + 1;
138 high = low + (u_word_t) ((range * sum0) / sum1 - 1);
139
140 RESCALE_OUTPUT_INTERVAL;
141
142 sum0++;
143 }
144 else
145 {
146 /*
147 * encode a '1'
148 */
149 range = (high - low) + 1;
150 low = low + (u_word_t) ((range * sum0) / sum1);
151
152 RESCALE_OUTPUT_INTERVAL;
153 }
154 /*
155 * Update the frequency counts
156 */
157 sum1++;
158 if (sum1 > scaling) /* Scale the symbol frequencies */
159 {
160 sum0 >>= 1;
161 sum1 >>= 1;
162 if (!sum0)
163 sum0 = 1;
164 if (sum0 >= sum1)
165 sum1 = sum0 + 1;
166 }
167 }
168 /*
169 * Flush the quasi-arithmetic encoder
170 */
171 low = high;
172
173 RESCALE_OUTPUT_INTERVAL;
174
175 OUTPUT_BYTE_ALIGN (output);
176 }