2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
39 ==========================================================
40 Tag-tree coder interface
41 ==========================================================
43 void tgt_tree_dump(FILE *fd, opj_tgt_tree_t * tree)
47 fprintf(fd, "TGT_TREE {\n");
48 fprintf(fd, " numnodes: %d \n", tree->numnodes);
49 fprintf(fd, " numleafsh: %d, numleafsv: %d, numleafsz: %d,\n", tree->numleafsh,
50 tree->numleafsv, tree->numleafsz);
52 for (nodesno = 0; nodesno < tree->numnodes; nodesno++) {
53 fprintf(fd, "tgt_node %d {\n", nodesno);
54 fprintf(fd, " value: %d \n", tree->nodes[nodesno].value);
55 fprintf(fd, " low: %d \n", tree->nodes[nodesno].low);
56 fprintf(fd, " known: %d \n", tree->nodes[nodesno].known);
57 if (tree->nodes[nodesno].parent) {
58 fprintf(fd, " parent.value: %d \n", tree->nodes[nodesno].parent->value);
59 fprintf(fd, " parent.low: %d \n", tree->nodes[nodesno].parent->low);
60 fprintf(fd, " parent.known: %d \n", tree->nodes[nodesno].parent->known);
70 opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz)
76 opj_tgt_node_t *node = NULL;
77 opj_tgt_node_t *parentnode = NULL;
78 opj_tgt_node_t *parentnode0 = NULL;
79 opj_tgt_node_t *parentnode1 = NULL;
80 opj_tgt_tree_t *tree = NULL;
85 tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
89 tree->numleafsh = numleafsh;
90 tree->numleafsv = numleafsv;
91 tree->numleafsz = numleafsz;
99 n = nplh[numlvls] * nplv[numlvls] * nplz[numlvls];
100 nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
101 nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
102 nplz[numlvls + 1] = (nplz[numlvls] + 1) / 2;
107 if (tree->numnodes == 0) {
112 tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(
120 parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv * tree->numleafsz];
121 parentnode0 = parentnode;
122 parentnode1 = parentnode;
123 /*fprintf(stdout,"\nH %d V %d Z %d numlvls %d nodes %d\n",tree->numleafsh,tree->numleafsv,tree->numleafsz,numlvls,tree->numnodes);*/
124 for (i = 0; i < numlvls - 1; ++i) {
125 for (z = 0; z < nplz[i]; ++z) {
126 for (j = 0; j < nplv[i]; ++j) {
130 parentnode; /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/
134 parentnode; /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/
139 if ((j & 1) || j == nplv[i] - 1) {
140 parentnode0 = parentnode;
142 parentnode = parentnode0;
145 if ((z & 1) || z == nplz[i] - 1) {
146 parentnode1 = parentnode;
148 parentnode0 = parentnode1;
149 parentnode = parentnode1;
161 void tgt_destroy(opj_tgt_tree_t *tree)
163 opj_free(tree->nodes);
167 void tgt_reset(opj_tgt_tree_t *tree)
175 for (i = 0; i < tree->numnodes; i++) {
176 tree->nodes[i].value = 999;
177 tree->nodes[i].low = 0;
178 tree->nodes[i].known = 0;
182 void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value)
184 opj_tgt_node_t *node;
185 node = &tree->nodes[leafno];
186 while (node && node->value > value) {
192 void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno,
195 opj_tgt_node_t *stk[31];
196 opj_tgt_node_t **stkptr;
197 opj_tgt_node_t *node;
201 node = &tree->nodes[leafno];
202 while (node->parent) {
209 if (low > node->low) {
215 while (low < threshold) {
216 if (low >= node->value) {
218 bio_write(bio, 1, 1);
223 bio_write(bio, 0, 1);
235 int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold)
237 opj_tgt_node_t *stk[31];
238 opj_tgt_node_t **stkptr;
239 opj_tgt_node_t *node;
243 node = &tree->nodes[leafno];
244 while (node->parent) {
251 if (low > node->low) {
256 while (low < threshold && low < node->value) {
257 if (bio_read(bio, 1)) {
270 return (node->value < threshold) ? 1 : 0;