| 1 |
/* |
| 2 |
* wfalib.c: Library functions both for encoding and decoding |
| 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/07/18 15:57:28 $ |
| 12 |
* $Author: hafner $ |
| 13 |
* $Revision: 5.5 $ |
| 14 |
* $State: Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */ |
| 18 |
#define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */ |
| 19 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "pm_c_util.h" |
| 27 |
|
| 28 |
#include "types.h" |
| 29 |
#include "macros.h" |
| 30 |
#include "error.h" |
| 31 |
|
| 32 |
#include "wfa.h" |
| 33 |
#include "misc.h" |
| 34 |
#include "wfalib.h" |
| 35 |
|
| 36 |
/***************************************************************************** |
| 37 |
|
| 38 |
prototypes |
| 39 |
|
| 40 |
*****************************************************************************/ |
| 41 |
|
| 42 |
static unsigned |
| 43 |
xy_to_address (unsigned x, unsigned y, unsigned level, unsigned n); |
| 44 |
|
| 45 |
/***************************************************************************** |
| 46 |
|
| 47 |
public code |
| 48 |
|
| 49 |
*****************************************************************************/ |
| 50 |
|
| 51 |
wfa_t * |
| 52 |
alloc_wfa (bool_t coding) |
| 53 |
/* |
| 54 |
* WFA constructor: |
| 55 |
* Initialize the WFA structure 'wfa' and allocate memory. |
| 56 |
* Flag 'coding' indicates whether WFA is used for coding or decoding. |
| 57 |
* |
| 58 |
* Return value: |
| 59 |
* pointer to the new WFA structure |
| 60 |
*/ |
| 61 |
{ |
| 62 |
wfa_t *wfa = Calloc (1, sizeof (wfa_t)); |
| 63 |
|
| 64 |
/* |
| 65 |
* Allocate memory |
| 66 |
*/ |
| 67 |
wfa->final_distribution = Calloc (MAXSTATES, sizeof (real_t)); |
| 68 |
wfa->level_of_state = Calloc (MAXSTATES, sizeof (byte_t)); |
| 69 |
wfa->domain_type = Calloc (MAXSTATES, sizeof (byte_t)); |
| 70 |
wfa->delta_state = Calloc (MAXSTATES, sizeof (bool_t)); |
| 71 |
wfa->tree = Calloc (MAXSTATES * MAXLABELS, sizeof (word_t)); |
| 72 |
wfa->x = Calloc (MAXSTATES * MAXLABELS, sizeof (word_t)); |
| 73 |
wfa->y = Calloc (MAXSTATES * MAXLABELS, sizeof (word_t)); |
| 74 |
wfa->mv_tree = Calloc (MAXSTATES * MAXLABELS, sizeof (mv_t)); |
| 75 |
wfa->y_state = Calloc (MAXSTATES * MAXLABELS, sizeof (word_t)); |
| 76 |
wfa->into = Calloc (MAXSTATES * MAXLABELS * (MAXEDGES + 1), |
| 77 |
sizeof (word_t)); |
| 78 |
wfa->weight = Calloc (MAXSTATES * MAXLABELS * (MAXEDGES + 1), |
| 79 |
sizeof (real_t)); |
| 80 |
wfa->int_weight = Calloc (MAXSTATES * MAXLABELS * (MAXEDGES + 1), |
| 81 |
sizeof (word_t)); |
| 82 |
wfa->wfainfo = Calloc (1, sizeof (wfa_info_t));; |
| 83 |
wfa->prediction = Calloc (MAXSTATES * MAXLABELS, sizeof (byte_t)); |
| 84 |
|
| 85 |
wfa->wfainfo->wfa_name = NULL; |
| 86 |
wfa->wfainfo->basis_name = NULL; |
| 87 |
wfa->wfainfo->title = strdup (""); |
| 88 |
wfa->wfainfo->comment = strdup (""); |
| 89 |
|
| 90 |
/* |
| 91 |
* Initialize structure |
| 92 |
*/ |
| 93 |
{ |
| 94 |
unsigned state, label; |
| 95 |
|
| 96 |
wfa->states = 0; |
| 97 |
wfa->basis_states = 0; |
| 98 |
wfa->root_state = 0; |
| 99 |
for (state = 0; state < MAXSTATES; state++) |
| 100 |
{ |
| 101 |
wfa->final_distribution [state] = 0; |
| 102 |
wfa->domain_type [state] = 0; |
| 103 |
for (label = 0; label < MAXLABELS; label++) |
| 104 |
{ |
| 105 |
wfa->into [state][label][0] = NO_EDGE; |
| 106 |
wfa->tree [state][label] = RANGE; |
| 107 |
wfa->y_state [state][label] = RANGE; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (coding) /* initialize additional variables */ |
| 113 |
wfa->y_column = Calloc (MAXSTATES * MAXLABELS, sizeof (byte_t)); |
| 114 |
else |
| 115 |
wfa->y_column = NULL; |
| 116 |
|
| 117 |
return wfa; |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
free_wfa (wfa_t *wfa) |
| 122 |
/* |
| 123 |
* WFA destructor: |
| 124 |
* Free memory of given 'wfa'. |
| 125 |
* |
| 126 |
* No return value. |
| 127 |
* |
| 128 |
* Side effects: |
| 129 |
* 'wfa' struct is discarded. |
| 130 |
*/ |
| 131 |
{ |
| 132 |
if (wfa->wfainfo->wfa_name) |
| 133 |
Free (wfa->wfainfo->wfa_name); |
| 134 |
if (wfa->wfainfo->basis_name) |
| 135 |
Free (wfa->wfainfo->basis_name); |
| 136 |
if (wfa->wfainfo->title) |
| 137 |
Free (wfa->wfainfo->title); |
| 138 |
if (wfa->wfainfo->comment) |
| 139 |
Free (wfa->wfainfo->comment); |
| 140 |
|
| 141 |
Free (wfa->final_distribution); |
| 142 |
Free (wfa->level_of_state); |
| 143 |
Free (wfa->domain_type); |
| 144 |
Free (wfa->tree); |
| 145 |
Free (wfa->x); |
| 146 |
Free (wfa->y); |
| 147 |
Free (wfa->mv_tree); |
| 148 |
Free (wfa->y_state); |
| 149 |
Free (wfa->into); |
| 150 |
Free (wfa->weight); |
| 151 |
Free (wfa->int_weight); |
| 152 |
Free (wfa->wfainfo); |
| 153 |
Free (wfa->prediction); |
| 154 |
Free (wfa->delta_state); |
| 155 |
if (wfa->y_column) |
| 156 |
Free (wfa->y_column); |
| 157 |
Free (wfa); |
| 158 |
} |
| 159 |
|
| 160 |
real_t |
| 161 |
compute_final_distribution (unsigned state, const wfa_t *wfa) |
| 162 |
/* |
| 163 |
* Compute the final distribution of the given 'state'. |
| 164 |
* Uses the fact that the generated 'wfa' is average preserving. |
| 165 |
* |
| 166 |
* Return value: |
| 167 |
* final distribution |
| 168 |
*/ |
| 169 |
{ |
| 170 |
unsigned label; |
| 171 |
real_t final = 0; |
| 172 |
|
| 173 |
for (label = 0; label < MAXLABELS; label++) |
| 174 |
{ |
| 175 |
unsigned edge; |
| 176 |
int domain; |
| 177 |
|
| 178 |
if (ischild (domain = wfa->tree [state][label])) |
| 179 |
final += wfa->final_distribution [domain]; |
| 180 |
for (edge = 0; isedge (domain = wfa->into [state][label][edge]); edge++) |
| 181 |
final += wfa->weight [state][label][edge] |
| 182 |
* wfa->final_distribution [domain]; |
| 183 |
} |
| 184 |
|
| 185 |
return final / MAXLABELS; |
| 186 |
} |
| 187 |
|
| 188 |
word_t * |
| 189 |
compute_hits (unsigned from, unsigned to, unsigned n, const wfa_t *wfa) |
| 190 |
/* |
| 191 |
* Selects the 'n' most popular domain images of the given 'wfa'. |
| 192 |
* Consider only linear combinations of state images |
| 193 |
* {i | 'from' <= i <= 'to'}. I.e. domains are in {i | from <= i < 'to'} |
| 194 |
* Always ensure that state 0 is among selected states even though from |
| 195 |
* may be > 0. |
| 196 |
* |
| 197 |
* Return value: |
| 198 |
* pointer to array of the most popular state images |
| 199 |
* sorted by increasing state numbers and terminated by -1 |
| 200 |
*/ |
| 201 |
{ |
| 202 |
word_t *domains; |
| 203 |
unsigned state, label, edge; |
| 204 |
int domain; |
| 205 |
pair_t *hits = Calloc (to, sizeof (pair_t)); |
| 206 |
|
| 207 |
for (domain = 0; domain < (int) to; domain++) |
| 208 |
{ |
| 209 |
hits [domain].value = domain; |
| 210 |
hits [domain].key = 0; |
| 211 |
} |
| 212 |
|
| 213 |
for (state = from; state <= to; state++) |
| 214 |
for (label = 0; label < MAXLABELS; label++) |
| 215 |
for (edge = 0; isedge (domain = wfa->into [state][label][edge]); |
| 216 |
edge++) |
| 217 |
hits [domain].key++; |
| 218 |
|
| 219 |
qsort (hits + 1, to - 1, sizeof (pair_t), sort_desc_pair); |
| 220 |
|
| 221 |
n = MIN(to, n); |
| 222 |
domains = Calloc (n + 1, sizeof (word_t)); |
| 223 |
|
| 224 |
for (domain = 0; domain < (int) n && (!domain || hits [domain].key); |
| 225 |
domain++) |
| 226 |
domains [domain] = hits [domain].value; |
| 227 |
if (n != domain) |
| 228 |
debug_message ("Only %d domains have been used in the luminance.", |
| 229 |
domain); |
| 230 |
n = domain; |
| 231 |
qsort (domains, n, sizeof (word_t), sort_asc_word); |
| 232 |
domains [n] = -1; |
| 233 |
|
| 234 |
Free (hits); |
| 235 |
|
| 236 |
return domains; |
| 237 |
} |
| 238 |
|
| 239 |
void |
| 240 |
append_edge (unsigned from, unsigned into, real_t weight, |
| 241 |
unsigned label, wfa_t *wfa) |
| 242 |
/* |
| 243 |
* Append an edge from state 'from' to state 'into' with |
| 244 |
* the given 'label' and 'weight' to the 'wfa'. |
| 245 |
* |
| 246 |
* No return value. |
| 247 |
* |
| 248 |
* Side effects: |
| 249 |
* 'wfa' structure is changed. |
| 250 |
*/ |
| 251 |
{ |
| 252 |
unsigned new; /* position of the new edge */ |
| 253 |
unsigned edge; |
| 254 |
|
| 255 |
/* |
| 256 |
* First look where to insert the new edge: |
| 257 |
* edges are sorted by increasing 'into' values |
| 258 |
*/ |
| 259 |
for (new = 0; (isedge (wfa->into [from][label][new]) |
| 260 |
&& wfa->into [from][label][new] < (int) into); new++) |
| 261 |
; |
| 262 |
/* |
| 263 |
* Move the edges 'n' to position 'n+1', for n = max, ..., 'new' |
| 264 |
*/ |
| 265 |
for (edge = new; isedge (wfa->into [from][label][edge]); edge++) |
| 266 |
; |
| 267 |
for (edge++; edge != new; edge--) |
| 268 |
{ |
| 269 |
wfa->into [from][label][edge] = wfa->into [from][label][edge - 1]; |
| 270 |
wfa->weight [from][label][edge] = wfa->weight [from][label][edge - 1]; |
| 271 |
wfa->int_weight [from][label][edge] |
| 272 |
= wfa->int_weight [from][label][edge - 1]; |
| 273 |
} |
| 274 |
/* |
| 275 |
* Insert the new edge |
| 276 |
*/ |
| 277 |
wfa->into [from][label][edge] = into; |
| 278 |
wfa->weight [from][label][edge] = weight; |
| 279 |
wfa->int_weight [from][label][edge] = weight * 512 + 0.5; |
| 280 |
} |
| 281 |
|
| 282 |
void |
| 283 |
remove_states (unsigned from, wfa_t *wfa) |
| 284 |
/* |
| 285 |
* Remove 'wfa' states 'wfa->basis_states',...,'wfa->states' - 1. |
| 286 |
* |
| 287 |
* No return value. |
| 288 |
* |
| 289 |
* Side effects: |
| 290 |
* 'wfa' structure is cleared for the given states. |
| 291 |
*/ |
| 292 |
{ |
| 293 |
unsigned state; |
| 294 |
|
| 295 |
for (state = from; state < wfa->states; state++) |
| 296 |
{ |
| 297 |
unsigned label; |
| 298 |
|
| 299 |
for (label = 0; label < MAXLABELS; label++) |
| 300 |
{ |
| 301 |
wfa->into [state][label][0] = NO_EDGE; |
| 302 |
wfa->tree [state][label] = RANGE; |
| 303 |
wfa->prediction [state][label] = FALSE; |
| 304 |
wfa->y_state [state][label] = RANGE; |
| 305 |
wfa->mv_tree [state][label].type = NONE; |
| 306 |
wfa->mv_tree [state][label].fx = 0; |
| 307 |
wfa->mv_tree [state][label].fy = 0; |
| 308 |
wfa->mv_tree [state][label].bx = 0; |
| 309 |
wfa->mv_tree [state][label].by = 0; |
| 310 |
} |
| 311 |
wfa->domain_type [state] = 0; |
| 312 |
wfa->delta_state [state] = FALSE; |
| 313 |
} |
| 314 |
|
| 315 |
wfa->states = from; |
| 316 |
} |
| 317 |
|
| 318 |
void |
| 319 |
copy_wfa (wfa_t *dst, const wfa_t *src) |
| 320 |
/* |
| 321 |
* Copy WFA struct 'src' to WFA struct 'dst'. |
| 322 |
* |
| 323 |
* No return value. |
| 324 |
* |
| 325 |
* Side effects: |
| 326 |
* 'dst' is filled with same data as 'src' |
| 327 |
* |
| 328 |
* NOTE: size of WFA 'dst' must be at least size of WFA 'src' |
| 329 |
*/ |
| 330 |
{ |
| 331 |
unsigned state; |
| 332 |
|
| 333 |
memset (dst->final_distribution, 0, MAXSTATES * sizeof (real_t)); |
| 334 |
memset (dst->level_of_state, 0, MAXSTATES * sizeof (byte_t)); |
| 335 |
memset (dst->domain_type, 0, MAXSTATES * sizeof (byte_t)); |
| 336 |
memset (dst->mv_tree, 0, MAXSTATES * MAXLABELS * sizeof (mv_t)); |
| 337 |
memset (dst->tree, 0, MAXSTATES * MAXLABELS * sizeof (word_t)); |
| 338 |
memset (dst->x, 0, MAXSTATES * MAXLABELS * sizeof (word_t)); |
| 339 |
memset (dst->y, 0, MAXSTATES * MAXLABELS * sizeof (word_t)); |
| 340 |
memset (dst->y_state, 0, MAXSTATES * MAXLABELS * sizeof (word_t)); |
| 341 |
memset (dst->into, NO_EDGE, |
| 342 |
MAXSTATES * MAXLABELS * (MAXEDGES + 1) * sizeof (word_t)); |
| 343 |
memset (dst->weight, 0, |
| 344 |
MAXSTATES * MAXLABELS * (MAXEDGES + 1) * sizeof (real_t)); |
| 345 |
memset (dst->int_weight, 0, |
| 346 |
MAXSTATES * MAXLABELS * (MAXEDGES + 1) * sizeof (word_t)); |
| 347 |
memset (dst->prediction, 0, MAXSTATES * MAXLABELS * sizeof (byte_t)); |
| 348 |
memset (dst->delta_state, 0, MAXSTATES * sizeof (bool_t)); |
| 349 |
if (dst->y_column) |
| 350 |
memset (dst->y_column, 0, MAXSTATES * MAXLABELS * sizeof (byte_t)); |
| 351 |
|
| 352 |
for (state = 0; state < MAXSTATES; state++) /* clear WFA struct */ |
| 353 |
{ |
| 354 |
unsigned label; |
| 355 |
|
| 356 |
for (label = 0; label < MAXLABELS; label++) |
| 357 |
{ |
| 358 |
dst->into [state][label][0] = NO_EDGE; |
| 359 |
dst->tree [state][label] = RANGE; |
| 360 |
dst->mv_tree [state][label].type = NONE; |
| 361 |
dst->y_state[state][label] = RANGE; |
| 362 |
} |
| 363 |
dst->delta_state [state] = NO; |
| 364 |
dst->domain_type [state] = 0; |
| 365 |
} |
| 366 |
|
| 367 |
dst->frame_type = src->frame_type; |
| 368 |
dst->states = src->states; |
| 369 |
dst->basis_states = src->basis_states; |
| 370 |
dst->root_state = src->root_state; |
| 371 |
|
| 372 |
memcpy (dst->wfainfo, src->wfainfo, sizeof (wfa_info_t)); |
| 373 |
|
| 374 |
if (dst->states == 0) /* nothing to do */ |
| 375 |
return; |
| 376 |
|
| 377 |
memcpy (dst->final_distribution, src->final_distribution, |
| 378 |
src->states * sizeof (real_t)); |
| 379 |
memcpy (dst->level_of_state, src->level_of_state, |
| 380 |
src->states * sizeof (byte_t)); |
| 381 |
memcpy (dst->domain_type, src->domain_type, |
| 382 |
src->states * sizeof (byte_t)); |
| 383 |
memcpy (dst->delta_state, src->delta_state, |
| 384 |
src->states * sizeof (bool_t)); |
| 385 |
memcpy (dst->mv_tree, src->mv_tree, |
| 386 |
src->states * MAXLABELS * sizeof (mv_t)); |
| 387 |
memcpy (dst->tree, src->tree, |
| 388 |
src->states * MAXLABELS * sizeof (word_t)); |
| 389 |
memcpy (dst->x, src->x, |
| 390 |
src->states * MAXLABELS * sizeof (word_t)); |
| 391 |
memcpy (dst->y, src->y, |
| 392 |
src->states * MAXLABELS * sizeof (word_t)); |
| 393 |
memcpy (dst->y_state, src->y_state, |
| 394 |
src->states * MAXLABELS * sizeof (word_t)); |
| 395 |
memcpy (dst->into, src->into, |
| 396 |
src->states * MAXLABELS * (MAXEDGES + 1) * sizeof (word_t)); |
| 397 |
memcpy (dst->weight, src->weight, |
| 398 |
src->states * MAXLABELS * (MAXEDGES + 1) * sizeof (real_t)); |
| 399 |
memcpy (dst->int_weight, src->int_weight, |
| 400 |
src->states * MAXLABELS * (MAXEDGES + 1) * sizeof (word_t)); |
| 401 |
memcpy (dst->prediction, src->prediction, |
| 402 |
src->states * MAXLABELS * sizeof (byte_t)); |
| 403 |
if (dst->y_column) |
| 404 |
memcpy (dst->y_column, src->y_column, |
| 405 |
src->states * MAXLABELS * sizeof (byte_t)); |
| 406 |
} |
| 407 |
|
| 408 |
void |
| 409 |
locate_subimage (unsigned orig_level, unsigned level, unsigned bintree, |
| 410 |
unsigned *x, unsigned *y, unsigned *width, unsigned *height) |
| 411 |
/* |
| 412 |
* Compute pixel coordinates of the subimage which 'bintree' address is given. |
| 413 |
* The level of the original image is 'orig_level' and the level of the |
| 414 |
* subimage is 'level'. |
| 415 |
* |
| 416 |
* No return value. |
| 417 |
* |
| 418 |
* Side effects: |
| 419 |
* '*x', '*y' coordinates of the upper left corner |
| 420 |
* '*width', '*height' size of image |
| 421 |
*/ |
| 422 |
{ |
| 423 |
/* |
| 424 |
* Compute coordinates of the subimage |
| 425 |
*/ |
| 426 |
*x = *y = 0; /* start at NW corner */ |
| 427 |
*width = width_of_level (level); |
| 428 |
*height = height_of_level (level); |
| 429 |
|
| 430 |
if (level > orig_level) |
| 431 |
{ |
| 432 |
error ("size of tile must be less or equal than image size."); |
| 433 |
return; |
| 434 |
} |
| 435 |
else if (bintree >= (unsigned) (1 << (orig_level - level))) |
| 436 |
{ |
| 437 |
error ("address out of bounds."); |
| 438 |
return; |
| 439 |
} |
| 440 |
else if (level < orig_level) |
| 441 |
{ |
| 442 |
unsigned mask; /* mask for bintree -> xy conversion */ |
| 443 |
bool_t hor; /* 1 next subdivision is horizontal |
| 444 |
0 next subdivision is vertical */ |
| 445 |
unsigned l = orig_level - 1; /* current level */ |
| 446 |
|
| 447 |
hor = orig_level % 2; /* start with vertival subdivision |
| 448 |
for square image and vice versa */ |
| 449 |
|
| 450 |
for (mask = 1 << (orig_level - level - 1); mask; mask >>= 1, hor = !hor) |
| 451 |
{ |
| 452 |
if (bintree & mask) /* change coordinates */ |
| 453 |
{ |
| 454 |
if (hor) /* horizontal subdivision */ |
| 455 |
*y += height_of_level (l); |
| 456 |
else /* vertical subdivision */ |
| 457 |
*x += width_of_level (l); |
| 458 |
} |
| 459 |
l--; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
static void |
| 465 |
findUncontainedTiles(unsigned int const tileCt, |
| 466 |
unsigned int const level, |
| 467 |
unsigned int const tilingExp, |
| 468 |
unsigned int const imageWidth, |
| 469 |
unsigned int const imageHeight, |
| 470 |
int * const vorder /* array */) { |
| 471 |
/*---------------------------------------------------------------------------- |
| 472 |
Set every element of 'vorder' (size 'tilingExp') to -1 to indicate the |
| 473 |
tile is not contained within the image or 0 to indicate that it is. |
| 474 |
-----------------------------------------------------------------------------*/ |
| 475 |
unsigned int address; |
| 476 |
|
| 477 |
for (address = 0; address < tileCt; ++address) { |
| 478 |
unsigned int x0, y0, width, height; |
| 479 |
|
| 480 |
locate_subimage(level, level - tilingExp, address, |
| 481 |
&x0, &y0, &width, &height); |
| 482 |
|
| 483 |
vorder[address] = (x0 < imageWidth && y0 < imageHeight) ? 0 : -1; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
static void |
| 490 |
debugPrintTiling(const int * const vorder, /* array */ |
| 491 |
unsigned int const tileCt) { |
| 492 |
|
| 493 |
unsigned int seq; |
| 494 |
unsigned int address; |
| 495 |
|
| 496 |
for (seq = 0, address = 0; address < tileCt; ++address) { |
| 497 |
if (vorder[address] != -1) { |
| 498 |
debug_message("number %u: address %u", |
| 499 |
seq++, (unsigned int)vorder[address]); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
void |
| 507 |
compute_spiral(int * const vorder, /* array */ |
| 508 |
unsigned int const imageWidth, |
| 509 |
unsigned int const imageHeight, |
| 510 |
unsigned int const tilingExp, |
| 511 |
bool_t const incSpiral) { |
| 512 |
/*---------------------------------------------------------------------------- |
| 513 |
Compute image tiling with spiral order. Put the tiling permutation in |
| 514 |
vorder[], which must be allocated large enough for one element per tile. |
| 515 |
|
| 516 |
Split the image into 2^tilingExp tiles. |
| 517 |
|
| 518 |
'incSpiral' specifies whether the spiral starts in the middle of the image |
| 519 |
(TRUE) or at the border (FALSE). |
| 520 |
|
| 521 |
'imageWidth' x 'imageHeight' define the size of the image. |
| 522 |
|
| 523 |
'imageWidth' and 'imageHeight' must be at least 2. |
| 524 |
-----------------------------------------------------------------------------*/ |
| 525 |
unsigned int const lx = log2(imageWidth - 1) + 1; |
| 526 |
unsigned int const ly = log2(imageHeight - 1) + 1; |
| 527 |
unsigned int const level = MAX(lx, ly) * 2 - ((ly == lx + 1) ? 1 : 0); |
| 528 |
unsigned int const tileCt = 1 << tilingExp; |
| 529 |
/* Total number of image tiles */ |
| 530 |
unsigned int const width = width_of_level (level - tilingExp); |
| 531 |
unsigned int const height = height_of_level(level - tilingExp); |
| 532 |
|
| 533 |
unsigned xmin, xmax, ymin, ymax; /* boundaries for current line */ |
| 534 |
unsigned address; /* bintree address */ |
| 535 |
|
| 536 |
assert(imageWidth >= 2); assert(imageHeight >= 2); |
| 537 |
|
| 538 |
findUncontainedTiles(tileCt, level, tilingExp, imageWidth, imageHeight, |
| 539 |
vorder); |
| 540 |
|
| 541 |
/* |
| 542 |
* 1234 |
| 543 |
* CDE5 Traverse image in spiral order |
| 544 |
* BGF6 starting at the top left corner |
| 545 |
* A987 |
| 546 |
*/ |
| 547 |
for (address = 0, |
| 548 |
xmin = 0, xmax = width_of_level(level), |
| 549 |
ymin = 0, ymax = height_of_level(level); address < tileCt;) { |
| 550 |
|
| 551 |
int x, y; /* current position; could be negative */ |
| 552 |
|
| 553 |
if (address < tileCt) { |
| 554 |
/* W>E */ |
| 555 |
for (x = xmin, y = ymin; x < xmax; x += width) { |
| 556 |
while (vorder[address] == -1) |
| 557 |
++address; |
| 558 |
if (x < imageWidth && y < imageHeight) /* valid range */ |
| 559 |
vorder[address++] = xy_to_address(x, y, level, tilingExp); |
| 560 |
while (address < tileCt && vorder[address] == -1) |
| 561 |
++address; |
| 562 |
} |
| 563 |
ymin += height; |
| 564 |
} |
| 565 |
|
| 566 |
if (address < tileCt) { |
| 567 |
/* N>S */ |
| 568 |
assert(xmax >= width); |
| 569 |
for (x = xmax - width, y = ymin; y < ymax; y += height) { |
| 570 |
while (vorder[address] == -1) |
| 571 |
++address; |
| 572 |
if (x <= imageWidth && y <= imageHeight) /* valid range */ |
| 573 |
vorder[address++] = xy_to_address(x, y, level, tilingExp); |
| 574 |
while (address < tileCt && vorder[address] == -1) |
| 575 |
++address; |
| 576 |
} |
| 577 |
assert(xmax >= width); |
| 578 |
xmax -= width; |
| 579 |
} |
| 580 |
|
| 581 |
if (address < tileCt) { |
| 582 |
/* E<W */ |
| 583 |
assert(xmax >= width); |
| 584 |
for (x = xmax - width, y = ymax - width; |
| 585 |
x >= (int)xmin; x -= width) { |
| 586 |
while (vorder[address] == -1) |
| 587 |
++address; |
| 588 |
if (x <= imageWidth && y <= imageHeight) /* valid range */ |
| 589 |
vorder[address++] = xy_to_address(x, y, level, tilingExp); |
| 590 |
while (address < tileCt && vorder[address] == -1) |
| 591 |
++address; |
| 592 |
} |
| 593 |
assert(ymax >= height); |
| 594 |
ymax -= height; |
| 595 |
} |
| 596 |
|
| 597 |
if (address < tileCt) { |
| 598 |
/* S>N */ |
| 599 |
assert(ymax >= height); |
| 600 |
for (x = xmin, y = ymax - height; y >= (int)ymin; y -= height) { |
| 601 |
while (vorder[address] == -1) |
| 602 |
++address; |
| 603 |
if (x <= imageWidth && y <= imageHeight) /* valid range */ |
| 604 |
vorder[address++] = xy_to_address(x, y, level, tilingExp); |
| 605 |
while (address < tileCt && vorder[address] == -1) |
| 606 |
++address; |
| 607 |
} |
| 608 |
xmin += width; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
if (incSpiral) { |
| 613 |
unsigned int i, j; |
| 614 |
|
| 615 |
for (i = 0, j = tileCt - 1; i < j;) { |
| 616 |
while (vorder[i] == -1) |
| 617 |
++i; |
| 618 |
while (vorder[j] == -1) |
| 619 |
--j; |
| 620 |
|
| 621 |
{ |
| 622 |
int const previousVorderI = vorder[i]; |
| 623 |
vorder[i] = vorder[j]; |
| 624 |
vorder[j] = previousVorderI; |
| 625 |
} |
| 626 |
++i; |
| 627 |
--j; |
| 628 |
} |
| 629 |
} |
| 630 |
debugPrintTiling(vorder, tileCt); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
bool_t |
| 636 |
find_range (unsigned x, unsigned y, unsigned band, |
| 637 |
const wfa_t *wfa, unsigned *range_state, unsigned *range_label) |
| 638 |
/* |
| 639 |
* Find a range ('*range_state', '*range_label') that contains |
| 640 |
* pixel ('x', 'y') in the iven color 'band'. |
| 641 |
* |
| 642 |
* Return value: |
| 643 |
* TRUE on success, or FALSE if there is no such range |
| 644 |
* |
| 645 |
* Side effects: |
| 646 |
* '*range_state' and '*range_label' are modified on success. |
| 647 |
*/ |
| 648 |
{ |
| 649 |
unsigned state, label; |
| 650 |
unsigned first_state, last_state; |
| 651 |
bool_t success = NO; |
| 652 |
|
| 653 |
first_state = wfa->basis_states; |
| 654 |
last_state = wfa->states; |
| 655 |
if (wfa->wfainfo->color) |
| 656 |
switch (band) |
| 657 |
{ |
| 658 |
case Y: |
| 659 |
first_state = wfa->basis_states; |
| 660 |
last_state = wfa->tree [wfa->tree [wfa->root_state][0]][0]; |
| 661 |
break; |
| 662 |
case Cb: |
| 663 |
first_state = wfa->tree [wfa->tree [wfa->root_state][0]][0] + 1; |
| 664 |
last_state = wfa->tree [wfa->tree [wfa->root_state][0]][1]; |
| 665 |
break; |
| 666 |
case Cr: |
| 667 |
first_state = wfa->tree [wfa->tree [wfa->root_state][0]][1] + 1; |
| 668 |
last_state = wfa->states; |
| 669 |
break; |
| 670 |
default: |
| 671 |
error ("unknown color component."); |
| 672 |
} |
| 673 |
|
| 674 |
for (state = first_state; state < last_state; state++) |
| 675 |
for (label = 0; label < MAXLABELS; label++) |
| 676 |
if (isrange (wfa->tree [state][label])) |
| 677 |
if (x >= wfa->x [state][label] && y >= wfa->y [state][label] |
| 678 |
&& x < (unsigned) (wfa->x [state][label] |
| 679 |
+ width_of_level (wfa->level_of_state [state] - 1)) |
| 680 |
&& y < (unsigned) (wfa->y [state][label] |
| 681 |
+ height_of_level (wfa->level_of_state [state] - 1))) |
| 682 |
{ |
| 683 |
success = YES; |
| 684 |
*range_state = state; |
| 685 |
*range_label = label; |
| 686 |
|
| 687 |
return success; |
| 688 |
} |
| 689 |
|
| 690 |
return success; |
| 691 |
} |
| 692 |
|
| 693 |
void |
| 694 |
sort_ranges (unsigned state, unsigned *domain, |
| 695 |
range_sort_t *rs, const wfa_t *wfa) |
| 696 |
/* |
| 697 |
* Generate list of ranges in coder order. |
| 698 |
* 'state' is the current state of the call tree while 'domain' is the |
| 699 |
* index of the last added WFA state. |
| 700 |
* |
| 701 |
* Side effects: |
| 702 |
* 'domain' is incremented after recursion returns |
| 703 |
* 'rs' is filled accordingly |
| 704 |
* |
| 705 |
* No return value. |
| 706 |
*/ |
| 707 |
{ |
| 708 |
unsigned label; |
| 709 |
|
| 710 |
for (label = 0; label < MAXLABELS; label++) |
| 711 |
{ |
| 712 |
if (isrange (wfa->tree [state][label])) |
| 713 |
rs->range_subdivided [rs->range_no] = NO; |
| 714 |
else |
| 715 |
{ |
| 716 |
sort_ranges (wfa->tree [state][label], domain, rs, wfa); |
| 717 |
rs->range_subdivided [rs->range_no] = YES; |
| 718 |
} |
| 719 |
|
| 720 |
rs->range_state [rs->range_no] = state; |
| 721 |
rs->range_label [rs->range_no] = label; |
| 722 |
rs->range_max_domain [rs->range_no] = *domain; |
| 723 |
while (!usedomain (rs->range_max_domain [rs->range_no], wfa)) |
| 724 |
rs->range_max_domain [rs->range_no]--; |
| 725 |
|
| 726 |
if (label == 1 || !rs->range_subdivided [rs->range_no]) |
| 727 |
rs->range_no++; |
| 728 |
} |
| 729 |
|
| 730 |
(*domain)++; |
| 731 |
} |
| 732 |
|
| 733 |
bool_t |
| 734 |
locate_delta_images (wfa_t *wfa) |
| 735 |
/* |
| 736 |
* Locate all WFA states that are part of a delta approximation. |
| 737 |
* I.e., these states are assigned to ranges that have been predicted |
| 738 |
* via MC or ND. |
| 739 |
* |
| 740 |
* Return value: |
| 741 |
* TRUE at least one state is part of a delta approximation |
| 742 |
* FALSE no delta approximations in this WFA |
| 743 |
* |
| 744 |
* Side effects: |
| 745 |
* 'wfa->delta [state][label]' is set accordingly. |
| 746 |
*/ |
| 747 |
{ |
| 748 |
unsigned state, label; |
| 749 |
bool_t delta = NO; |
| 750 |
|
| 751 |
for (state = wfa->root_state; state >= wfa->basis_states; state--) |
| 752 |
wfa->delta_state [state] = NO; |
| 753 |
|
| 754 |
for (state = wfa->root_state; state >= wfa->basis_states; state--) |
| 755 |
for (label = 0; label < MAXLABELS; label++) |
| 756 |
if (ischild (wfa->tree [state][label])) |
| 757 |
if (wfa->mv_tree [state][label].type != NONE |
| 758 |
|| isedge (wfa->into [state][label][0]) |
| 759 |
|| wfa->delta_state [state]) |
| 760 |
{ |
| 761 |
delta = YES; |
| 762 |
wfa->delta_state [wfa->tree [state][label]] = YES; |
| 763 |
} |
| 764 |
|
| 765 |
return delta; |
| 766 |
} |
| 767 |
|
| 768 |
/***************************************************************************** |
| 769 |
|
| 770 |
private code |
| 771 |
|
| 772 |
******************************************************************************/ |
| 773 |
|
| 774 |
static unsigned |
| 775 |
xy_to_address (unsigned x, unsigned y, unsigned level, unsigned n) |
| 776 |
/* |
| 777 |
* Compute bintree address of subimage at coordinates ('x', 'y'). |
| 778 |
* Size of original image is determined by 'level'. |
| 779 |
* 'n' specifies number of iterations. |
| 780 |
* |
| 781 |
* Return value: |
| 782 |
* address of subimage |
| 783 |
*/ |
| 784 |
{ |
| 785 |
unsigned address = 0; |
| 786 |
|
| 787 |
while (n--) |
| 788 |
{ |
| 789 |
address <<= 1; |
| 790 |
if (--level % 2) |
| 791 |
{ |
| 792 |
if (x & width_of_level (level)) |
| 793 |
address++; |
| 794 |
} |
| 795 |
else |
| 796 |
{ |
| 797 |
if (y & height_of_level (level)) |
| 798 |
address++; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
return address; |
| 803 |
} |