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];
111 opj_tgt_tree_t *tgt_create_v2(OPJ_UINT32 numleafsh, OPJ_UINT32 numleafsv) {
114 opj_tgt_node_t *node = 00;
115 opj_tgt_node_t *l_parent_node = 00;
116 opj_tgt_node_t *l_parent_node0 = 00;
117 opj_tgt_tree_t *tree = 00;
123 tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
125 fprintf(stderr, "ERROR in tgt_create_v2 while allocating tree\n");
128 memset(tree,0,sizeof(opj_tgt_tree_t));
130 tree->numleafsh = numleafsh;
131 tree->numleafsv = numleafsv;
138 n = nplh[numlvls] * nplv[numlvls];
139 nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
140 nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
146 if (tree->numnodes == 0) {
148 fprintf(stderr, "WARNING in tgt_create_v2 tree->numnodes == 0, no tree created.\n");
152 tree->nodes = (opj_tgt_node_t*) opj_calloc(tree->numnodes, sizeof(opj_tgt_node_t));
154 fprintf(stderr, "ERROR in tgt_create_v2 while allocating node of the tree\n");
158 memset(tree->nodes,0,tree->numnodes * sizeof(opj_tgt_node_t));
159 tree->nodes_size = tree->numnodes * sizeof(opj_tgt_node_t);
162 l_parent_node = &tree->nodes[tree->numleafsh * tree->numleafsv];
163 l_parent_node0 = l_parent_node;
165 for (i = 0; i < numlvls - 1; ++i) {
166 for (j = 0; j < nplv[i]; ++j) {
169 node->parent = l_parent_node;
172 node->parent = l_parent_node;
177 if ((j & 1) || j == nplv[i] - 1) {
178 l_parent_node0 = l_parent_node;
180 l_parent_node = l_parent_node0;
181 l_parent_node0 += nplh[i];
191 * Reinitialises a tag-tree from an exixting one. (V2 framevork)
193 * @param p_tree the tree to reinitialize.
194 * @param p_num_leafs_h the width of the array of leafs of the tree
195 * @param p_num_leafs_v the height of the array of leafs of the tree
196 * @return a new tag-tree if successful, NULL otherwise
198 opj_tgt_tree_t *tgt_init(opj_tgt_tree_t * p_tree,OPJ_UINT32 p_num_leafs_h, OPJ_UINT32 p_num_leafs_v)
200 OPJ_INT32 l_nplh[32];
201 OPJ_INT32 l_nplv[32];
202 opj_tgt_node_t *l_node = 00;
203 opj_tgt_node_t *l_parent_node = 00;
204 opj_tgt_node_t *l_parent_node0 = 00;
207 OPJ_UINT32 l_num_levels;
209 OPJ_UINT32 l_node_size;
217 ((p_tree->numleafsh != p_num_leafs_h) || (p_tree->numleafsv != p_num_leafs_v))
219 p_tree->numleafsh = p_num_leafs_h;
220 p_tree->numleafsv = p_num_leafs_v;
223 l_nplh[0] = p_num_leafs_h;
224 l_nplv[0] = p_num_leafs_v;
225 p_tree->numnodes = 0;
228 n = l_nplh[l_num_levels] * l_nplv[l_num_levels];
229 l_nplh[l_num_levels + 1] = (l_nplh[l_num_levels] + 1) / 2;
230 l_nplv[l_num_levels + 1] = (l_nplv[l_num_levels] + 1) / 2;
231 p_tree->numnodes += n;
238 (p_tree->numnodes == 0)
243 l_node_size = p_tree->numnodes * sizeof(opj_tgt_node_t);
245 (l_node_size > p_tree->nodes_size)
247 p_tree->nodes = (opj_tgt_node_t*) opj_realloc(p_tree->nodes, l_node_size);
254 memset(((char *) p_tree->nodes) + p_tree->nodes_size, 0 , l_node_size - p_tree->nodes_size);
255 p_tree->nodes_size = l_node_size;
257 l_node = p_tree->nodes;
258 l_parent_node = &p_tree->nodes[p_tree->numleafsh * p_tree->numleafsv];
259 l_parent_node0 = l_parent_node;
262 (i = 0; i < l_num_levels - 1; ++i)
265 (j = 0; j < l_nplv[i]; ++j)
271 l_node->parent = l_parent_node;
275 l_node->parent = l_parent_node;
280 if ((j & 1) || j == l_nplv[i] - 1)
282 l_parent_node0 = l_parent_node;
286 l_parent_node = l_parent_node0;
287 l_parent_node0 += l_nplh[i];
298 /*void tgt_destroy(opj_tgt_tree_t *tree) {
299 opj_free(tree->nodes);
303 void tgt_destroy(opj_tgt_tree_t *p_tree)
310 opj_free(p_tree->nodes);
316 /*void tgt_reset(opj_tgt_tree_t *tree) {
322 for (i = 0; i < tree->numnodes; i++) {
323 tree->nodes[i].value = 999;
324 tree->nodes[i].low = 0;
325 tree->nodes[i].known = 0;
329 void tgt_reset(opj_tgt_tree_t *p_tree) {
331 opj_tgt_node_t * l_current_node = 00;;
337 l_current_node = p_tree->nodes;
338 for (i = 0; i < p_tree->numnodes; ++i)
340 l_current_node->value = 999;
341 l_current_node->low = 0;
342 l_current_node->known = 0;
347 void tgt_setvalue(opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 value) {
348 opj_tgt_node_t *node;
349 node = &tree->nodes[leafno];
350 while (node && node->value > value) {
356 void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) {
357 opj_tgt_node_t *stk[31];
358 opj_tgt_node_t **stkptr;
359 opj_tgt_node_t *node;
363 node = &tree->nodes[leafno];
364 while (node->parent) {
371 if (low > node->low) {
377 while (low < threshold) {
378 if (low >= node->value) {
380 bio_write(bio, 1, 1);
385 bio_write(bio, 0, 1);
396 OPJ_UINT32 tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, OPJ_UINT32 leafno, OPJ_INT32 threshold) {
397 opj_tgt_node_t *stk[31];
398 opj_tgt_node_t **stkptr;
399 opj_tgt_node_t *node;
403 node = &tree->nodes[leafno];
404 while (node->parent) {
411 if (low > node->low) {
416 while (low < threshold && low < node->value) {
417 if (bio_read(bio, 1)) {
430 return (node->value < threshold) ? 1 : 0;