| 1 |
/* |
| 2 |
* control.c: Control unit of WFA structure |
| 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 <string.h> |
| 20 |
|
| 21 |
#include "types.h" |
| 22 |
#include "macros.h" |
| 23 |
#include "error.h" |
| 24 |
|
| 25 |
#include "cwfa.h" |
| 26 |
#include "ip.h" |
| 27 |
#include "misc.h" |
| 28 |
#include "wfalib.h" |
| 29 |
#include "control.h" |
| 30 |
|
| 31 |
/***************************************************************************** |
| 32 |
|
| 33 |
prototypes |
| 34 |
|
| 35 |
*****************************************************************************/ |
| 36 |
|
| 37 |
static void |
| 38 |
clear_or_alloc (real_t **ptr, size_t size); |
| 39 |
static void |
| 40 |
compute_images (unsigned from, unsigned to, const wfa_t *wfa, coding_t *c); |
| 41 |
|
| 42 |
/***************************************************************************** |
| 43 |
|
| 44 |
public code |
| 45 |
|
| 46 |
*****************************************************************************/ |
| 47 |
|
| 48 |
void |
| 49 |
append_state (bool_t auxiliary_state, real_t final, unsigned level_of_state, |
| 50 |
wfa_t *wfa, coding_t *c) |
| 51 |
/* |
| 52 |
* Append a 'wfa' state. If 'auxiliary_state' == YES then |
| 53 |
* allocate memory for inner products and state images. 'final' is |
| 54 |
* the final distribution of the new state. 'level_of_state' is the |
| 55 |
* level of the subimage which is represented by this state |
| 56 |
* |
| 57 |
* No return value. |
| 58 |
* |
| 59 |
* Side effects: |
| 60 |
* The WFA information are updated in structure 'wfa' |
| 61 |
* State images are computed and inner products are cleared (in 'c') |
| 62 |
*/ |
| 63 |
{ |
| 64 |
wfa->final_distribution [wfa->states] = final; |
| 65 |
wfa->level_of_state [wfa->states] = level_of_state; |
| 66 |
|
| 67 |
if (!auxiliary_state) |
| 68 |
{ |
| 69 |
unsigned level; |
| 70 |
|
| 71 |
wfa->domain_type [wfa->states] = USE_DOMAIN_MASK; |
| 72 |
|
| 73 |
/* |
| 74 |
* Allocate memory for inner products and for state images |
| 75 |
*/ |
| 76 |
clear_or_alloc (&c->images_of_state [wfa->states], |
| 77 |
size_of_tree (c->options.images_level)); |
| 78 |
|
| 79 |
for (level = c->options.images_level + 1; |
| 80 |
level <= c->options.lc_max_level; level++) |
| 81 |
clear_or_alloc (&c->ip_states_state [wfa->states][level], |
| 82 |
wfa->states + 1); |
| 83 |
|
| 84 |
clear_or_alloc (&c->ip_images_state [wfa->states], |
| 85 |
size_of_tree (c->products_level)); |
| 86 |
|
| 87 |
/* |
| 88 |
* Compute the images of the current state at level 0,..,'imageslevel' |
| 89 |
*/ |
| 90 |
|
| 91 |
c->images_of_state [wfa->states][0] = final; |
| 92 |
compute_images (wfa->states, wfa->states, wfa, c); |
| 93 |
|
| 94 |
/* |
| 95 |
* Compute the inner products between the current state and the |
| 96 |
* old states 0,...,'states'-1 |
| 97 |
*/ |
| 98 |
|
| 99 |
compute_ip_states_state (wfa->states, wfa->states, wfa, c); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
unsigned level; |
| 104 |
|
| 105 |
wfa->domain_type [wfa->states] = 0; |
| 106 |
|
| 107 |
/* |
| 108 |
* Free the allocated memory |
| 109 |
*/ |
| 110 |
if (c->images_of_state [wfa->states] != NULL) |
| 111 |
{ |
| 112 |
Free (c->images_of_state [wfa->states]); |
| 113 |
c->images_of_state [wfa->states] = NULL; |
| 114 |
} |
| 115 |
for (level = 0; level <= c->options.lc_max_level; level++) |
| 116 |
if (c->ip_states_state [wfa->states][level]) |
| 117 |
{ |
| 118 |
Free (c->ip_states_state [wfa->states][level]); |
| 119 |
c->ip_states_state [wfa->states][level] = NULL; |
| 120 |
} |
| 121 |
if (c->ip_images_state [wfa->states]) |
| 122 |
{ |
| 123 |
Free (c->ip_images_state [wfa->states]); |
| 124 |
c->ip_images_state [wfa->states] = NULL; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
wfa->states++; |
| 129 |
if (wfa->states >= MAXSTATES) |
| 130 |
error ("Maximum number of states reached!"); |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
append_basis_states (unsigned basis_states, wfa_t *wfa, coding_t *c) |
| 135 |
/* |
| 136 |
* Append the WFA basis states 0, ... , ('basis_states' - 1). |
| 137 |
* |
| 138 |
* No return value. |
| 139 |
* |
| 140 |
* Side effects: |
| 141 |
* The WFA information are updated in structure 'wfa' |
| 142 |
* State images and inner products are computed (in 'c') |
| 143 |
*/ |
| 144 |
{ |
| 145 |
unsigned level, state; |
| 146 |
|
| 147 |
/* |
| 148 |
* Allocate memory |
| 149 |
*/ |
| 150 |
|
| 151 |
for (state = 0; state < basis_states; state++) |
| 152 |
{ |
| 153 |
clear_or_alloc (&c->images_of_state [state], |
| 154 |
size_of_tree (c->options.images_level)); |
| 155 |
|
| 156 |
for (level = c->options.images_level + 1; |
| 157 |
level <= c->options.lc_max_level; level++) |
| 158 |
clear_or_alloc (&c->ip_states_state [state][level], state + 1); |
| 159 |
|
| 160 |
clear_or_alloc (&c->ip_images_state [state], |
| 161 |
size_of_tree (c->products_level)); |
| 162 |
|
| 163 |
c->images_of_state [state][0] = wfa->final_distribution [state]; |
| 164 |
wfa->level_of_state [state] = -1; |
| 165 |
} |
| 166 |
|
| 167 |
compute_images (0, basis_states - 1, wfa, c); |
| 168 |
compute_ip_states_state (0, basis_states - 1, wfa, c); |
| 169 |
wfa->states = basis_states; |
| 170 |
|
| 171 |
if (wfa->states >= MAXSTATES) |
| 172 |
error ("Maximum number of states reached!"); |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
append_transitions (unsigned state, unsigned label, const real_t *weight, |
| 177 |
const word_t *into, wfa_t *wfa) |
| 178 |
/* |
| 179 |
* Append the 'wfa' transitions (given by the arrays 'weight' and 'into') |
| 180 |
* of the range ('state','label'). |
| 181 |
* |
| 182 |
* No return value. |
| 183 |
* |
| 184 |
* Side effects: |
| 185 |
* new 'wfa' edges are appended |
| 186 |
*/ |
| 187 |
{ |
| 188 |
unsigned edge; |
| 189 |
|
| 190 |
wfa->y_column [state][label] = 0; |
| 191 |
for (edge = 0; isedge (into [edge]); edge++) |
| 192 |
{ |
| 193 |
append_edge (state, into [edge], weight [edge], label, wfa); |
| 194 |
if (into [edge] == wfa->y_state [state][label]) |
| 195 |
wfa->y_column [state][label] = 1; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/***************************************************************************** |
| 200 |
|
| 201 |
private code |
| 202 |
|
| 203 |
*****************************************************************************/ |
| 204 |
|
| 205 |
static void |
| 206 |
compute_images (unsigned from, unsigned to, const wfa_t *wfa, coding_t *c) |
| 207 |
/* |
| 208 |
* Computes the images of the given states 'from', ... , 'to' |
| 209 |
* at level 0,...,'c->imagelevel'. |
| 210 |
* Uses the fact that each state image is a linear combination of state |
| 211 |
* images, i.e. s_i := c_0 s_0 + ... + c_i s_i. |
| 212 |
*/ |
| 213 |
{ |
| 214 |
unsigned label, level, state; |
| 215 |
|
| 216 |
/* |
| 217 |
* Compute the images Phi(state) |
| 218 |
* # level = 0 |
| 219 |
* ## level = 1 |
| 220 |
* #### level = 2 |
| 221 |
* ######## level = 3 |
| 222 |
* ... |
| 223 |
* ########...## level = imageslevel |
| 224 |
*/ |
| 225 |
|
| 226 |
for (level = 1; level <= c->options.images_level; level++) |
| 227 |
for (state = from; state <= to; state++) |
| 228 |
for (label = 0; label < MAXLABELS; label++) |
| 229 |
{ |
| 230 |
real_t *dst, *src; |
| 231 |
unsigned edge; |
| 232 |
int domain; /* current domain */ |
| 233 |
|
| 234 |
if (ischild (domain = wfa->tree[state][label])) |
| 235 |
{ |
| 236 |
dst = c->images_of_state [state] + address_of_level (level) + |
| 237 |
label * size_of_level (level - 1); |
| 238 |
src = c->images_of_state [domain] |
| 239 |
+ address_of_level (level - 1); |
| 240 |
memcpy (dst, src, size_of_level (level - 1) * sizeof (real_t)); |
| 241 |
} |
| 242 |
for (edge = 0; isedge (domain = wfa->into[state][label][edge]); |
| 243 |
edge++) |
| 244 |
{ |
| 245 |
unsigned n; |
| 246 |
real_t weight = wfa->weight [state][label][edge]; |
| 247 |
|
| 248 |
dst = c->images_of_state [state] + address_of_level (level) + |
| 249 |
label * size_of_level (level - 1); |
| 250 |
src = c->images_of_state [domain] |
| 251 |
+ address_of_level (level - 1); |
| 252 |
|
| 253 |
for (n = size_of_level (level - 1); n; n--) |
| 254 |
*dst++ += *src++ * weight; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
clear_or_alloc (real_t **ptr, size_t size) |
| 262 |
/* |
| 263 |
* if *ptr == NULL allocate memory with Calloc |
| 264 |
* otherwise fill the real_t-array ptr[] with 0 |
| 265 |
*/ |
| 266 |
{ |
| 267 |
if (*ptr == NULL) |
| 268 |
*ptr = Calloc (size, sizeof (real_t)); |
| 269 |
else |
| 270 |
memset (*ptr, 0, size * sizeof (real_t)); |
| 271 |
|
| 272 |
} |