001/* 002 * $RCSfile: TagTreeDecoder.java,v $ 003 * $Revision: 1.1 $ 004 * $Date: 2005/02/11 05:02:02 $ 005 * $State: Exp $ 006 * 007 * Class: TagTreeDecoder 008 * 009 * Description: Decoder of tag trees 010 * 011 * 012 * 013 * COPYRIGHT: 014 * 015 * This software module was originally developed by Raphaël Grosbois and 016 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel 017 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David 018 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research 019 * Centre France S.A) in the course of development of the JPEG2000 020 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This 021 * software module is an implementation of a part of the JPEG 2000 022 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio 023 * Systems AB and Canon Research Centre France S.A (collectively JJ2000 024 * Partners) agree not to assert against ISO/IEC and users of the JPEG 025 * 2000 Standard (Users) any of their rights under the copyright, not 026 * including other intellectual property rights, for this software module 027 * with respect to the usage by ISO/IEC and Users of this software module 028 * or modifications thereof for use in hardware or software products 029 * claiming conformance to the JPEG 2000 Standard. Those intending to use 030 * this software module in hardware or software products are advised that 031 * their use may infringe existing patents. The original developers of 032 * this software module, JJ2000 Partners and ISO/IEC assume no liability 033 * for use of this software module or modifications thereof. No license 034 * or right to this software module is granted for non JPEG 2000 Standard 035 * conforming products. JJ2000 Partners have full right to use this 036 * software module for his/her own purpose, assign or donate this 037 * software module to any third party and to inhibit third parties from 038 * using this software module for non JPEG 2000 Standard conforming 039 * products. This copyright notice must be included in all copies or 040 * derivative works of this software module. 041 * 042 * Copyright (c) 1999/2000 JJ2000 Partners. 043 * 044 * 045 * 046 */ 047 048 049package jj2000.j2k.codestream.reader; 050 051import jj2000.j2k.io.*; 052import jj2000.j2k.util.*; 053import java.io.*; 054 055/** 056 * This class implements the tag tree decoder. A tag tree codes a 2D 057 * matrix of integer elements in an efficient way. The decoding 058 * procedure 'update()' updates a value of the matrix from a stream of 059 * coded data, given a threshold. This procedure decodes enough 060 * information to identify whether or not the value is greater than 061 * or equal to the threshold, and updates the value accordingly. 062 * 063 * <P>In general the decoding procedure must follow the same sequence 064 * of elements and thresholds as the encoding one. The encoder is 065 * implemented by the TagTreeEncoder class. 066 * 067 * <P>Tag trees that have one dimension, or both, as 0 are allowed for 068 * convenience. Of course no values can be set or coded in such cases. 069 * 070 * @see jj2000.j2k.codestream.writer.TagTreeEncoder 071 * */ 072public class TagTreeDecoder { 073 074 /** The horizontal dimension of the base level */ 075 protected int w; 076 077 /** The vertical dimensions of the base level */ 078 protected int h; 079 080 /** The number of levels in the tag tree */ 081 protected int lvls; 082 083 /** The tag tree values. The first index is the level, 084 * starting at level 0 (leafs). The second index is the element 085 * within the level, in lexicographical order. */ 086 protected int treeV[][]; 087 088 /** The tag tree state. The first index is the level, starting at 089 * level 0 (leafs). The second index is the element within the 090 * level, in lexicographical order. */ 091 protected int treeS[][]; 092 093 /** 094 * Creates a tag tree decoder with 'w' elements along the 095 * horizontal dimension and 'h' elements along the vertical 096 * direction. The total number of elements is thus 'vdim' x 097 * 'hdim'. 098 * 099 * <P>The values of all elements are initialized to 100 * Integer.MAX_VALUE (i.e. no information decoded so far). The 101 * states are initialized all to 0. 102 * 103 * @param h The number of elements along the vertical direction. 104 * 105 * @param w The number of elements along the horizontal direction. 106 * 107 * 108 * */ 109 public TagTreeDecoder(int h, int w) { 110 int i; 111 112 // Check arguments 113 if ( w < 0 || h < 0 ) { 114 throw new IllegalArgumentException(); 115 } 116 // Initialize dimensions 117 this.w = w; 118 this.h = h; 119 // Calculate the number of levels 120 if (w == 0 || h == 0) { 121 lvls = 0; // Empty tree 122 } 123 else { 124 lvls = 1; 125 while (h != 1 || w != 1) { // Loop until we reach root 126 w = (w+1)>>1; 127 h = (h+1)>>1; 128 lvls++; 129 } 130 } 131 // Allocate tree values and states 132 treeV = new int[lvls][]; 133 treeS = new int[lvls][]; 134 w = this.w; 135 h = this.h; 136 for (i=0; i<lvls; i++) { 137 treeV[i] = new int[h*w]; 138 // Initialize to infinite value 139 ArrayUtil.intArraySet(treeV[i],Integer.MAX_VALUE); 140 141 // (no need to initialize to 0 since it's the default) 142 treeS[i] = new int[h*w]; 143 w = (w+1)>>1; 144 h = (h+1)>>1; 145 } 146 } 147 148 /** 149 * Returns the number of leafs along the horizontal direction. 150 * 151 * @return The number of leafs along the horizontal direction. 152 * 153 * 154 * */ 155 public final int getWidth() { 156 return w; 157 } 158 159 /** 160 * Returns the number of leafs along the vertical direction. 161 * 162 * @return The number of leafs along the vertical direction. 163 * 164 * 165 * */ 166 public final int getHeight() { 167 return h; 168 } 169 170 /** 171 * Decodes information for the specified element of the tree, 172 * given the threshold, and updates its value. The information 173 * that can be decoded is whether or not the value of the element 174 * is greater than, or equal to, the value of the 175 * threshold. 176 * 177 * @param m The vertical index of the element. 178 * 179 * @param n The horizontal index of the element. 180 * 181 * @param t The threshold to use in decoding. It must be non-negative. 182 * 183 * @param in The stream from where to read the coded information. 184 * 185 * @return The updated value at position (m,n). 186 * 187 * @exception IOException If an I/O error occurs while reading 188 * from 'in'. 189 * 190 * @exception EOFException If the ned of the 'in' stream is 191 * reached before getting all the necessary data. 192 * 193 * 194 * */ 195 public int update(int m, int n, int t, PktHeaderBitReader in) 196 throws IOException { 197 int k,tmin; 198 int idx,ts,tv; 199 200 // Check arguments 201 if (m >= h || n >= w || t < 0) { 202 throw new IllegalArgumentException(); 203 } 204 205 // Initialize 206 k = lvls-1; 207 tmin = treeS[k][0]; 208 209 // Loop on levels 210 idx = (m>>k)*((w+(1<<k)-1)>>k)+(n>>k); 211 while (true) { 212 // Cache state and value 213 ts = treeS[k][idx]; 214 tv = treeV[k][idx]; 215 if (ts < tmin) { 216 ts = tmin; 217 } 218 while (t > ts) { 219 if (tv >= ts) { // We are not done yet 220 if (in.readBit() == 0) { // '0' bit 221 // We know that 'value' > treeS[k][idx] 222 ts++; 223 } 224 else { // '1' bit 225 // We know that 'value' = treeS[k][idx] 226 tv = ts++; 227 } 228 // Increment of treeS[k][idx] done above 229 } 230 else { // We are done, we can set ts and get out 231 ts = t; 232 break; // get out of this while 233 } 234 } 235 // Update state and value 236 treeS[k][idx] = ts; 237 treeV[k][idx] = tv; 238 // Update tmin or terminate 239 if (k>0) { 240 tmin = ts < tv ? ts : tv; 241 k--; 242 // Index of element for next iteration 243 idx = (m>>k)*((w+(1<<k)-1)>>k)+(n>>k); 244 } 245 else { 246 // Return the updated value 247 return tv; 248 } 249 } 250 } 251 252 /** 253 * Returns the current value of the specified element in the tag 254 * tree. This is the value as last updated by the update() method. 255 * 256 * @param m The vertical index of the element. 257 * 258 * @param n The horizontal index of the element. 259 * 260 * @return The current value of the element. 261 * 262 * @see #update 263 * 264 * 265 * */ 266 public int getValue(int m, int n) { 267 // Check arguments 268 if (m >= h || n >= w) { 269 throw new IllegalArgumentException(); 270 } 271 // Return value 272 return treeV[0][m*w+n]; 273 } 274}