2003-11-27 11:10:17 +01:00
|
|
|
|
/*
|
2005-12-02 14:34:15 +01:00
|
|
|
|
* Copyright (c) 2001-2003, David Janssens
|
|
|
|
|
* Copyright (c) 2002-2003, Yannick Verschueren
|
|
|
|
|
* Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
|
|
|
|
|
* Copyright (c) 2005, Herv<EFBFBD> Drolon, FreeImage Team
|
|
|
|
|
* Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
|
2003-11-27 11:10:17 +01:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
#include "opj_includes.h"
|
2003-11-27 11:10:17 +01:00
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
/*
|
|
|
|
|
==========================================================
|
|
|
|
|
Tag-tree coder interface
|
|
|
|
|
==========================================================
|
|
|
|
|
*/
|
2003-11-27 11:10:17 +01:00
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) {
|
2004-04-29 15:27:24 +02:00
|
|
|
|
int nplh[32];
|
|
|
|
|
int nplv[32];
|
2005-12-02 14:34:15 +01:00
|
|
|
|
opj_tgt_node_t *node = NULL;
|
|
|
|
|
opj_tgt_node_t *parentnode = NULL;
|
|
|
|
|
opj_tgt_node_t *parentnode0 = NULL;
|
|
|
|
|
opj_tgt_tree_t *tree = NULL;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
int i, j, k;
|
|
|
|
|
int numlvls;
|
|
|
|
|
int n;
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
|
|
|
|
|
if(!tree) return NULL;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
tree->numleafsh = numleafsh;
|
|
|
|
|
tree->numleafsv = numleafsv;
|
|
|
|
|
|
|
|
|
|
numlvls = 0;
|
|
|
|
|
nplh[0] = numleafsh;
|
|
|
|
|
nplv[0] = numleafsv;
|
|
|
|
|
tree->numnodes = 0;
|
|
|
|
|
do {
|
|
|
|
|
n = nplh[numlvls] * nplv[numlvls];
|
|
|
|
|
nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
|
|
|
|
|
nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
|
|
|
|
|
tree->numnodes += n;
|
|
|
|
|
++numlvls;
|
|
|
|
|
} while (n > 1);
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
/* ADD */
|
|
|
|
|
if (tree->numnodes == 0) {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
opj_free(tree);
|
2004-04-29 15:27:24 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t));
|
|
|
|
|
if(!tree->nodes) {
|
|
|
|
|
opj_free(tree);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2004-04-29 15:27:24 +02:00
|
|
|
|
|
|
|
|
|
node = tree->nodes;
|
|
|
|
|
parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];
|
|
|
|
|
parentnode0 = parentnode;
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
for (i = 0; i < numlvls - 1; ++i) {
|
|
|
|
|
for (j = 0; j < nplv[i]; ++j) {
|
|
|
|
|
k = nplh[i];
|
|
|
|
|
while (--k >= 0) {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
node->parent = parentnode;
|
|
|
|
|
++node;
|
|
|
|
|
if (--k >= 0) {
|
|
|
|
|
node->parent = parentnode;
|
|
|
|
|
++node;
|
|
|
|
|
}
|
|
|
|
|
++parentnode;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
}
|
|
|
|
|
if ((j & 1) || j == nplv[i] - 1) {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
parentnode0 = parentnode;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
} else {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
parentnode = parentnode0;
|
|
|
|
|
parentnode0 += nplh[i];
|
2004-04-29 15:27:24 +02:00
|
|
|
|
}
|
2004-04-29 15:10:05 +02:00
|
|
|
|
}
|
2004-04-29 15:27:24 +02:00
|
|
|
|
}
|
|
|
|
|
node->parent = 0;
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
tgt_reset(tree);
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
return tree;
|
2003-11-27 11:10:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
void tgt_destroy(opj_tgt_tree_t *tree) {
|
|
|
|
|
opj_free(tree->nodes);
|
|
|
|
|
opj_free(tree);
|
2003-11-27 11:10:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
void tgt_reset(opj_tgt_tree_t *tree) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (NULL == tree)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < tree->numnodes; i++) {
|
|
|
|
|
tree->nodes[i].value = 999;
|
|
|
|
|
tree->nodes[i].low = 0;
|
|
|
|
|
tree->nodes[i].known = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
|
|
|
|
|
opj_tgt_node_t *node;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
node = &tree->nodes[leafno];
|
|
|
|
|
while (node && node->value > value) {
|
|
|
|
|
node->value = value;
|
|
|
|
|
node = node->parent;
|
|
|
|
|
}
|
2003-11-27 11:10:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
|
|
|
|
|
opj_tgt_node_t *stk[31];
|
|
|
|
|
opj_tgt_node_t **stkptr;
|
|
|
|
|
opj_tgt_node_t *node;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
int low;
|
|
|
|
|
|
|
|
|
|
stkptr = stk;
|
|
|
|
|
node = &tree->nodes[leafno];
|
|
|
|
|
while (node->parent) {
|
|
|
|
|
*stkptr++ = node;
|
|
|
|
|
node = node->parent;
|
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
low = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (low > node->low) {
|
|
|
|
|
node->low = low;
|
|
|
|
|
} else {
|
|
|
|
|
low = node->low;
|
2004-04-29 15:10:05 +02:00
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
while (low < threshold) {
|
|
|
|
|
if (low >= node->value) {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
if (!node->known) {
|
|
|
|
|
bio_write(bio, 1, 1);
|
|
|
|
|
node->known = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
bio_write(bio, 0, 1);
|
2004-04-29 15:27:24 +02:00
|
|
|
|
++low;
|
2004-04-29 15:10:05 +02:00
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
node->low = low;
|
|
|
|
|
if (stkptr == stk)
|
|
|
|
|
break;
|
|
|
|
|
node = *--stkptr;
|
|
|
|
|
}
|
2003-11-27 11:10:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2005-12-02 14:34:15 +01:00
|
|
|
|
int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
|
|
|
|
|
opj_tgt_node_t *stk[31];
|
|
|
|
|
opj_tgt_node_t **stkptr;
|
|
|
|
|
opj_tgt_node_t *node;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
int low;
|
|
|
|
|
|
|
|
|
|
stkptr = stk;
|
|
|
|
|
node = &tree->nodes[leafno];
|
|
|
|
|
while (node->parent) {
|
|
|
|
|
*stkptr++ = node;
|
|
|
|
|
node = node->parent;
|
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
low = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (low > node->low) {
|
|
|
|
|
node->low = low;
|
|
|
|
|
} else {
|
|
|
|
|
low = node->low;
|
2004-04-29 15:10:05 +02:00
|
|
|
|
}
|
2004-04-29 15:27:24 +02:00
|
|
|
|
while (low < threshold && low < node->value) {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
if (bio_read(bio, 1)) {
|
|
|
|
|
node->value = low;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
} else {
|
2005-12-02 14:34:15 +01:00
|
|
|
|
++low;
|
2004-04-29 15:27:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
node->low = low;
|
|
|
|
|
if (stkptr == stk) {
|
|
|
|
|
break;
|
2004-04-29 15:10:05 +02:00
|
|
|
|
}
|
2004-04-29 15:27:24 +02:00
|
|
|
|
node = *--stkptr;
|
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
|
2004-04-29 15:27:24 +02:00
|
|
|
|
return (node->value < threshold) ? 1 : 0;
|
2003-11-27 11:10:17 +01:00
|
|
|
|
}
|