001/* 002 * $RCSfile: BitstreamReaderAgent.java,v $ 003 * $Revision: 1.1 $ 004 * $Date: 2005/02/11 05:02:00 $ 005 * $State: Exp $ 006 * 007 * Class: BitstreamReaderAgent 008 * 009 * Description: The generic interface for bit stream 010 * transport agents. 011 * 012 * 013 * 014 * COPYRIGHT: 015 * 016 * This software module was originally developed by Raphaël Grosbois and 017 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel 018 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David 019 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research 020 * Centre France S.A) in the course of development of the JPEG2000 021 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This 022 * software module is an implementation of a part of the JPEG 2000 023 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio 024 * Systems AB and Canon Research Centre France S.A (collectively JJ2000 025 * Partners) agree not to assert against ISO/IEC and users of the JPEG 026 * 2000 Standard (Users) any of their rights under the copyright, not 027 * including other intellectual property rights, for this software module 028 * with respect to the usage by ISO/IEC and Users of this software module 029 * or modifications thereof for use in hardware or software products 030 * claiming conformance to the JPEG 2000 Standard. Those intending to use 031 * this software module in hardware or software products are advised that 032 * their use may infringe existing patents. The original developers of 033 * this software module, JJ2000 Partners and ISO/IEC assume no liability 034 * for use of this software module or modifications thereof. No license 035 * or right to this software module is granted for non JPEG 2000 Standard 036 * conforming products. JJ2000 Partners have full right to use this 037 * software module for his/her own purpose, assign or donate this 038 * software module to any third party and to inhibit third parties from 039 * using this software module for non JPEG 2000 Standard conforming 040 * products. This copyright notice must be included in all copies or 041 * derivative works of this software module. 042 * 043 * Copyright (c) 1999/2000 JJ2000 Partners. 044 * */ 045package jj2000.j2k.codestream.reader; 046import java.awt.Point; 047import java.io.IOException; 048 049import jj2000.j2k.ModuleSpec; 050import jj2000.j2k.codestream.HeaderInfo; 051import jj2000.j2k.decoder.DecoderSpecs; 052import jj2000.j2k.entropy.decoder.CodedCBlkDataSrcDec; 053import jj2000.j2k.io.RandomAccessIO; 054import jj2000.j2k.quantization.dequantizer.StdDequantizerParams; 055import jj2000.j2k.util.MathUtil; 056import jj2000.j2k.wavelet.Subband; 057import jj2000.j2k.wavelet.synthesis.SubbandSyn; 058 059import com.github.jaiimageio.jpeg2000.impl.J2KImageReadParamJava; 060 061/** 062 * This is the generic interface for bit stream reader agents. A bit stream 063 * reader agent is an entity that allows reading from a bit stream and 064 * requesting compressed code-blocks. It can be a simple file reader, or a 065 * network connection, or anything else. 066 * 067 * <P>The bit stream reader agent allows to make request for compressed block 068 * data in any order. The amount of data returned would normally depend on the 069 * data available at the time of the request, be it from a file or from a 070 * network connection. 071 * 072 * <P>The bit stream reader agent has the notion of a current tile, and 073 * coordinates are relative to the current tile, where applicable. 074 * 075 * <P>Resolution level 0 is the lowest resolution level, i.e. the LL subband 076 * alone. 077 * */ 078public abstract class BitstreamReaderAgent implements CodedCBlkDataSrcDec { 079 080 /** The decoder specifications */ 081 protected DecoderSpecs decSpec; 082 083 /** 084 * Whether or not the components in the current tile uses a derived 085 * quantization step size (only relevant in non reversible quantization 086 * mode). This field is actualized by the setTile method in 087 * FileBitstreamReaderAgent. 088 * 089 * @see FileBitstreamReaderAgent#initSubbandsFields 090 * */ 091 protected boolean derived[] = null; 092 093 /** 094 * Number of guard bits off all component in the current tile. This field 095 * is actualized by the setTile method in FileBitstreamReaderAgent. 096 * 097 * @see FileBitstreamReaderAgent#initSubbandsFields 098 * */ 099 protected int[] gb = null; 100 101 /** 102 * Dequantization parameters of all subbands and all components in the 103 * current tile. The value is actualized by the setTile method in 104 * FileBitstreamReaderAgent. 105 * 106 * @see FileBitstreamReaderAgent#initSubbandsFields 107 * */ 108 protected StdDequantizerParams params[] = null; 109 110 /** The prefix for bit stream reader options: 'B' */ 111 public final static char OPT_PREFIX = 'B'; 112 113 /** The list of parameters that is accepted by the bit stream 114 * readers. They start with 'B'. */ 115 private static final String [][] pinfo = null; 116 117 /** 118 * The maximum number of decompostion levels for each component of the 119 * current tile. It means that component c has mdl[c]+1 resolution levels 120 * (indexed from 0 to mdl[c]) 121 * */ 122 protected int mdl[]; 123 124 /** The number of components */ 125 protected final int nc; 126 127 /** Image resolution level to generate */ 128 protected int targetRes; 129 130 /** 131 * The subband trees for each component in the current tile. Each element 132 * in the array is the root element of the subband tree for a 133 * component. The number of magnitude bits in each subband (magBits member 134 * variable) is not initialized. 135 * */ 136 protected SubbandSyn subbTrees[]; 137 138 /** The image width on the hi-res reference grid */ 139 protected final int imgW; 140 141 /** The image width on the hi-res reference grid */ 142 protected final int imgH; 143 144 /** The horizontal coordinate of the image origin in the canvas system, on 145 * the reference grid. */ 146 protected final int ax; 147 148 /** The vertical coordinate of the image origin in the canvas system, on 149 * the reference grid. */ 150 protected final int ay; 151 152 /** The horizontal coordinate of the tiling origin in the canvas system, on 153 * the reference grid. */ 154 protected final int px; 155 156 /** The vertical coordinate of the tiling origin in the canvas system, on 157 * the reference grid. */ 158 protected final int py; 159 160 /** The horizontal offsets of the upper-left corner of the current tile 161 * (not active tile) with respect to the canvas origin, in the component 162 * hi-res grid, for each component. */ 163 protected final int offX[]; 164 165 /** The vertical offsets of the upper-left corner of the current tile (not 166 * active tile) with respect to the canvas origin, in the component hi-res 167 * grid, for each component. */ 168 protected final int offY[]; 169 170 /** The horizontal coordinates of the upper-left corner of the active 171 * tile, with respect to the canvas origin, in the component hi-res grid, 172 * for each component. */ 173 protected final int culx[]; 174 175 /** The vertical coordinates of the upper-left corner of the active tile, 176 * with respect to the canvas origin, in the component hi-res grid, for 177 * each component. */ 178 protected final int culy[]; 179 180 /** The nominal tile width, in the hi-res reference grid */ 181 protected final int ntW; 182 183 /** The nominal tile height, in the hi-res reference grid */ 184 protected final int ntH; 185 186 /** The number of tile in the horizontal direction */ 187 protected final int ntX; 188 189 /** The number of tiles in the vertical direction */ 190 protected final int ntY; 191 192 /** The total number of tiles. */ 193 protected final int nt; 194 195 /** The current tile horizontal index */ 196 protected int ctX; 197 198 /** The current tile vertical index */ 199 protected int ctY; 200 201 /** The decoded bit stream header */ 202 protected final HeaderDecoder hd; 203 204 /** Number of bytes targeted to be read */ 205 protected int tnbytes; 206 207 /** Actual number of read bytes */ 208 protected int anbytes; 209 210 /** Target decoding rate in bpp */ 211 protected float trate; 212 213 /** Actual decoding rate in bpp */ 214 protected float arate; 215 216 /** 217 * Initializes members of this class. This constructor takes a 218 * HeaderDecoder object. This object must be initialized by the 219 * constructor of the implementing class from the header of the bit 220 * stream. 221 * 222 * @param hd The decoded header of the bit stream from where to initialize 223 * the values. 224 * 225 * @param decSpec The decoder specifications 226 * */ 227 protected BitstreamReaderAgent(HeaderDecoder hd,DecoderSpecs decSpec){ 228 Point co; 229 int i,j,max; 230 231 this.decSpec = decSpec; 232 this.hd = hd; 233 234 // Number of components 235 nc = hd.getNumComps(); 236 offX = new int[nc]; 237 offY = new int[nc]; 238 culx = new int[nc]; 239 culy = new int[nc]; 240 241 // Image size and origin 242 imgW = hd.getImgWidth(); 243 imgH = hd.getImgHeight(); 244 ax = hd.getImgULX(); 245 ay = hd.getImgULY(); 246 247 // Tiles 248 co = hd.getTilingOrigin(null); 249 px = co.x; 250 py = co.y; 251 ntW = hd.getNomTileWidth(); 252 ntH = hd.getNomTileHeight(); 253 ntX = (ax+imgW-px+ntW-1) / ntW; 254 ntY = (ay+imgH-py+ntH-1) / ntH; 255 nt = ntX * ntY; 256 } 257 258 /** 259 * Returns the vertical code-block partition origin. Allowable values are 260 * 0 and 1, nothing else. 261 * */ 262 public final int getCbULX() { 263 return hd.getCbULX(); 264 } 265 266 /** 267 * Returns the vertical code-block partition origin. Allowable values are 268 * 0 and 1, nothing else. 269 * */ 270 public int getCbULY() { 271 return hd.getCbULY(); 272 } 273 274 /** 275 * Returns the number of components in the image. 276 * 277 * @return The number of components in the image. 278 * */ 279 public final int getNumComps() { 280 return nc; 281 } 282 283 /** 284 * Returns the component subsampling factor in the horizontal direction, 285 * for the specified component. This is, approximately, the ratio of 286 * dimensions between the reference grid and the component itself, see the 287 * 'ImgData' interface desription for details. 288 * 289 * @param c The index of the component (between 0 and N-1) 290 * 291 * @return The horizontal subsampling factor of component 'c' 292 * 293 * @see jj2000.j2k.image.ImgData 294 * */ 295 public final int getCompSubsX(int c) { 296 return hd.getCompSubsX(c); 297 } 298 299 /** 300 * Returns the component subsampling factor in the vertical direction, for 301 * the specified component. This is, approximately, the ratio of 302 * dimensions between the reference grid and the component itself, see the 303 * 'ImgData' interface desription for details. 304 * 305 * @param c The index of the component (between 0 and C-1) 306 * 307 * @return The vertical subsampling factor of component 'c' 308 * 309 * @see jj2000.j2k.image.ImgData 310 * */ 311 public int getCompSubsY(int c) { 312 return hd.getCompSubsY(c); 313 } 314 315 /** 316 * Returns the overall width of the current tile in pixels for the given 317 * (tile) resolution level. This is the tile's width without accounting 318 * for any component subsampling. 319 * 320 * <P>Note: Tile resolution level indexes may be different from 321 * tile-component resolution index. They are indeed indexed starting from 322 * the lowest number of decomposition levels of each component of the 323 * tile. 324 * 325 * <P>For an image (1 tile) with 2 components (component 0 having 2 326 * decomposition levels and component 1 having 3 decomposition levels), 327 * the first (tile-)component has 3 resolution levels and the second one 328 * has 4 resolution levels, whereas the tile has only 3 resolution levels 329 * available. 330 * 331 * @param rl The (tile) resolution level. 332 * 333 * @return The current tile's width in pixels. 334 * */ 335 public int getTileWidth(int rl){ 336 // The minumum number of decomposition levels between all the 337 // components 338 int mindl = decSpec.dls.getMinInTile(getTileIdx()); 339 if(rl>mindl){ 340 throw new IllegalArgumentException("Requested resolution level"+ 341 " is not available for, at "+ 342 "least, one component in "+ 343 "tile: "+ctX+"x"+ctY); 344 } 345 int ctulx,ntulx; 346 int dl = mindl-rl; // Number of decomposition to obtain this 347 // resolution 348 349 // Calculate starting X of current tile at hi-res 350 ctulx = (ctX == 0) ? ax : px+ctX*ntW; 351 // Calculate starting X of next tile X-wise at hi-res 352 ntulx = (ctX < ntX-1) ? px+(ctX+1)*ntW : ax+imgW; 353 dl = 1 << dl; 354 // The difference at the rl resolution level is the width 355 return (ntulx+dl-1)/dl-(ctulx+dl-1)/dl; 356 } 357 358 /** 359 * Returns the overall height of the current tile in pixels, for the given 360 * resolution level. This is the tile's height without accounting for any 361 * component subsampling. 362 * 363 * <P>Note: Tile resolution level indexes may be different from 364 * tile-component resolution index. They are indeed indexed starting from 365 * the lowest number of decomposition levels of each component of the 366 * tile. 367 * 368 * <P>For an image (1 tile) with 2 components (component 0 having 2 369 * decomposition levels and component 1 having 3 decomposition levels), 370 * the first (tile-)component has 3 resolution levels and the second one 371 * has 4 resolution levels, whereas the tile has only 3 resolution levels 372 * available. 373 * 374 * @param rl The (tile) resolution level. 375 * 376 * @return The total current tile's height in pixels. 377 * */ 378 public int getTileHeight(int rl){ 379 // The minumum number of decomposition levels between all the 380 // components 381 int mindl = decSpec.dls.getMinInTile(getTileIdx()); 382 if(rl>mindl){ 383 throw new IllegalArgumentException("Requested resolution level"+ 384 " is not available for, at "+ 385 "least, one component in"+ 386 " tile: "+ctX+"x"+ctY); 387 } 388 389 int ctuly,ntuly; 390 int dl = mindl-rl; // Number of decomposition to obtain this 391 // resolution 392 393 // Calculate starting Y of current tile at hi-res 394 ctuly = (ctY == 0) ? ay : py+ctY*ntH; 395 // Calculate starting Y of next tile Y-wise at hi-res 396 ntuly = (ctY < ntY-1) ? py+(ctY+1)*ntH : ay+imgH; 397 dl = 1 <<dl; 398 // The difference at the rl level is the height 399 return (ntuly+dl-1)/dl-(ctuly+dl-1)/dl; 400 } 401 402 /** 403 * Returns the overall width of the image in pixels, for the given (image) 404 * resolution level. This is the image's width without accounting for any 405 * component subsampling or tiling. 406 * 407 * <P>Note: Image resolution level indexes may differ from tile-component 408 * resolution index. They are indeed indexed starting from the lowest 409 * number of decomposition levels of each component of each tile. 410 * 411 * <P>Example: For an image (1 tile) with 2 components (component 0 having 412 * 2 decomposition levels and component 1 having 3 decomposition levels), 413 * the first (tile-) component has 3 resolution levels and the second one 414 * has 4 resolution levels, whereas the image has only 3 resolution levels 415 * available. 416 * 417 * @param rl The image resolution level. 418 * 419 * @return The total image's width in pixels. 420 * */ 421 public int getImgWidth(int rl){ 422 // The minimum number of decomposition levels of each 423 // tile-component 424 int mindl = decSpec.dls.getMin(); 425 if(rl>mindl){ 426 throw new IllegalArgumentException("Requested resolution level"+ 427 " is not available for, at "+ 428 "least, one tile-component"); 429 } 430 // Retrieve number of decomposition levels corresponding to 431 // this resolution level 432 int dl = 1 << mindl - rl; 433 return (ax+imgW+dl-1)/dl-(ax+dl-1)/dl; 434 } 435 436 /** 437 * Returns the overall height of the image in pixels, for the given 438 * resolution level. This is the image's height without accounting for any 439 * component subsampling or tiling. 440 * 441 * <P>Note: Image resolution level indexes may differ from tile-component 442 * resolution index. They are indeed indexed starting from the lowest 443 * number of decomposition levels of each component of each tile. 444 * 445 * <P>Example: For an image (1 tile) with 2 components (component 0 having 446 * 2 decomposition levels and component 1 having 3 decomposition levels), 447 * the first (tile-) component has 3 resolution levels and the second one 448 * has 4 resolution levels, whereas the image has only 3 resolution levels 449 * available. 450 * 451 * @param rl The image resolution level, from 0 to L. 452 * 453 * @return The total image's height in pixels. 454 * */ 455 public int getImgHeight(int rl){ 456 int mindl = decSpec.dls.getMin(); 457 if(rl>mindl){ 458 throw new IllegalArgumentException("Requested resolution level"+ 459 " is not available for, at "+ 460 "least, one tile-component"); 461 } 462 // Retrieve number of decomposition levels corresponding to this 463 // resolution level 464 int dl = 1 << mindl - rl; 465 return (ay+imgH+dl-1)/dl-(ay+dl-1)/dl; 466 } 467 468 /** 469 * Returns the horizontal coordinate of the image origin, the top-left 470 * corner, in the canvas system, on the reference grid at the specified 471 * resolution level. 472 * 473 * <P>Note: Image resolution level indexes may differ from tile-component 474 * resolution index. They are indeed indexed starting from the lowest 475 * number of decomposition levels of each component of each tile. 476 * 477 * <P>Example: For an image (1 tile) with 2 components (component 0 having 478 * 2 decomposition levels and component 1 having 3 decomposition levels), 479 * the first (tile-) component has 3 resolution levels and the second one 480 * has 4 resolution levels, whereas the image has only 3 resolution levels 481 * available. 482 * 483 * @param rl The resolution level, from 0 to L. 484 * 485 * @return The horizontal coordinate of the image origin in the canvas 486 * system, on the reference grid. 487 * */ 488 public int getImgULX(int rl){ 489 int mindl = decSpec.dls.getMin(); 490 if(rl>mindl){ 491 throw new IllegalArgumentException("Requested resolution level"+ 492 " is not available for, at "+ 493 "least, one tile-component"); 494 } 495 // Retrieve number of decomposition levels corresponding to this 496 // resolution level 497 int dl = 1 << mindl - rl; 498 return (ax+dl-1)/dl; 499 } 500 501 /** 502 * Returns the vertical coordinate of the image origin, the top-left 503 * corner, in the canvas system, on the reference grid at the specified 504 * resolution level. 505 * 506 * <P>Note: Image resolution level indexes may differ from tile-component 507 * resolution index. They are indeed indexed starting from the lowest 508 * number of decomposition levels of each component of each tile. 509 * 510 * <P>Example: For an image (1 tile) with 2 components (component 0 having 511 * 2 decomposition levels and component 1 having 3 decomposition levels), 512 * the first (tile-) component has 3 resolution levels and the second one 513 * has 4 resolution levels, whereas the image has only 3 resolution levels 514 * available. 515 * 516 * @param rl The resolution level, from 0 to L. 517 * 518 * @return The vertical coordinate of the image origin in the canvas 519 * system, on the reference grid. 520 * */ 521 public int getImgULY(int rl){ 522 int mindl = decSpec.dls.getMin(); 523 if(rl>mindl){ 524 throw new IllegalArgumentException("Requested resolution level"+ 525 " is not available for, at "+ 526 "least, one tile-component"); 527 } 528 // Retrieve number of decomposition levels corresponding to this 529 // resolution level 530 int dl = 1 << mindl - rl; 531 return (ay+dl-1)/dl; 532 } 533 534 /** 535 * Returns the width in pixels of the specified tile-component for the 536 * given (tile-component) resolution level. 537 * 538 * @param t The tile index 539 * 540 * @param c The index of the component, from 0 to N-1. 541 * 542 * @param rl The resolution level, from 0 to L. 543 * 544 * @return The width in pixels of component <tt>c</tt> in tile <tt>t</tt> 545 * for resolution level <tt>rl</tt>. 546 * */ 547 public final int getTileCompWidth(int t,int c,int rl) { 548 int tIdx = getTileIdx(); 549 if(t!=tIdx) { 550 throw new Error("Asking the tile-component width of a tile "+ 551 "different from the current one."); 552 } 553 // Calculate starting X of next tile X-wise at reference grid hi-res 554 int ntulx = (ctX < ntX-1) ? px+(ctX+1)*ntW : ax+imgW; 555 // Convert reference grid hi-res to component grid hi-res 556 ntulx = (ntulx+hd.getCompSubsX(c)-1)/hd.getCompSubsX(c); 557 int dl = 1 << mdl[c]-rl; 558 // Starting X of current tile at component grid hi-res is culx[c] 559 // The difference at the rl level is the width 560 return (ntulx+dl-1)/dl-(culx[c]+dl-1)/dl; 561 } 562 563 /** 564 * Returns the height in pixels of the specified tile-component for the 565 * given (tile-component) resolution level. 566 * 567 * @param t The tile index. 568 * 569 * @param c The index of the component, from 0 to N-1. 570 * 571 * @param rl The resolution level, from 0 to L. 572 * 573 * @return The height in pixels of component <tt>c</tt> in the current 574 * tile. 575 * */ 576 public final int getTileCompHeight(int t,int c,int rl) { 577 int tIdx = getTileIdx(); 578 if(t!=tIdx) { 579 throw new Error("Asking the tile-component width of a tile "+ 580 "different from the current one."); 581 } 582 // Calculate starting Y of next tile Y-wise at reference grid hi-res 583 int ntuly = (ctY < ntY-1) ? py+(ctY+1)*ntH : ay+imgH; 584 // Convert reference grid hi-res to component grid hi-res 585 ntuly = (ntuly+hd.getCompSubsY(c)-1)/hd.getCompSubsY(c); 586 int dl = 1 << mdl[c]-rl; // Revert level indexation (0 is hi-res) 587 // Starting Y of current tile at component grid hi-res is culy[c] 588 // The difference at the rl level is the height 589 return (ntuly+dl-1)/dl-(culy[c]+dl-1)/dl; 590 } 591 592 593 /** 594 * Returns the width in pixels of the specified component in the overall 595 * image, for the given (component) resolution level. 596 * 597 * <P>Note: Component resolution level indexes may differ from 598 * tile-component resolution index. They are indeed indexed starting from 599 * the lowest number of decomposition levels of same component of each 600 * tile. 601 * 602 * <P>Example: For an image (2 tiles) with 1 component (tile 0 having 2 603 * decomposition levels and tile 1 having 3 decomposition levels), the 604 * first tile(-component) has 3 resolution levels and the second one has 4 605 * resolution levels, whereas the component has only 3 resolution levels 606 * available. 607 * 608 * @param c The index of the component, from 0 to N-1. 609 * 610 * @param rl The resolution level, from 0 to L. 611 * 612 * @return The width in pixels of component <tt>c</tt> in the overall 613 * image. 614 * */ 615 public final int getCompImgWidth(int c,int rl){ 616 // indexation (0 is hi-res) 617 // Calculate image starting x at component hi-res grid 618 int sx = (ax+hd.getCompSubsX(c)-1)/hd.getCompSubsX(c); 619 // Calculate image ending (excluding) x at component hi-res grid 620 int ex = (ax+imgW+hd.getCompSubsX(c)-1)/hd.getCompSubsX(c); 621 int dl = 1 << decSpec.dls.getMinInComp(c)-rl; 622 // The difference at the rl level is the width 623 return (ex+dl-1)/dl-(sx+dl-1)/dl; 624 } 625 626 /** 627 * Returns the height in pixels of the specified component in the overall 628 * image, for the given (component) resolution level. 629 * 630 * <P>Note: Component resolution level indexes may differ from 631 * tile-component resolution index. They are indeed indexed starting from 632 * the lowest number of decomposition levels of same component of each 633 * tile. 634 * 635 * <P>Example: For an image (2 tiles) with 1 component (tile 0 having 2 636 * decomposition levels and tile 1 having 3 decomposition levels), the 637 * first tile(-component) has 3 resolution levels and the second one has 4 638 * resolution levels, whereas the component has only 3 resolution levels 639 * available. 640 * 641 * @param c The index of the component, from 0 to N-1. 642 * 643 * @param rl The resolution level, from 0 to L. 644 * 645 * @return The height in pixels of component <tt>c</tt> in the overall 646 * image. 647 * */ 648 public final int getCompImgHeight(int c,int rl){ 649 // indexation (0 is hi-res) 650 // Calculate image starting x at component hi-res grid 651 int sy = (ay+hd.getCompSubsY(c)-1)/hd.getCompSubsY(c); 652 // Calculate image ending (excluding) x at component hi-res grid 653 int ey = (ay+imgH+hd.getCompSubsY(c)-1)/hd.getCompSubsY(c); 654 int dl = 1 << decSpec.dls.getMinInComp(c)-rl; 655 // The difference at the rl level is the width 656 return (ey+dl-1)/dl-(sy+dl-1)/dl; 657 } 658 659 /** 660 * Changes the current tile, given the new indexes. An 661 * IllegalArgumentException is thrown if the indexes do not correspond to 662 * a valid tile. 663 * 664 * @param x The horizontal indexes the tile. 665 * 666 * @param y The vertical indexes of the new tile. 667 * */ 668 public abstract void setTile(int x, int y); 669 670 /** 671 * Advances to the next tile, in standard scan-line order (by rows then 672 * columns). An NoNextElementException is thrown if the current tile is 673 * the last one (i.e. there is no next tile). 674 * */ 675 public abstract void nextTile(); 676 677 /** 678 * Returns the indexes of the current tile. These are the horizontal and 679 * vertical indexes of the current tile. 680 * 681 * @param co If not null this object is used to return the information. If 682 * null a new one is created and returned. 683 * 684 * @return The current tile's indexes (vertical and horizontal indexes). 685 * */ 686 public final Point getTile(Point co) { 687 if (co != null) { 688 co.x = ctX; 689 co.y = ctY; 690 return co; 691 } 692 else { 693 return new Point(ctX,ctY); 694 } 695 } 696 697 /** 698 * Returns the index of the current tile, relative to a standard scan-line 699 * order. 700 * 701 * @return The current tile's index (starts at 0). 702 * */ 703 public final int getTileIdx() { 704 return ctY*ntX+ctX; 705 } 706 707 /** 708 * Returns the horizontal coordinate of the upper-left corner of the 709 * specified resolution in the given component of the current tile. 710 * 711 * @param c The component index. 712 * 713 * @param rl The resolution level index. 714 * */ 715 public final int getResULX(int c,int rl) { 716 int dl = mdl[c]-rl; 717 if(dl<0){ 718 throw new IllegalArgumentException("Requested resolution level"+ 719 " is not available for, at "+ 720 "least, one component in "+ 721 "tile: "+ctX+"x"+ctY); 722 } 723 int tx0 = (int)Math.max(px+ctX*ntW,ax); 724 int tcx0 = (int)Math.ceil(tx0/(double)getCompSubsX(c)); 725 return (int)Math.ceil(tcx0/(double)(1<<dl)); 726 } 727 728 /** 729 * Returns the vertical coordinate of the upper-left corner of the 730 * specified component in the given component of the current tile. 731 * 732 * @param c The component index. 733 * 734 * @param rl The resolution level index. 735 * */ 736 public final int getResULY(int c,int rl) { 737 int dl = mdl[c]-rl; 738 if(dl<0){ 739 throw new IllegalArgumentException("Requested resolution level"+ 740 " is not available for, at "+ 741 "least, one component in "+ 742 "tile: "+ctX+"x"+ctY); 743 } 744 int ty0 = (int)Math.max(py+ctY*ntH,ay); 745 int tcy0 = (int)Math.ceil(ty0/(double)getCompSubsY(c)); 746 return (int)Math.ceil(tcy0/(double)(1<<dl)); 747 } 748 749 /** 750 * Returns the number of tiles in the horizontal and vertical directions. 751 * 752 * @param co If not null this object is used to return the information. If 753 * null a new one is created and returned. 754 * 755 * @return The number of tiles in the horizontal (Point.x) and vertical 756 * (Point.y) directions. 757 * */ 758 public final Point getNumTiles(Point co) { 759 if (co != null) { 760 co.x = ntX; 761 co.y = ntY; 762 return co; 763 } 764 else { 765 return new Point(ntX,ntY); 766 } 767 } 768 769 /** 770 * Returns the total number of tiles in the image. 771 * 772 * @return The total number of tiles in the image. 773 * */ 774 public final int getNumTiles() { 775 return ntX*ntY; 776 } 777 778 /** 779 * Returns the subband tree, for the specified tile-component. This method 780 * returns the root element of the subband tree structure, see Subband and 781 * SubbandSyn. The tree comprises all the available resolution levels. 782 * 783 * <p>Note: this method is not able to return subband tree for a tile 784 * different than the current one.</p> 785 * 786 * <p>The number of magnitude bits ('magBits' member variable) for each 787 * subband is not initialized.</p> 788 * 789 * @param t The tile index 790 * 791 * @param c The index of the component, from 0 to C-1. 792 * 793 * @return The root of the tree structure. 794 * */ 795 public final SubbandSyn getSynSubbandTree(int t,int c) { 796 if(t!=getTileIdx()) { 797 throw new IllegalArgumentException("Can not request subband"+ 798 " tree of a different tile"+ 799 " than the current one"); 800 } 801 if(c<0 || c>=nc) { 802 throw new IllegalArgumentException("Component index out of range"); 803 } 804 return subbTrees[c]; 805 } 806 807 808 /** 809 * Creates a bit stream reader of the correct type that works on the 810 * provided RandomAccessIO, with the special parameters from the parameter 811 * list. 812 * 813 * @param in The RandomAccessIO source from which to read the bit stream. 814 * 815 * @param hd Header of the codestream. 816 * 817 * @param j2krparam The parameters applicable to the 818 * bit stream read (other parameters may also be present). 819 * 820 * @param decSpec The decoder specifications 821 * 822 * @param cdstrInfo Whether or not to print information found in 823 * codestream. 824 * 825 * @param hi Reference to the HeaderInfo instance. 826 * 827 * @exception IOException If an I/O error occurs while reading initial 828 * data from the bit stream. 829 * @exception IllegalArgumentException If an unrecognised bit stream 830 * reader option is present. 831 * */ 832 public static BitstreamReaderAgent createInstance(RandomAccessIO in, 833 HeaderDecoder hd, 834 J2KImageReadParamJava j2krparam, 835 DecoderSpecs decSpec, 836 boolean cdstrInfo, 837 HeaderInfo hi) 838 throws IOException { 839 // Check header length 840/* 841 if (in.getPos() != hd.getTotalHeaderLength() + hd.initPos) { 842 throw new IllegalArgumentException("Invalid header length"); 843 } 844*/ 845 return new FileBitstreamReaderAgent(hd,in,decSpec,j2krparam,cdstrInfo,hi); 846 } 847 848 849 /** 850 * Returns the parameters that are used in this class and implementing 851 * classes. It returns a 2D String array. Each of the 1D arrays is for a 852 * different option, and they have 3 elements. The first element is the 853 * option name, the second one is the synopsis and the third one is a long 854 * description of what the parameter is. The synopsis or description may 855 * be 'null', in which case it is assumed that there is no synopsis or 856 * description of the option, respectively. Null may be returned if no 857 * options are supported. 858 * 859 * @return the options name, their synopsis and their explanation, or null 860 * if no options are supported. 861 * */ 862 public static String[][] getParameterInfo() { 863 return pinfo; 864 } 865 866 /** 867 * Returns the precinct partition width for the specified tile-component 868 * and (tile-component) resolution level. 869 * 870 * @param t the tile index 871 * 872 * @param c The index of the component (between 0 and N-1) 873 * 874 * @param rl The resolution level, from 0 to L. 875 * 876 * @return the precinct partition width for the specified component, 877 * resolution level and tile. 878 * */ 879 public final int getPPX(int t,int c,int rl){ 880 return decSpec.pss.getPPX(t,c,rl); 881 } 882 883 /** 884 * Returns the precinct partition height for the specified tile-component 885 * and (tile-component) resolution level. 886 * 887 * @param t The tile index 888 * 889 * @param c The index of the component (between 0 and N-1) 890 * 891 * @param rl The resolution level, from 0 to L. 892 * 893 * @return The precinct partition height in the specified component, for 894 * the specified resolution level, for the current tile. 895 * */ 896 public final int getPPY(int t,int c,int rl){ 897 return decSpec.pss.getPPY(t,c,rl); 898 } 899 900 /** 901 * Initialises subbands fields, such as code-blocks dimension and number 902 * of magnitude bits, in the subband tree. The nominal code-block 903 * width/height depends on the precincts dimensions if used. The way the 904 * number of magnitude bits is computed depends on the quantization type 905 * (reversible, derived, expounded). 906 * 907 * @param c The component index 908 * 909 * @param sb The subband tree to be initialised. 910 * */ 911 protected void initSubbandsFields(int c,SubbandSyn sb){ 912 int t = getTileIdx(); 913 int rl = sb.resLvl; 914 int cbw, cbh; 915 916 cbw = decSpec.cblks.getCBlkWidth(ModuleSpec.SPEC_TILE_COMP,t,c); 917 cbh = decSpec.cblks.getCBlkHeight(ModuleSpec.SPEC_TILE_COMP,t,c); 918 919 if( !sb.isNode ){ 920 if( hd.precinctPartitionUsed() ){ 921 // The precinct partition is used 922 int ppxExp, ppyExp, cbwExp, cbhExp; 923 924 // Get exponents 925 ppxExp = MathUtil.log2(getPPX(t,c,rl)); 926 ppyExp = MathUtil.log2(getPPY(t,c,rl)); 927 cbwExp = MathUtil.log2(cbw); 928 cbhExp = MathUtil.log2(cbh); 929 930 switch (sb.resLvl) { 931 case 0: 932 sb.nomCBlkW = ( cbwExp<ppxExp ? 933 (1<<cbwExp) : (1<<ppxExp) ); 934 sb.nomCBlkH = ( cbhExp<ppyExp ? 935 (1<<cbhExp) : (1<<ppyExp) ); 936 break; 937 938 default: 939 sb.nomCBlkW = ( cbwExp<ppxExp-1 ? 940 (1<<cbwExp) : (1<<(ppxExp-1)) ); 941 sb.nomCBlkH = ( cbhExp<ppyExp-1 ? 942 (1<<cbhExp) : (1<<(ppyExp-1)) ); 943 break; 944 } 945 } 946 else { 947 sb.nomCBlkW = cbw; 948 sb.nomCBlkH = cbh; 949 } 950 951 // Number of code-blocks 952 if(sb.numCb == null) sb.numCb = new Point(); 953 if (sb.w==0 || sb.h==0) { 954 sb.numCb.x = 0; 955 sb.numCb.y = 0; 956 } else { 957 int cb0x = getCbULX(); 958 int cb0y = getCbULY(); 959 int tmp; 960 961 // Projects code-block partition origin to subband. Since the 962 // origin is always 0 or 1, it projects to the low-pass side 963 // (throught the ceil operator) as itself (i.e. no change) and 964 // to the high-pass side (through the floor operator) as 0, 965 // always. 966 int acb0x = cb0x; 967 int acb0y = cb0y; 968 969 switch (sb.sbandIdx) { 970 case Subband.WT_ORIENT_LL: 971 // No need to project since all low-pass => nothing to do 972 break; 973 case Subband.WT_ORIENT_HL: 974 acb0x = 0; 975 break; 976 case Subband.WT_ORIENT_LH: 977 acb0y = 0; 978 break; 979 case Subband.WT_ORIENT_HH: 980 acb0x = 0; 981 acb0y = 0; 982 break; 983 default: 984 throw new Error("Internal JJ2000 error"); 985 } 986 if(sb.ulcx-acb0x<0 || sb.ulcy-acb0y<0) { 987 throw new IllegalArgumentException("Invalid code-blocks "+ 988 "partition origin or "+ 989 "image offset in the "+ 990 "reference grid."); 991 } 992 993 // NOTE: when calculating "floor()" by integer division the 994 // dividend and divisor must be positive, we ensure that by 995 // adding the divisor to the dividend and then substracting 1 996 // to the result of the division 997 998 tmp = sb.ulcx-acb0x+sb.nomCBlkW; 999 sb.numCb.x = (tmp+sb.w-1)/sb.nomCBlkW - (tmp/sb.nomCBlkW-1); 1000 1001 tmp = sb.ulcy-acb0y+sb.nomCBlkH; 1002 sb.numCb.y = (tmp+sb.h-1)/sb.nomCBlkH - (tmp/sb.nomCBlkH-1); 1003 } 1004 1005 if(derived[c]){ 1006 sb.magbits = gb[c]+(params[c].exp[0][0]-(mdl[c]-sb.level))-1; 1007 } 1008 else { 1009 sb.magbits = gb[c]+params[c].exp[sb.resLvl][sb.sbandIdx]-1; 1010 } 1011 } 1012 else { 1013 initSubbandsFields(c,(SubbandSyn)sb.getLL()); 1014 initSubbandsFields(c,(SubbandSyn)sb.getHL()); 1015 initSubbandsFields(c,(SubbandSyn)sb.getLH()); 1016 initSubbandsFields(c,(SubbandSyn)sb.getHH()); 1017 } 1018 } 1019 1020 /** 1021 * Returns the image resolution level to reconstruct from the 1022 * codestream. This value cannot be computed before every main and tile 1023 * headers are read. 1024 * 1025 * @return The image resolution level 1026 * */ 1027 public int getImgRes(){ 1028 return targetRes; 1029 } 1030 1031 /** 1032 * Return the target decoding rate in bits per pixel. 1033 * 1034 * @return Target decoding rate in bpp. 1035 * */ 1036 public float getTargetRate(){ 1037 return trate; 1038 } 1039 1040 /** 1041 * Return the actual decoding rate in bits per pixel. 1042 * 1043 * @return Actual decoding rate in bpp. 1044 * */ 1045 public float getActualRate() { 1046 arate = anbytes*8f/hd.getMaxCompImgWidth()/hd.getMaxCompImgHeight(); 1047 return arate; 1048 } 1049 1050 /** 1051 * Return the target number of read bytes. 1052 * 1053 * @return Target decoding rate in bytes. 1054 * */ 1055 public int getTargetNbytes(){ 1056 return tnbytes; 1057 } 1058 1059 /** 1060 * Return the actual number of read bytes. 1061 * 1062 * @return Actual decoding rate in bytes. 1063 * */ 1064 public int getActualNbytes(){ 1065 return anbytes; 1066 } 1067 1068 /** Returns the horizontal offset of tile partition */ 1069 public int getTilePartULX() { 1070 return hd.getTilingOrigin(null).x; 1071 } 1072 1073 /** Returns the vertical offset of tile partition */ 1074 public int getTilePartULY() { 1075 return hd.getTilingOrigin(null).y; 1076 } 1077 1078 /** Returns the nominal tile width */ 1079 public int getNomTileWidth() { 1080 return hd.getNomTileWidth(); 1081 } 1082 1083 /** Returns the nominal tile height */ 1084 public int getNomTileHeight() { 1085 return hd.getNomTileHeight(); 1086 } 1087}