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 void tgt_destroy(opj_tgt_tree_t *tree) {
112 opj_free(tree->nodes);
116 void tgt_reset(opj_tgt_tree_t *tree) {
122 for (i = 0; i < tree->numnodes; i++) {
123 tree->nodes[i].value = 999;
124 tree->nodes[i].low = 0;
125 tree->nodes[i].known = 0;
129 void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
130 opj_tgt_node_t *node;
131 node = &tree->nodes[leafno];
132 while (node && node->value > value) {
138 void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
139 opj_tgt_node_t *stk[31];
140 opj_tgt_node_t **stkptr;
141 opj_tgt_node_t *node;
145 node = &tree->nodes[leafno];
146 while (node->parent) {
153 if (low > node->low) {
159 while (low < threshold) {
160 if (low >= node->value) {
162 bio_write(bio, 1, 1);
167 bio_write(bio, 0, 1);
178 int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
179 opj_tgt_node_t *stk[31];
180 opj_tgt_node_t **stkptr;
181 opj_tgt_node_t *node;
185 node = &tree->nodes[leafno];
186 while (node->parent) {
193 if (low > node->low) {
198 while (low < threshold && low < node->value) {
199 if (bio_read(bio, 1)) {
212 return (node->value < threshold) ? 1 : 0;