| 1 |
/* |
| 2 |
* tree.c: Input 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:13 $ |
| 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 "bit-io.h" |
| 24 |
#include "arith.h" |
| 25 |
#include "misc.h" |
| 26 |
#include "wfalib.h" |
| 27 |
#include "tiling.h" |
| 28 |
|
| 29 |
#include "tree.h" |
| 30 |
|
| 31 |
/***************************************************************************** |
| 32 |
|
| 33 |
prototypes |
| 34 |
|
| 35 |
*****************************************************************************/ |
| 36 |
|
| 37 |
static unsigned |
| 38 |
restore_depth_first_order (unsigned src_state, unsigned level, unsigned x, |
| 39 |
unsigned y, unsigned *dst_state, |
| 40 |
word_t (*bfo_tree)[MAXLABELS], |
| 41 |
wfa_t *wfa, tiling_t *tiling); |
| 42 |
static void |
| 43 |
decode_tree (bitfile_t *input, byte_t *data, unsigned n_data, unsigned scaling, |
| 44 |
u_word_t sum0, u_word_t sum1); |
| 45 |
|
| 46 |
/***************************************************************************** |
| 47 |
|
| 48 |
public code |
| 49 |
|
| 50 |
*****************************************************************************/ |
| 51 |
|
| 52 |
void |
| 53 |
read_tree (wfa_t *wfa, tiling_t *tiling, bitfile_t *input) |
| 54 |
/* |
| 55 |
* Read bintree partitioning of WFA from the 'input' stream. |
| 56 |
* 'tiling' provides the information about image tiling, if applied. |
| 57 |
* |
| 58 |
* No return value. |
| 59 |
* |
| 60 |
* Side effects: |
| 61 |
* 'wfa->tree', 'wfa->x', 'wfa->y', 'wfa->level_of_state' |
| 62 |
* are filled with decoded values. |
| 63 |
*/ |
| 64 |
{ |
| 65 |
byte_t *bitstring; /* the encoded data */ |
| 66 |
word_t (*bfo_tree)[MAXLABELS]; /* node numbers in BFO */ |
| 67 |
|
| 68 |
/* |
| 69 |
* Read WFA tree stored in breadth first order |
| 70 |
*/ |
| 71 |
{ |
| 72 |
unsigned total = (wfa->states - wfa->basis_states) * MAXLABELS; |
| 73 |
unsigned scale = total / 20; |
| 74 |
|
| 75 |
bitstring = Calloc (total, sizeof (byte_t)); |
| 76 |
decode_tree (input, bitstring, total, scale, 1, 11); |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* Generate tree using a breadth first traversal |
| 81 |
*/ |
| 82 |
{ |
| 83 |
unsigned next; /* next free node number of the tree */ |
| 84 |
unsigned state; |
| 85 |
unsigned label; |
| 86 |
byte_t *buffer = bitstring; /* pointer to decoded data */ |
| 87 |
|
| 88 |
bfo_tree = Calloc (wfa->states * MAXLABELS, sizeof (word_t)); |
| 89 |
for (state = 0, next = 1; state < next; state++) |
| 90 |
for (label = 0; label < MAXLABELS; label++) |
| 91 |
bfo_tree [state][label] = *buffer++ ? next++ : RANGE; |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* Traverse tree and restore depth first order |
| 96 |
*/ |
| 97 |
{ |
| 98 |
unsigned dst_state = wfa->basis_states; |
| 99 |
|
| 100 |
wfa->root_state |
| 101 |
= restore_depth_first_order (0, (wfa->wfainfo->level |
| 102 |
+ (wfa->wfainfo->color ? 2 : 0)), |
| 103 |
0, 0, &dst_state, bfo_tree, wfa, tiling); |
| 104 |
} |
| 105 |
|
| 106 |
Free (bitstring); |
| 107 |
Free (bfo_tree); |
| 108 |
} |
| 109 |
|
| 110 |
/***************************************************************************** |
| 111 |
|
| 112 |
private code |
| 113 |
|
| 114 |
*****************************************************************************/ |
| 115 |
|
| 116 |
static unsigned |
| 117 |
restore_depth_first_order (unsigned src_state, unsigned level, unsigned x, |
| 118 |
unsigned y, unsigned *dst_state, |
| 119 |
word_t (*bfo_tree)[MAXLABELS], |
| 120 |
wfa_t *wfa, tiling_t *tiling) |
| 121 |
/* |
| 122 |
* Map state 'src_state' (breadth first order) |
| 123 |
* to state '*dst_state' (depth first order) |
| 124 |
* Add a tree edge 'state' --> 'child' with label and weight 1.0 |
| 125 |
* if required. |
| 126 |
* 'x', 'y' give the coordinates of the current state in the 'color' image |
| 127 |
* of size 'image_level'. 'tiling' defines the image partitioning. |
| 128 |
* |
| 129 |
* Return value: |
| 130 |
* new node number in depth first order |
| 131 |
* |
| 132 |
* Side effects: |
| 133 |
* 'wfa->tree', 'wfa->x', 'wfa->y', 'wfa->level_of_state' |
| 134 |
* are filled with decoded values. |
| 135 |
*/ |
| 136 |
{ |
| 137 |
unsigned newx [MAXLABELS]; /* x coordinate of children */ |
| 138 |
unsigned newy [MAXLABELS]; /* y coordinate of children */ |
| 139 |
unsigned x0, y0; /* NW corner of image tile */ |
| 140 |
unsigned width, height; /* size of image tile */ |
| 141 |
|
| 142 |
/* |
| 143 |
* If tiling is performed then replace current coordinates |
| 144 |
*/ |
| 145 |
if (tiling->exponent && level == wfa->wfainfo->level - tiling->exponent) |
| 146 |
{ |
| 147 |
unsigned tile; |
| 148 |
|
| 149 |
for (tile = 0; tile < 1U << tiling->exponent; tile++) |
| 150 |
{ |
| 151 |
locate_subimage (wfa->wfainfo->level, level, tile, |
| 152 |
&x0, &y0, &width, &height); |
| 153 |
if (x0 == x && y0 == y) /* matched ! */ |
| 154 |
{ |
| 155 |
locate_subimage (wfa->wfainfo->level, level, tiling->vorder[tile], |
| 156 |
&x, &y, &width, &height); |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
/* |
| 162 |
* Coordinates of children 0 and 1 |
| 163 |
*/ |
| 164 |
if (wfa->wfainfo->color && level == wfa->wfainfo->level + 1) |
| 165 |
newx[0] = newy[0] = newx[1] = newy[1] = 0; |
| 166 |
else |
| 167 |
{ |
| 168 |
newx[0] = x; |
| 169 |
newy[0] = y; |
| 170 |
newx[1] = level & 1 ? x : x + width_of_level (level - 1); |
| 171 |
newy[1] = level & 1 ? y + height_of_level (level - 1) : y; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* Remap node numbers |
| 176 |
*/ |
| 177 |
{ |
| 178 |
int child [MAXLABELS]; /* children of current node (state) */ |
| 179 |
int domain; /* current domain */ |
| 180 |
unsigned label; |
| 181 |
|
| 182 |
for (label = 0; label < MAXLABELS; label++) |
| 183 |
if (!isrange (domain = bfo_tree [src_state][label])) |
| 184 |
child [label] = restore_depth_first_order (domain, level - 1, |
| 185 |
newx [label], |
| 186 |
newy [label], dst_state, |
| 187 |
bfo_tree, wfa, tiling); |
| 188 |
else |
| 189 |
child [label] = RANGE; |
| 190 |
|
| 191 |
for (label = 0; label < MAXLABELS; label++) |
| 192 |
{ |
| 193 |
wfa->tree [*dst_state][label] = child [label]; |
| 194 |
wfa->x [*dst_state][label] = newx [label]; |
| 195 |
wfa->y [*dst_state][label] = newy [label]; |
| 196 |
} |
| 197 |
wfa->level_of_state [*dst_state] = level; |
| 198 |
} |
| 199 |
|
| 200 |
return (*dst_state)++; |
| 201 |
} |
| 202 |
|
| 203 |
/**************************************************************************** |
| 204 |
|
| 205 |
Binary adaptive arithmetic compression |
| 206 |
|
| 207 |
****************************************************************************/ |
| 208 |
|
| 209 |
static void |
| 210 |
decode_tree (bitfile_t *input, byte_t *data, unsigned n_data, unsigned scaling, |
| 211 |
u_word_t sum0, u_word_t sum1) |
| 212 |
/* |
| 213 |
* Decode bintree partitioning using adaptive binary arithmetic decoding. |
| 214 |
* 'input' input stream, |
| 215 |
* 'data' buffer for decoded szmbols, |
| 216 |
* 'n_data' number of symbols to decode, |
| 217 |
* 'scaling' rescale probability models if range > 'scaling' |
| 218 |
* 'sum0' initial totals of symbol '0' |
| 219 |
* 'sum1' initial totals of symbol '1' |
| 220 |
* |
| 221 |
* No return value. |
| 222 |
* |
| 223 |
* Side effects: |
| 224 |
* 'data []' is filled with the decoded bitstring |
| 225 |
*/ |
| 226 |
{ |
| 227 |
u_word_t code; /* The present input code value */ |
| 228 |
u_word_t low; /* Start of the current code range */ |
| 229 |
u_word_t high; /* End of the current code range */ |
| 230 |
unsigned n; /* Data counter */ |
| 231 |
|
| 232 |
assert (data); |
| 233 |
|
| 234 |
code = get_bits (input, 16); |
| 235 |
low = 0; |
| 236 |
high = 0xffff; |
| 237 |
|
| 238 |
for (n = n_data; n; n--) |
| 239 |
{ |
| 240 |
unsigned count; /* Current interval count */ |
| 241 |
unsigned range; /* Current interval range */ |
| 242 |
|
| 243 |
count = (((code - low) + 1) * sum1 - 1) / ((high - low) + 1); |
| 244 |
if (count < sum0) |
| 245 |
{ |
| 246 |
/* |
| 247 |
* Decode a '0' symbol |
| 248 |
* First, the range is expanded to account for the symbol removal. |
| 249 |
*/ |
| 250 |
range = (high - low) + 1; |
| 251 |
high = low + (u_word_t) ((range * sum0) / sum1 - 1 ); |
| 252 |
|
| 253 |
RESCALE_INPUT_INTERVAL; |
| 254 |
|
| 255 |
*data++ = 0; |
| 256 |
/* |
| 257 |
* Update the frequency counts |
| 258 |
*/ |
| 259 |
sum0++; |
| 260 |
sum1++; |
| 261 |
if (sum1 > scaling) /* scale the symbol frequencies */ |
| 262 |
{ |
| 263 |
sum0 >>= 1; |
| 264 |
sum1 >>= 1; |
| 265 |
if (!sum0) |
| 266 |
sum0 = 1; |
| 267 |
if (sum0 >= sum1) |
| 268 |
sum1 = sum0 + 1; |
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
/* |
| 275 |
* Decode a '1' symbol |
| 276 |
* First, the range is expanded to account for the symbol removal. |
| 277 |
*/ |
| 278 |
range = (high - low) + 1; |
| 279 |
high = low + (u_word_t) ((range * sum1) / sum1 - 1); |
| 280 |
low = low + (u_word_t) ((range * sum0) / sum1); |
| 281 |
|
| 282 |
RESCALE_INPUT_INTERVAL; |
| 283 |
|
| 284 |
*data++ = 1; |
| 285 |
/* |
| 286 |
* Update the frequency counts |
| 287 |
*/ |
| 288 |
sum1++; |
| 289 |
if (sum1 > scaling) /* scale the symbol frequencies */ |
| 290 |
{ |
| 291 |
sum0 >>= 1; |
| 292 |
sum1 >>= 1; |
| 293 |
if (!sum0) |
| 294 |
sum0 = 1; |
| 295 |
if (sum0 >= sum1) |
| 296 |
sum1 = sum0 + 1; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
INPUT_BYTE_ALIGN (input); |
| 301 |
} |
| 302 |
|
| 303 |
|