001/*
002 * $RCSfile: ForwWTFull.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:31 $
005 * $State: Exp $
006 *
007 * Class:                   ForwWTFull
008 *
009 * Description:             This class implements the full page
010 *                          forward wavelet transform for both integer
011 *                          and floating point implementations.
012 *
013 *
014 *
015 * COPYRIGHT:
016 *
017 * This software module was originally developed by Raphaël Grosbois and
018 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
019 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
020 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
021 * Centre France S.A) in the course of development of the JPEG2000
022 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
023 * software module is an implementation of a part of the JPEG 2000
024 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
025 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
026 * Partners) agree not to assert against ISO/IEC and users of the JPEG
027 * 2000 Standard (Users) any of their rights under the copyright, not
028 * including other intellectual property rights, for this software module
029 * with respect to the usage by ISO/IEC and Users of this software module
030 * or modifications thereof for use in hardware or software products
031 * claiming conformance to the JPEG 2000 Standard. Those intending to use
032 * this software module in hardware or software products are advised that
033 * their use may infringe existing patents. The original developers of
034 * this software module, JJ2000 Partners and ISO/IEC assume no liability
035 * for use of this software module or modifications thereof. No license
036 * or right to this software module is granted for non JPEG 2000 Standard
037 * conforming products. JJ2000 Partners have full right to use this
038 * software module for his/her own purpose, assign or donate this
039 * software module to any third party and to inhibit third parties from
040 * using this software module for non JPEG 2000 Standard conforming
041 * products. This copyright notice must be included in all copies or
042 * derivative works of this software module.
043 *
044 * Copyright (c) 1999/2000 JJ2000 Partners.
045 * */
046package jj2000.j2k.wavelet.analysis;
047import java.awt.Point;
048
049import jj2000.j2k.codestream.*;
050import jj2000.j2k.entropy.*;
051import jj2000.j2k.wavelet.*;
052//import jj2000.j2k.encoder.*;
053import jj2000.j2k.image.*;
054import jj2000.j2k.util.*;
055import jj2000.j2k.*;
056
057import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
058/**
059 * This class implements the ForwardWT with the full-page approach to be used
060 * either with integer or floating-point filters
061 * */
062public class ForwWTFull extends ForwardWT {
063
064    /** Boolean to know if one are currently dealing with int or float
065        data. */
066    private boolean intData;
067
068    /**
069     * The subband trees for each component, in each tile. The array is
070     * allocated by the constructor of this class. This array is updated by
071     * the getSubbandTree() method, an a as needed basis. The first index is
072     * the tile index (in lexicographical order) and the second index is the
073     * component index.
074     *
075     * <P>The subband tree for a component in the current tile is created on
076     * the first call to getSubbandTree() for that component, in the current
077     * tile. Before that the element in 'subbTrees' is null.
078     * */
079    private SubbandAn subbTrees[][];
080
081    /** The source of image data */
082    private BlkImgDataSrc src;
083
084    /** The horizontal coordinate of the code-block partition origin on the
085        reference grid */
086    private int cb0x;
087
088    /** The vertical coordinate of the code-block partition on the reference
089        grid */
090    private int cb0y;
091
092    /** The number of decomposition levels specification */
093    private IntegerSpec dls;
094
095    /** Wavelet filters for all components and tiles */
096    private AnWTFilterSpec filters;
097
098    /** The code-block size specifications */
099    private CBlkSizeSpec cblks;
100
101    /** The precinct partition specifications */
102    private PrecinctSizeSpec pss;
103
104    /** Block storing the full band decomposition for each component. */
105    private DataBlk decomposedComps[];
106
107    /**
108     * The horizontal index of the last code-block "sent" in the current
109     * subband in each component. It should be -1 if none have been sent yet.
110     * */
111    private int lastn[];
112
113    /**
114     * The vertical index of the last code-block "sent" in the current subband
115     * in each component. It should be 0 if none have been sent yet.
116     * */
117    private int lastm[];
118
119    /** The subband being dealt with in each component */
120    SubbandAn currentSubband[];
121
122    /** Cache object to avoid excessive allocation/deallocation. This variable
123     * makes the class inheritently thread unsafe. */
124    Point ncblks;
125
126    /**
127     * Initializes this object with the given source of image data and with
128     * all the decompositon parameters
129     *
130     * @param src From where the image data should be obtained.
131     *
132     * @param encSpec The encoder specifications
133     *
134     * @param pox The horizontal coordinate of the cell and code-block
135     * partition origin with respect to the canvas origin, on the reference
136     * grid.
137     *
138     * @param poy The vertical coordinate of the cell and code-block partition
139     * origin with respect to the canvas origin, on the reference grid.
140     *
141     * @see ForwardWT
142     * */
143    public ForwWTFull(BlkImgDataSrc src,J2KImageWriteParamJava wp,int pox,int poy) {
144        super(src);
145        this.src  = src;
146
147        this.cb0x  = cb0x;
148        this.cb0y  = cb0y;
149        this.dls  = wp.getDecompositionLevel();
150        this.filters = wp.getFilters();
151        this.cblks = wp.getCodeBlockSize();
152        this.pss = wp.getPrecinctPartition();
153
154        int ncomp = src.getNumComps();
155        int ntiles = src.getNumTiles();
156
157        currentSubband = new SubbandAn[ncomp];
158        decomposedComps = new DataBlk[ncomp];
159        subbTrees = new SubbandAn[ntiles][ncomp];
160        lastn = new int[ncomp];
161        lastm = new int[ncomp];
162    }
163
164    /**
165     * Returns the implementation type of this wavelet transform, WT_IMPL_FULL
166     * (full-page based transform). All components return the same.
167     *
168     * @param c The index of the component.
169     *
170     * @return WT_IMPL_FULL
171     * */
172    public int getImplementationType(int c) {
173        return WaveletTransform.WT_IMPL_FULL;
174    }
175
176    /**
177     * Returns the number of decomposition levels that are applied to the LL
178     * band, in the specified tile-component. A value of 0 means that no
179     * wavelet transform is applied.
180     *
181     * @param t The tile index
182     *
183     * @param c The index of the component.
184     *
185     * @return The number of decompositions applied to the LL band (0 for no
186     * wavelet transform).
187     * */
188    public int getDecompLevels(int t,int c) {
189        return ((Integer)dls.getTileCompVal(t,c)).intValue();
190    }
191
192    /**
193     * Returns the wavelet tree decomposition. Actually JPEG 2000 part 1 only
194     * supports WT_DECOMP_DYADIC decomposition.
195     *
196     * @param t The tile-index
197     *
198     * @param c The index of the component.
199     *
200     * @return The wavelet decomposition.
201     * */
202    public int getDecomp(int t,int c) {
203        return WT_DECOMP_DYADIC;
204    }
205
206    /**
207     * Returns the horizontal analysis wavelet filters used in each level, for
208     * the specified component and tile. The first element in the array is the
209     * filter used to obtain the lowest resolution (resolution level 0)
210     * subbands (i.e. lowest frequency LL subband), the second element is the
211     * one used to generate the resolution level 1 subbands, and so on. If
212     * there are less elements in the array than the number of resolution
213     * levels, then the last one is assumed to repeat itself.
214     *
215     * <P>The returned filters are applicable only to the specified component
216     * and in the current tile.
217     *
218     * <P>The resolution level of a subband is the resolution level to which a
219     * subband contributes, which is different from its decomposition level.
220     *
221     * @param t The index of the tile for which to return the filters.
222     *
223     * @param c The index of the component for which to return the filters.
224     *
225     * @return The horizontal analysis wavelet filters used in each level.
226     * */
227    public AnWTFilter[] getHorAnWaveletFilters(int t,int c) {
228        return filters.getHFilters(t,c);
229    }
230
231    /**
232     * Returns the vertical analysis wavelet filters used in each level, for
233     * the specified component and tile. The first element in the array is the
234     * filter used to obtain the lowest resolution (resolution level 0)
235     * subbands (i.e. lowest frequency LL subband), the second element is the
236     * one used to generate the resolution level 1 subbands, and so on. If
237     * there are less elements in the array than the number of resolution
238     * levels, then the last one is assumed to repeat itself.
239     *
240     * <P>The returned filters are applicable only to the specified component
241     * and in the current tile.
242     *
243     * <P>The resolution level of a subband is the resolution level to which a
244     * subband contributes, which is different from its decomposition level.
245     *
246     * @param t The index of the tile for which to return the filters.
247     *
248     * @param c The index of the component for which to return the filters.
249     *
250     * @return The vertical analysis wavelet filters used in each level.
251     * */
252    public AnWTFilter[] getVertAnWaveletFilters(int t,int c) {
253        return filters.getVFilters(t,c);
254    }
255
256    /**
257     * Returns the reversibility of the wavelet transform for the specified
258     * component and tile. A wavelet transform is reversible when it is
259     * suitable for lossless and lossy-to-lossless compression.
260     *
261     * @param t The index of the tile.
262     *
263     * @param c The index of the component.
264     *
265     * @return true is the wavelet transform is reversible, false if not.
266     * */
267    public boolean isReversible(int t,int c) {
268        return filters.isReversible(t,c);
269    }
270
271    /**
272     * Returns the horizontal offset of the code-block partition. Allowable
273     * values are 0 and 1, nothing else.
274     * */
275    public int getCbULX() {
276        return cb0x;
277    }
278
279    /**
280     * Returns the vertical offset of the code-block partition. Allowable
281     * values are 0 and 1, nothing else.
282     * */
283    public int getCbULY() {
284        return cb0y;
285    }
286
287    /**
288     * Returns the position of the fixed point in the specified
289     * component. This is the position of the least significant integral
290     * (i.e. non-fractional) bit, which is equivalent to the number of
291     * fractional bits. For instance, for fixed-point values with 2 fractional
292     * bits, 2 is returned. For floating-point data this value does not apply
293     * and 0 should be returned. Position 0 is the position of the least
294     * significant bit in the data.
295     *
296     * @param c The index of the component.
297     *
298     * @return The position of the fixed-point, which is the same as the
299     * number of fractional bits. For floating-point data 0 is returned.
300     * */
301    public int getFixedPoint(int c) {
302        return src.getFixedPoint(c);
303    }
304
305
306    /**
307     * Returns the next code-block in the current tile for the specified
308     * component. The order in which code-blocks are returned is not
309     * specified. However each code-block is returned only once and all
310     * code-blocks will be returned if the method is called 'N' times, where
311     * 'N' is the number of code-blocks in the tile. After all the code-blocks
312     * have been returned for the current tile calls to this method will
313     * return 'null'.
314     *
315     * <p>When changing the current tile (through 'setTile()' or 'nextTile()')
316     * this method will always return the first code-block, as if this method
317     * was never called before for the new current tile.</p>
318     *
319     * <p>The data returned by this method is the data in the internal buffer
320     * of this object, and thus can not be modified by the caller. The
321     * 'offset' and 'scanw' of the returned data have, in general, some
322     * non-zero value. The 'magbits' of the returned data is not set by this
323     * method and should be ignored. See the 'CBlkWTData' class.</p>
324     *
325     * <p>The 'ulx' and 'uly' members of the returned 'CBlkWTData' object
326     * contain the coordinates of the top-left corner of the block, with
327     * respect to the tile, not the subband.</p>
328     *
329     * @param c The component for which to return the next code-block.
330     *
331     * @param cblk If non-null this object will be used to return the new
332     * code-block. If null a new one will be allocated and returned.
333     *
334     * @return The next code-block in the current tile for component 'n', or
335     * null if all code-blocks for the current tile have been returned.
336     *
337     * @see CBlkWTData
338     * */
339    public CBlkWTData getNextInternCodeBlock(int c, CBlkWTData cblk) {
340        int cbm,cbn,cn,cm;
341        int acb0x, acb0y;
342        SubbandAn sb;
343        intData = (filters.getWTDataType(tIdx,c)==DataBlk.TYPE_INT);
344
345        //If the source image has not been decomposed 
346        if(decomposedComps[c]==null) {
347            int k,w,h;
348            DataBlk bufblk;
349            Object dst_data;
350
351            w = getTileCompWidth(tIdx,c);
352            h = getTileCompHeight(tIdx,c);
353
354            //Get the source image data
355            if(intData) {
356                decomposedComps[c] = new DataBlkInt(0,0,w,h);
357                bufblk = new DataBlkInt();
358            } else {
359                decomposedComps[c] = new DataBlkFloat(0,0,w,h);
360                bufblk = new DataBlkFloat();
361            }
362
363            // Get data from source line by line (this diminishes the memory
364            // requirements on the data source)
365            dst_data = decomposedComps[c].getData();
366            int lstart = getCompULX(c);
367            bufblk.ulx = lstart;
368            bufblk.w = w;
369            bufblk.h = 1;
370            int kk = getCompULY(c);
371            for (k=0; k<h; k++,kk++) {
372                bufblk.uly = kk;
373                bufblk.ulx = lstart;
374                bufblk = src.getInternCompData(bufblk,c);
375                System.arraycopy(bufblk.getData(),bufblk.offset,
376                                 dst_data,k*w,w);
377            }
378
379            //Decompose source image
380            waveletTreeDecomposition(decomposedComps[c],
381                                     getAnSubbandTree(tIdx,c),c);
382
383            // Make the first subband the current one
384            currentSubband[c] = getNextSubband(c);
385
386            lastn[c] = -1;
387            lastm[c] = 0;
388        }
389
390        // Get the next code-block to "send"
391        do {
392            // Calculate number of code-blocks in current subband
393            ncblks = currentSubband[c].numCb;
394            // Goto next code-block
395            lastn[c]++;
396            if (lastn[c] == ncblks.x) { // Got to end of this row of
397                // code-blocks
398                lastn[c] = 0;
399                lastm[c]++;
400            }
401            if (lastm[c] < ncblks.y) {
402                // Not past the last code-block in the subband, we can return
403                // this code-block
404                break;
405            }
406            // If we get here we already sent all code-blocks in this subband,
407            // goto next subband
408            currentSubband[c] = getNextSubband(c);
409            lastn[c] = -1;
410            lastm[c] = 0;
411            if ( currentSubband[c] == null ) {
412                // We don't need the transformed data any more (a priori)
413                decomposedComps[c] = null;
414                // All code-blocks from all subbands in the current
415                // tile have been returned so we return a null
416                // reference
417                return null;
418            }
419            // Loop to find the next code-block
420        } while (true);
421
422
423        // Project code-block partition origin to subband. Since the origin is
424        // always 0 or 1, it projects to the low-pass side (throught the ceil
425        // operator) as itself (i.e. no change) and to the high-pass side
426        // (through the floor operator) as 0, always.
427        acb0x = cb0x;
428        acb0y = cb0y;
429        switch (currentSubband[c].sbandIdx) {
430        case Subband.WT_ORIENT_LL:
431            // No need to project since all low-pass => nothing to do
432            break;
433        case Subband.WT_ORIENT_HL:
434            acb0x = 0;
435            break;
436        case Subband.WT_ORIENT_LH:
437            acb0y = 0;
438            break;
439        case Subband.WT_ORIENT_HH:
440            acb0x = 0;
441            acb0y = 0;
442            break;
443        default:
444            throw new Error("Internal JJ2000 error");
445        }
446        // Initialize output code-block
447        if (cblk==null) {
448            if (intData) {
449                cblk = new CBlkWTDataInt();
450            } else {
451                cblk = new CBlkWTDataFloat();
452            }
453        }
454        cbn = lastn[c];
455        cbm = lastm[c];
456        sb = currentSubband[c];
457        cblk.n = cbn;
458        cblk.m = cbm;
459        cblk.sb = sb;
460        // Calculate the indexes of first code-block in subband with respect
461        // to the partitioning origin, to then calculate the position and size
462        // NOTE: when calculating "floor()" by integer division the dividend
463        // and divisor must be positive, we ensure that by adding the divisor
464        // to the dividend and then substracting 1 to the result of the
465        // division
466        cn = (sb.ulcx-acb0x+sb.nomCBlkW)/sb.nomCBlkW-1;
467        cm = (sb.ulcy-acb0y+sb.nomCBlkH)/sb.nomCBlkH-1;
468        if (cbn == 0) { // Left-most code-block, starts where subband starts
469            cblk.ulx = sb.ulx;
470        } else {
471            // Calculate starting canvas coordinate and convert to subb. coords
472            cblk.ulx = (cn+cbn)*sb.nomCBlkW - (sb.ulcx-acb0x) + sb.ulx;
473        }
474        if (cbm == 0) { // Bottom-most code-block, starts where subband starts
475            cblk.uly = sb.uly;
476        } else {
477            cblk.uly = (cm+cbm)*sb.nomCBlkH - (sb.ulcy-acb0y) + sb.uly;
478        }
479        if (cbn < ncblks.x-1) {
480            // Calculate where next code-block starts => width
481            cblk.w = (cn+cbn+1)*sb.nomCBlkW - (sb.ulcx-acb0x) + sb.ulx -
482                cblk.ulx;
483        } else { // Right-most code-block, ends where subband ends
484            cblk.w = sb.ulx+sb.w-cblk.ulx;
485        }
486        if (cbm < ncblks.y-1) {
487            // Calculate where next code-block starts => height
488            cblk.h = (cm+cbm+1)*sb.nomCBlkH - (sb.ulcy-acb0y) + sb.uly -
489                cblk.uly;
490        } else { // Bottom-most code-block, ends where subband ends
491            cblk.h = sb.uly+sb.h-cblk.uly;
492        }
493        cblk.wmseScaling = 1f;
494
495        // Since we are in getNextInternCodeBlock() we can return a
496        // reference to the internal buffer, no need to copy. Just initialize
497        // the 'offset' and 'scanw'
498        cblk.offset = cblk.uly*decomposedComps[c].w+cblk.ulx;
499        cblk.scanw = decomposedComps[c].w;
500
501        // For the data just put a reference to our buffer
502        cblk.setData(decomposedComps[c].getData());
503        // Return code-block
504        return cblk;
505    }
506
507    /**
508     * Returns the next code-block in the current tile for the specified
509     * component, as a copy (see below). The order in which code-blocks are
510     * returned is not specified. However each code-block is returned only
511     * once and all code-blocks will be returned if the method is called 'N'
512     * times, where 'N' is the number of code-blocks in the tile. After all
513     * the code-blocks have been returned for the current tile calls to this
514     * method will return 'null'.
515     *
516     * <P>When changing the current tile (through 'setTile()' or 'nextTile()')
517     * this method will always return the first code-block, as if this method
518     * was never called before for the new current tile.
519     *
520     * <P>The data returned by this method is always a copy of the internal
521     * data of this object, and it can be modified "in place" without
522     * any problems after being returned. The 'offset' of the returned data is
523     * 0, and the 'scanw' is the same as the code-block width.  The 'magbits'
524     * of the returned data is not set by this method and should be
525     * ignored. See the 'CBlkWTData' class.
526     *
527     * <P>The 'ulx' and 'uly' members of the returned 'CBlkWTData' object
528     * contain the coordinates of the top-left corner of the block, with
529     * respect to the tile, not the subband.
530     *
531     * @param c The component for which to return the next code-block.
532     *
533     * @param cblk If non-null this object will be used to return the new
534     * code-block. If null a new one will be allocated and returned. If the
535     * "data" array of the object is non-null it will be reused, if possible,
536     * to return the data.
537     *
538     * @return The next code-block in the current tile for component 'c', or
539     * null if all code-blocks for the current tile have been returned.
540     *
541     * @see CBlkWTData
542     * */
543    public CBlkWTData getNextCodeBlock(int c, CBlkWTData cblk) {
544        // We can not directly use getNextInternCodeBlock() since that returns
545        // a reference to the internal buffer, we have to copy that data
546
547        int j,k;
548        int w;
549        Object dst_data; // a int[] or float[] object
550        int[] dst_data_int;
551        float[] dst_data_float;
552        Object src_data; // a int[] or float[] object
553
554        intData = (filters.getWTDataType(tIdx,c)==DataBlk.TYPE_INT);
555
556        dst_data = null;
557
558        // Cache the data array, if any
559        if (cblk != null) {
560            dst_data = cblk.getData();
561        }
562
563        // Get the next code-block
564        cblk = getNextInternCodeBlock(c,cblk);
565
566        if (cblk == null) {
567            return null; // No more code-blocks in current tile for component
568            // c
569        }
570
571        // Ensure size of output buffer
572        if (intData) { // int data
573            dst_data_int = (int[]) dst_data;
574            if (dst_data_int == null || dst_data_int.length < cblk.w*cblk.h) {
575                dst_data = new int[cblk.w*cblk.h];
576            }
577        }
578        else { // float data
579            dst_data_float = (float[]) dst_data;
580            if (dst_data_float == null ||
581                dst_data_float.length < cblk.w*cblk.h) {
582                dst_data = new float[cblk.w*cblk.h];
583            }
584        }
585
586        // Copy data line by line
587        src_data = cblk.getData();
588        w = cblk.w;
589        for (j = w*(cblk.h-1), k = cblk.offset+(cblk.h-1)*cblk.scanw;
590             j >= 0; j -= w, k -= cblk.scanw) {
591            System.arraycopy(src_data,k,dst_data,j,w);
592        }
593        cblk.setData(dst_data);
594        cblk.offset = 0;
595        cblk.scanw = w;
596
597        return cblk;
598    }
599
600    /**
601     * Return the data type of this CBlkWTDataSrc. Its value should be either
602     * DataBlk.TYPE_INT or DataBlk.TYPE_FLOAT but can change according to the
603     * current tile-component.
604     *
605     * @param t The index of the tile for which to return the data type.
606     *
607     * @param c The index of the component for which to return the data type.
608     *
609     * @return Current data type
610     * */
611    public int getDataType(int t,int c){
612        return filters.getWTDataType(t,c);
613    }
614
615    /**
616     * Returns the next subband that will be used to get the next code-block
617     * to return by the getNext[Intern]CodeBlock method.
618     *
619     * @param c The component
620     *
621     * @return Its returns the next subband that will be used to get the next
622     * code-block to return by the getNext[Intern]CodeBlock method.
623     **/
624    private SubbandAn getNextSubband(int c) {
625        int down = 1;
626        int up = 0;
627        int direction = down;
628        SubbandAn nextsb;
629
630        nextsb = currentSubband[c];
631        //If it is the first call to this method
632        if(nextsb == null) {
633            nextsb = this.getAnSubbandTree(tIdx,c);
634            //If there is no decomposition level then send the whole image
635            if(!nextsb.isNode) {
636                return nextsb;
637            }
638        }
639
640        //Find the next subband to send
641        do {
642            //If the current subband is null then break
643            if(nextsb == null) {
644                break;
645            }
646
647            //If the current subband is a leaf then select the next leaf to
648            //send or go up in the decomposition tree if the leaf was a LL
649            //one.
650            else if(!nextsb.isNode) {
651                switch (nextsb.orientation) {
652                    case Subband.WT_ORIENT_HH :
653                        nextsb = (SubbandAn)nextsb.getParent().getLH();
654                        direction = down;
655                        break;
656                    case Subband.WT_ORIENT_LH :
657                        nextsb = (SubbandAn)nextsb.getParent().getHL();
658                        direction = down;
659                        break;
660                    case Subband.WT_ORIENT_HL :
661                        nextsb = (SubbandAn)nextsb.getParent().getLL();
662                        direction = down;
663                        break;
664                    case Subband.WT_ORIENT_LL :
665                        nextsb = (SubbandAn)nextsb.getParent();
666                        direction = up;
667                        break;
668                }
669            }
670
671            //Else if the current subband is a node
672            else if(nextsb.isNode) {
673                //If the direction is down the select the HH subband of the
674                //current node.
675                if(direction == down) {
676                    nextsb = (SubbandAn)nextsb.getHH();
677                }
678                //Else the direction is up the select the next node to cover
679                //or still go up in the decomposition tree if the node is a LL
680                //subband
681                else if(direction == up) {
682                    switch (nextsb.orientation) {
683                        case Subband.WT_ORIENT_HH :
684                            nextsb = (SubbandAn)nextsb.getParent().getLH();
685                            direction = down;
686                            break;
687                        case Subband.WT_ORIENT_LH :
688                            nextsb = (SubbandAn)nextsb.getParent().getHL();
689                            direction = down;
690                            break;
691                        case Subband.WT_ORIENT_HL :
692                            nextsb = (SubbandAn)nextsb.getParent().getLL();
693                            direction = down;
694                            break;
695                        case Subband.WT_ORIENT_LL :
696                            nextsb = (SubbandAn)nextsb.getParent();
697                            direction = up;
698                            break;
699                    }
700                }
701            }
702
703            if(nextsb == null) {
704                break;
705            }
706        } while(nextsb.isNode);
707        return nextsb;
708    }
709
710    /**
711     * Performs the forward wavelet transform on the whole band. It
712     * iteratively decomposes the subbands from the top node to the leaves.
713     *
714     * @param band The band containing the float data to decompose
715     *
716     * @param subband The structure containing the coordinates of the current
717     * subband in the whole band to decompose.
718     *
719     * @param c The index of the current component to decompose
720     * */
721    private void waveletTreeDecomposition(DataBlk band,
722                                          SubbandAn subband, int c) {
723
724        //If the current subband is a leaf then nothing to be done (a leaf is
725        //not decomposed).
726        if(!subband.isNode)
727            return;
728
729        else {
730            //Perform the 2D wavelet decomposition of the current subband
731            wavelet2DDecomposition(band, (SubbandAn)subband, c);
732
733            //Perform the decomposition of the four resulting subbands
734            waveletTreeDecomposition(band, (SubbandAn)subband.getHH(), c);
735            waveletTreeDecomposition(band, (SubbandAn)subband.getLH(), c);
736            waveletTreeDecomposition(band, (SubbandAn)subband.getHL(), c);
737            waveletTreeDecomposition(band, (SubbandAn)subband.getLL(), c);
738        }
739    }
740
741    /**
742     * Performs the 2D forward wavelet transform on a subband of the initial
743     * band. This method will successively perform 1D filtering steps on all
744     * lines and then all columns of the subband. In this class only filters
745     * with floating point implementations can be used.
746     *
747     * @param band The band containing the float data to decompose
748     *
749     * @param subband The structure containing the coordinates of the subband
750     * in the whole band to decompose.
751     *
752     * @param c The index of the current component to decompose
753     * */
754    private void wavelet2DDecomposition(DataBlk band,
755        SubbandAn subband, int c) {
756
757        int ulx, uly, w, h;
758        int band_w, band_h;
759
760        // If subband is empty (i.e. zero size) nothing to do
761        if (subband.w == 0 || subband.h == 0) {
762            return;
763        }
764
765        ulx = subband.ulx;
766        uly = subband.uly;
767        w = subband.w;
768        h = subband.h;
769        band_w = getTileCompWidth(tIdx, c);
770        band_h = getTileCompHeight(tIdx, c);
771
772        if ( intData ) {
773            //Perform the decompositions if the filter is implemented with an
774            //integer arithmetic.
775            int i, j;
776            int offset;
777            int[] tmpVector = new int[java.lang.Math.max(w,h)];
778
779            int[] data = ((DataBlkInt)band).getDataInt();
780
781            //Perform the vertical decomposition
782            if (subband.ulcy%2==0) { // Even start index => use LPF
783                for(j=0; j<w; j++) {
784                    offset = uly*band_w + ulx+j;
785                    for(i=0; i<h; i++)
786                        tmpVector[i] = data[offset+(i*band_w)];
787                    subband.vFilter.analyze_lpf(tmpVector, 0, h, 1,
788                                             data, offset, band_w,
789                                             data, offset+((h+1)/2)*band_w,
790                                             band_w);
791                }
792            }
793            else { // Odd start index => use HPF
794                for(j=0; j<w; j++) {
795                    offset = uly*band_w + ulx+j;
796                    for(i=0; i<h; i++)
797                        tmpVector[i] = data[offset+(i*band_w)];
798                    subband.vFilter.analyze_hpf(tmpVector, 0, h, 1,
799                                             data, offset, band_w,
800                                             data, offset+(h/2)*band_w,
801                                             band_w);
802                }
803            }
804
805            //Perform the horizontal decomposition.
806            if (subband.ulcx%2==0) { // Even start index => use LPF
807                for(i=0; i<h; i++) {
808                    offset = (uly+i)*band_w + ulx;
809                    for(j=0; j<w; j++)
810                        tmpVector[j] = data[offset+j];
811                    subband.hFilter.analyze_lpf(tmpVector, 0, w, 1,
812                                             data, offset, 1,
813                                             data, offset+(w+1)/2, 1);
814                }
815            }
816            else { // Odd start index => use HPF
817                for(i=0; i<h; i++) {
818                    offset = (uly+i)*band_w + ulx;
819                    for(j=0; j<w; j++)
820                        tmpVector[j] = data[offset+j];
821                    subband.hFilter.analyze_hpf(tmpVector, 0, w, 1,
822                                             data, offset, 1,
823                                             data, offset+w/2, 1);
824                }
825            }
826        }
827        else {
828            //Perform the decompositions if the filter is implemented with a
829            //float arithmetic.
830            int i, j;
831            int offset;
832            float[] tmpVector = new float[java.lang.Math.max(w,h)];
833            float[]data = ((DataBlkFloat)band).getDataFloat();
834
835            //Perform the vertical decomposition.
836            if (subband.ulcy%2==0) { // Even start index => use LPF
837                for(j=0; j<w; j++) {
838                    offset = uly*band_w + ulx+j;
839                    for(i=0; i<h; i++)
840                        tmpVector[i] = data[offset+(i*band_w)];
841                    subband.vFilter.analyze_lpf(tmpVector, 0, h, 1,
842                                             data, offset, band_w,
843                                             data, offset+((h+1)/2)*band_w,
844                                             band_w);
845                }
846            }
847            else { // Odd start index => use HPF
848                for(j=0; j<w; j++) {
849                    offset = uly*band_w + ulx+j;
850                    for(i=0; i<h; i++)
851                        tmpVector[i] = data[offset+(i*band_w)];
852                    subband.vFilter.analyze_hpf(tmpVector, 0, h, 1,
853                                             data, offset, band_w,
854                                             data, offset+(h/2)*band_w,
855                                             band_w);
856                }
857            }
858            //Perform the horizontal decomposition.
859            if (subband.ulcx%2==0) { // Even start index => use LPF
860                for(i=0; i<h; i++) {
861                    offset = (uly+i)*band_w + ulx;
862                    for(j=0; j<w; j++)
863                        tmpVector[j] = data[offset+j];
864                    subband.hFilter.analyze_lpf(tmpVector, 0, w, 1,
865                                             data, offset, 1,
866                                             data, offset+(w+1)/2, 1);
867                }
868            }
869            else { // Odd start index => use HPF
870                for(i=0; i<h; i++) {
871                    offset = (uly+i)*band_w + ulx;
872                    for(j=0; j<w; j++)
873                        tmpVector[j] = data[offset+j];
874                    subband.hFilter.analyze_hpf(tmpVector, 0, w, 1,
875                                             data, offset, 1,
876                                             data, offset+w/2, 1);
877                }
878            }
879        }
880    }
881
882    /**
883     * Changes the current tile, given the new coordinates.
884     *
885     * <P>This method resets the 'subbTrees' array, and recalculates the
886     * values of the 'reversible' array. It also resets the decomposed
887     * component buffers.
888     *
889     * @param x The horizontal coordinate of the tile.
890     *
891     * @param y The vertical coordinate of the new tile.
892     * */
893    public void setTile(int x, int y) {
894        int i;
895
896        // Change tile
897        super.setTile(x,y);
898
899        // Reset the decomposed component buffers.
900        if (decomposedComps != null) {
901            for (i=decomposedComps.length-1; i>=0; i--) {
902                decomposedComps[i] = null;
903                currentSubband[i] = null;
904            }
905        }
906
907    }
908
909    /**
910     * Advances to the next tile, in standard scan-line order (by rows then
911     * columns). An NoNextElementException is thrown if the current tile is
912     * the last one (i.e. there is no next tile).
913     *
914     * <P>This method resets the 'subbTrees' array, and recalculates the
915     * values of the 'reversible' array. It also resets the decomposed
916     * component buffers.
917     * */
918    public void nextTile() {
919        int i;
920
921        // Change tile
922        super.nextTile();
923        // Reset the decomposed component buffers
924        if (decomposedComps != null) {
925            for (i=decomposedComps.length-1; i>=0; i--) {
926                decomposedComps[i] = null;
927                currentSubband[i] = null;
928            }
929        }
930
931    }
932
933    /**
934     * Returns a reference to the subband tree structure representing the
935     * subband decomposition for the specified tile-component of the source.
936     *
937     * @param t The index of the tile.
938     *
939     * @param c The index of the component.
940     *
941     * @return The subband tree structure, see Subband.
942     *
943     * @see SubbandAn
944     * @see Subband
945     * */
946    public SubbandAn getAnSubbandTree(int t,int c) {
947        if (subbTrees[t][c] == null) {
948            subbTrees[t][c] =
949                new SubbandAn(getTileCompWidth(tIdx, c),getTileCompHeight(tIdx, c),
950                              getCompULX(c),getCompULY(c),
951                              getDecompLevels(t,c),
952                              getHorAnWaveletFilters(t,c),
953                              getVertAnWaveletFilters(t,c));
954            initSubbandsFields(t,c,subbTrees[t][c]);
955        }
956        return subbTrees[t][c];
957    }
958
959    /** 
960     * Initialises subbands fields, such as number of code-blocks and
961     * code-blocks dimension, in the subband tree. The nominal code-block
962     * width/height depends on the precincts dimensions if used.
963     *
964     * @param t The tile index of the subband
965     *
966     * @param c The component index
967     *
968     * @param sb The subband tree to be initialised.
969     * */
970    private void initSubbandsFields(int t,int c,Subband sb) {
971        int cbw = cblks.getCBlkWidth(ModuleSpec.SPEC_TILE_COMP,t,c);
972        int cbh = cblks.getCBlkHeight(ModuleSpec.SPEC_TILE_COMP,t,c);
973
974        if (!sb.isNode) {
975            // Code-blocks dimension
976            int ppx, ppy;
977            int ppxExp, ppyExp, cbwExp, cbhExp;
978            ppx = pss.getPPX(t,c,sb.resLvl);
979            ppy = pss.getPPY(t,c,sb.resLvl);
980
981            if (ppx!=Markers.PRECINCT_PARTITION_DEF_SIZE
982                 || ppy!=Markers.PRECINCT_PARTITION_DEF_SIZE ) {
983
984                ppxExp = MathUtil.log2(ppx);
985                ppyExp = MathUtil.log2(ppy);
986                cbwExp = MathUtil.log2(cbw);
987                cbhExp = MathUtil.log2(cbh);
988
989                // Precinct partition is used
990                switch (sb.resLvl) {
991                    case 0:
992                        sb.nomCBlkW = ( cbwExp<ppxExp ?
993                            (1<<cbwExp) : (1<<ppxExp) );
994                        sb.nomCBlkH = ( cbhExp<ppyExp ?
995                            (1<<cbhExp) : (1<<ppyExp) );
996                        break;
997
998                    default:
999                        sb.nomCBlkW = ( cbwExp<ppxExp-1 ?
1000                            (1<<cbwExp) : (1<<(ppxExp-1)) );
1001                        sb.nomCBlkH = ( cbhExp<ppyExp-1 ?
1002                            (1<<cbhExp) : (1<<(ppyExp-1)) );
1003                        break;
1004                }
1005            } else {
1006                sb.nomCBlkW = cbw;
1007                sb.nomCBlkH = cbh;
1008            }
1009
1010            // Number of code-blocks
1011            if(sb.numCb==null) sb.numCb = new Point();
1012            if(sb.w!=0 && sb.h!=0) {
1013                int acb0x = cb0x;
1014                int acb0y = cb0y;
1015                int tmp;
1016
1017                // Project code-block partition origin to subband. Since the
1018                // origin is always 0 or 1, it projects to the low-pass side
1019                // (throught the ceil operator) as itself (i.e. no change) and
1020                // to the high-pass side (through the floor operator) as 0,
1021                // always.
1022                switch (sb.sbandIdx) {
1023                case Subband.WT_ORIENT_LL:
1024                    // No need to project since all low-pass => nothing to do
1025                    break;
1026                case Subband.WT_ORIENT_HL:
1027                    acb0x = 0;
1028                    break;
1029                case Subband.WT_ORIENT_LH:
1030                    acb0y = 0;
1031                    break;
1032                case Subband.WT_ORIENT_HH:
1033                    acb0x = 0;
1034                    acb0y = 0;
1035                    break;
1036                default:
1037                    throw new Error("Internal JJ2000 error");
1038                }
1039                if(sb.ulcx-acb0x<0 || sb.ulcy-acb0y<0) {
1040                    throw new IllegalArgumentException("Invalid code-blocks "+
1041                                                       "partition origin or "+
1042                                                       "image offset in the "+
1043                                                       "reference grid.");
1044                }
1045                // NOTE: when calculating "floor()" by integer division the
1046                // dividend and divisor must be positive, we ensure that by
1047                // adding the divisor to the dividend and then substracting 1
1048                // to the result of the division
1049                tmp = sb.ulcx-acb0x+sb.nomCBlkW;
1050                sb.numCb.x = (tmp+sb.w-1)/sb.nomCBlkW - (tmp/sb.nomCBlkW-1);
1051                tmp = sb.ulcy-acb0y+sb.nomCBlkH;
1052                sb.numCb.y = (tmp+sb.h-1)/sb.nomCBlkH - (tmp/sb.nomCBlkH-1);
1053            } else {
1054                sb.numCb.x = sb.numCb.y = 0;
1055            }
1056        } else {
1057            initSubbandsFields(t,c,sb.getLL());
1058            initSubbandsFields(t,c,sb.getHL());
1059            initSubbandsFields(t,c,sb.getLH());
1060            initSubbandsFields(t,c,sb.getHH());
1061        }
1062    }
1063
1064}