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){
46 fprintf(fd, "TGT_TREE {\n");
47 fprintf(fd, " numnodes: %d \n", tree->numnodes);
48 fprintf(fd, " numleafsh: %d, numleafsv: %d, numleafsz: %d,\n", tree->numleafsh, tree->numleafsv, tree->numleafsz);
50 for (nodesno = 0; nodesno < tree->numnodes; nodesno++) {
51 fprintf(fd, "tgt_node %d {\n", nodesno);
52 fprintf(fd, " value: %d \n", tree->nodes[nodesno].value);
53 fprintf(fd, " low: %d \n", tree->nodes[nodesno].low);
54 fprintf(fd, " known: %d \n", tree->nodes[nodesno].known);
55 if (tree->nodes[nodesno].parent) {
56 fprintf(fd, " parent.value: %d \n", tree->nodes[nodesno].parent->value);
57 fprintf(fd, " parent.low: %d \n", tree->nodes[nodesno].parent->low);
58 fprintf(fd, " parent.known: %d \n", tree->nodes[nodesno].parent->known);
68 opj_tgt_tree_t *tgt_create(int numleafsh, int numleafsv, int numleafsz) {
73 opj_tgt_node_t *node = NULL;
74 opj_tgt_node_t *parentnode = NULL;
75 opj_tgt_node_t *parentnode0 = NULL;
76 opj_tgt_node_t *parentnode1 = NULL;
77 opj_tgt_tree_t *tree = NULL;
82 tree = (opj_tgt_tree_t *) opj_malloc(sizeof(opj_tgt_tree_t));
85 tree->numleafsh = numleafsh;
86 tree->numleafsv = numleafsv;
87 tree->numleafsz = numleafsz;
95 n = nplh[numlvls] * nplv[numlvls] * nplz[numlvls];
96 nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2;
97 nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2;
98 nplz[numlvls + 1] = (nplz[numlvls] + 1) / 2;
103 if (tree->numnodes == 0) {
108 tree->nodes = (opj_tgt_node_t *) opj_malloc(tree->numnodes * sizeof(opj_tgt_node_t));
115 parentnode = &tree->nodes[tree->numleafsh * tree->numleafsv * tree->numleafsz];
116 parentnode0 = parentnode;
117 parentnode1 = parentnode;
118 /*fprintf(stdout,"\nH %d V %d Z %d numlvls %d nodes %d\n",tree->numleafsh,tree->numleafsv,tree->numleafsz,numlvls,tree->numnodes);*/
119 for (i = 0; i < numlvls - 1; ++i) {
120 for (z = 0; z < nplz[i]; ++z) {
121 for (j = 0; j < nplv[i]; ++j) {
124 node->parent = parentnode; /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/
127 node->parent = parentnode; /*fprintf(stdout,"node[%d].parent = node[%d]\n",n,p);*/
132 if((j & 1) || j == nplv[i] - 1) {
133 parentnode0 = parentnode;
135 parentnode = parentnode0;
138 if ((z & 1) || z == nplz[i] - 1) {
139 parentnode1 = parentnode;
141 parentnode0 = parentnode1;
142 parentnode = parentnode1;
154 void tgt_destroy(opj_tgt_tree_t *tree) {
155 opj_free(tree->nodes);
159 void tgt_reset(opj_tgt_tree_t *tree) {
165 for (i = 0; i < tree->numnodes; i++) {
166 tree->nodes[i].value = 999;
167 tree->nodes[i].low = 0;
168 tree->nodes[i].known = 0;
172 void tgt_setvalue(opj_tgt_tree_t *tree, int leafno, int value) {
173 opj_tgt_node_t *node;
174 node = &tree->nodes[leafno];
175 while (node && node->value > value) {
181 void tgt_encode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
182 opj_tgt_node_t *stk[31];
183 opj_tgt_node_t **stkptr;
184 opj_tgt_node_t *node;
188 node = &tree->nodes[leafno];
189 while (node->parent) {
196 if (low > node->low) {
202 while (low < threshold) {
203 if (low >= node->value) {
205 bio_write(bio, 1, 1);
210 bio_write(bio, 0, 1);
221 int tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, int leafno, int threshold) {
222 opj_tgt_node_t *stk[31];
223 opj_tgt_node_t **stkptr;
224 opj_tgt_node_t *node;
228 node = &tree->nodes[leafno];
229 while (node->parent) {
236 if (low > node->low) {
241 while (low < threshold && low < node->value) {
242 if (bio_read(bio, 1)) {
255 return (node->value < threshold) ? 1 : 0;