2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
34 ==========================================================
35 Tag-tree coder interface
36 ==========================================================
39 opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv) {
42 opj_tgt_node_t *node = NULL;
43 opj_tgt_node_t *parentnode = NULL;
44 opj_tgt_node_t *parentnode0 = NULL;
45 opj_tgt_tree_t *tree = NULL;
50 tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
51 if(!tree) return NULL;
52 tree->numleafsh = numleafsh;
53 tree->numleafsv = numleafsv;
60 n = nplh[numlvls] * nplv[numlvls];
61 nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
62 nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
68 if (tree->numnodes == 0) {
73 tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t));
80 parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv];
81 parentnode0 = parentnode;
83 for (i = 0; i < numlvls - 1; ++i) {
84 for (j = 0; j < nplv[i]; ++j) {
87 node->parent = parentnode;
90 node->parent = parentnode;
95 if ((j & 1) || j == nplv[i] - 1) {
96 parentnode0 = parentnode;
98 parentnode = parentnode0;
99 parentnode0 += nplh[i];
110 void tgt_destroy(opj_tgt_tree_t *tree) {
111 opj_free(tree->nodes);
115 void tgt_reset(opj_tgt_tree_t *tree) {
121 for (i = 0; i < tree->numnodes; i++) {
122 tree->nodes[i].value = 999;
123 tree->nodes[i].low = 0;
124 tree->nodes[i].known = 0;
128 void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
129 opj_tgt_node_t *node;
130 node = &tree->nodes[leafno];
131 while (node && node->value > value) {
137 void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
138 opj_tgt_node_t *stk[31];
139 opj_tgt_node_t **stkptr;
140 opj_tgt_node_t *node;
144 node = &tree->nodes[leafno];
145 while (node->parent) {
152 if (low > node->low) {
158 while (low < threshold) {
159 if (low >= node->value) {
161 bio_write(bio, 1, 1);
166 bio_write(bio, 0, 1);
177 int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
178 opj_tgt_node_t *stk[31];
179 opj_tgt_node_t **stkptr;
180 opj_tgt_node_t *node;
184 node = &tree->nodes[leafno];
185 while (node->parent) {
192 if (low > node->low) {
197 while (low < threshold && low < node->value) {
198 if (bio_read(bio, 1)) {
211 return (node->value < threshold) ? 1 : 0;