2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
35 ==========================================================
36 Tag-tree coder interface
37 ==========================================================
40 opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) {
43 opj_tgt_node_t *node = NULL;
44 opj_tgt_node_t *parentnode = NULL;
45 opj_tgt_node_t *parentnode0 = NULL;
46 opj_tgt_tree_t *tree = NULL;
51 tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
52 if(!tree) return NULL;
53 tree->numleafsh = numleafsh;
54 tree->numleafsv = numleafsv;
61 n = nplh[numlvls] * nplv[numlvls];
62 nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
63 nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
69 if (tree->numnodes == 0) {
74 tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t));
81 parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];
82 parentnode0 = parentnode;
84 for (i = 0; i < numlvls - 1; ++i) {
85 for (j = 0; j < nplv[i]; ++j) {
88 node->parent = parentnode;
91 node->parent = parentnode;
96 if ((j & 1) || j == nplv[i] - 1) {
97 parentnode0 = parentnode;
99 parentnode = parentnode0;
100 parentnode0 += nplh[i];
112 * Reinitialises a tag-tree from an exixting one.
114 * @param p_tree the tree to reinitialize.
115 * @param p_num_leafs_h the width of the array of leafs of the tree
116 * @param p_num_leafs_v the height of the array of leafs of the tree
117 * @return a new tag-tree if successful, NULL otherwise
119 opj_tgt_tree_t *tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, OPJ_UINT32 p_num_leafs_v)
121 OPJ_INT32 l_nplh[32];
122 OPJ_INT32 l_nplv[32];
123 opj_tgt_node_t *l_node = 00;
124 opj_tgt_node_t *l_parent_node = 00;
125 opj_tgt_node_t *l_parent_node0 = 00;
128 OPJ_UINT32 l_num_levels;
130 OPJ_UINT32 l_node_size;
138 ((p_tree->numleafsh != p_num_leafs_h) || (p_tree->numleafsv != p_num_leafs_v))
140 p_tree->numleafsh = p_num_leafs_h;
141 p_tree->numleafsv = p_num_leafs_v;
144 l_nplh[0] = p_num_leafs_h;
145 l_nplv[0] = p_num_leafs_v;
146 p_tree->numnodes = 0;
149 n = l_nplh[l_num_levels] * l_nplv[l_num_levels];
150 l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2;
151 l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2;
152 p_tree->numnodes += n;
159 (p_tree->numnodes == 0)
164 l_node_size = p_tree->numnodes * sizeof(opj_tgt_node_t);
166 (l_node_size > p_tree->nodes_size)
168 p_tree->nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, l_node_size);
175 memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0 , l_node_size - p_tree->nodes_size);
176 p_tree->nodes_size = l_node_size;
178 l_node = p_tree->nodes;
179 l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv];
180 l_parent_node0 = l_parent_node;
183 (i = 0; i < l_num_levels - 1; ++i)
186 (j = 0; j < l_nplv[i]; ++j)
192 l_node->parent = l_parent_node;
196 l_node->parent = l_parent_node;
201 if ((j & 1) || j == l_nplv[i] - 1)
203 l_parent_node0 = l_parent_node;
207 l_parent_node = l_parent_node0;
208 l_parent_node0 += l_nplh[i];
219 void tgt_destroy(opj_tgt_tree_t *tree) {
220 opj_free(tree->nodes);
224 void tgt_reset(opj_tgt_tree_t *tree) {
230 for (i = 0; i < tree->numnodes; i++) {
231 tree->nodes[i].value = 999;
232 tree->nodes[i].low = 0;
233 tree->nodes[i].known = 0;
237 void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
238 opj_tgt_node_t *node;
239 node = &tree->nodes[leafno];
240 while (node && node->value > value) {
246 void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
247 opj_tgt_node_t *stk[31];
248 opj_tgt_node_t **stkptr;
249 opj_tgt_node_t *node;
253 node = &tree->nodes[leafno];
254 while (node->parent) {
261 if (low > node->low) {
267 while (low < threshold) {
268 if (low >= node->value) {
270 bio_write(bio, 1, 1);
275 bio_write(bio, 0, 1);
286 int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
287 opj_tgt_node_t *stk[31];
288 opj_tgt_node_t **stkptr;
289 opj_tgt_node_t *node;
293 node = &tree->nodes[leafno];
294 while (node->parent) {
301 if (low > node->low) {
306 while (low < threshold && low < node->value) {
307 if (bio_read(bio, 1)) {
320 return (node->value < threshold) ? 1 : 0;