001/*
002 * $RCSfile: SubbandAn.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:31 $
005 * $State: Exp $
006 *
007 * Class:                   SubbandAn
008 *
009 * Description:             Element for a tree structure for a descripotion
010 *                          of subbands on the anslysis side.
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 *
045 *
046 *
047 */
048
049
050package jj2000.j2k.wavelet.analysis;
051
052import jj2000.j2k.wavelet.Subband;
053import jj2000.j2k.wavelet.WaveletFilter;
054
055/**
056 * This class represents a subband in a bidirectional tree structure
057 * that describes the subband decomposition for a wavelet transform,
058 * specifically for the analysis side.
059 *
060 * <P>The element can be either a node or a leaf of the tree. If it is
061 * a node then ther are 4 descendants (LL, HL, LH and HH). If it is a
062 * leaf there are no descendants.
063 *
064 * <P>The tree is bidirectional. Each element in the tree structure
065 * has a "parent", which is the subband from which the element was
066 * obtained by decomposition. The only exception is the root element
067 * which has no parent (i.e.it's null), for obvious reasons.
068 * */
069public class SubbandAn extends Subband {
070
071    /**
072     * The reference to the parent of this subband. It is null for the
073     * root element. It is null by default.  */
074    public SubbandAn parent = null;
075
076    /**
077     * The reference to the LL subband resulting from the
078     * decomposition of this subband. It is null by default.  */
079    public SubbandAn subb_LL;
080
081    /**
082     * The reference to the HL subband (horizontal high-pass)
083     * resulting from the decomposition of this subband. It is null by
084     * default.  */
085    public SubbandAn subb_HL;
086
087    /**
088     * The reference to the LH subband (vertical high-pass) resulting
089     * from the decomposition of this subband. It is null by default.
090     * */
091    public SubbandAn subb_LH;
092
093    /**
094     * The reference to the HH subband resulting from the
095     * decomposition of this subband. It is null by default.
096     */
097    public SubbandAn subb_HH;
098
099    /** The horizontal analysis filter used to decompose this
100        subband. This is applicable to "node" elements only. The
101        default value is null. */
102    public AnWTFilter hFilter;
103
104    /** The vertical analysis filter used to decompose this
105        subband. This is applicable to "node" elements only. The
106        default value is null. */
107    public AnWTFilter vFilter;
108
109    /**
110     * The L2-norm of the synthesis basis waveform of this subband,
111     * applicable to "leafs" only. By default it is -1 (i.e. not
112     * calculated yet).
113     * */
114    public float l2Norm = -1.0f;
115
116    /**
117     * The contribution to the MSE or WMSE error that would result in the
118     * image if there was an error of exactly one quantization step size in
119     * the sample of the subband. This value is expressed relative to a
120     * nominal dynamic range in the image domain of exactly 1.0. This field
121     * contains valid data only after quantization 9See Quantizer).
122     *
123     * @see jj2000.j2k.quantization.quantizer.Quantizer
124     * */
125    public float stepWMSE;
126
127    /**
128     * Creates a SubbandAn element with all the default values. The
129     * dimensions are (0,0) and the upper left corner is (0,0).
130     *
131     *
132     * */
133    public SubbandAn() {
134    }
135
136    /**
137     * Creates the top-level node and the entire subband tree, with
138     * the top-level dimensions, the number of decompositions, and the
139     * decomposition tree as specified.
140     *
141     * <P>This constructor just calls the same constructor of the
142     * super class, and then calculates the L2-norm (or energy weight)
143     * of each leaf.
144     *
145     * <P>This constructor does not initialize the value of the magBits or
146     * stepWMSE member variables. This variables are normally initialized by
147     * the quantizer (see Quantizer).
148     *
149     * @param w The top-level width
150     *
151     * @param h The top-level height
152     *
153     * @param ulcx The horizontal coordinate of the upper-left corner with
154     * respect to the canvas origin, in the component grid.
155     *
156     * @param ulcy The vertical  coordinate of the upper-left corner with
157     * respect to the canvas origin, in the component grid.
158     *
159     * @param lvls The number of levels (or LL decompositions) in the
160     * tree.
161     *
162     * @param hfilters The horizontal wavelet analysis filters for each
163     * resolution level, starting at resolution level 0.
164     *
165     * @param vfilters The vertical wavelet analysis filters for each
166     * resolution level, starting at resolution level 0.
167     *
168     * @see Subband#Subband(int,int,int,int,int,
169     * WaveletFilter[],WaveletFilter[])
170     *
171     * @see jj2000.j2k.quantization.quantizer.Quantizer
172     *
173     *
174     * */
175    public SubbandAn(int w, int h, int ulcx, int ulcy, int lvls,
176                     WaveletFilter hfilters[], WaveletFilter vfilters[]) {
177        super(w,h,ulcx,ulcy,lvls,hfilters,vfilters);
178        // Caculate the L2-norms
179        calcL2Norms();
180    }
181
182    /**
183     * Returns the parent of this subband. The parent of a subband is
184     * the subband from which this one was obtained by
185     * decomposition. The root element has no parent subband (null).
186     *
187     * @return The parent subband, or null for the root one.
188     *
189     *
190     * */
191    public Subband getParent() {
192        return parent;
193    }
194
195    /**
196     * Returns the LL child subband of this subband.
197     *
198     * @return The LL child subband, or null if there are no childs.
199     *
200     *
201     * */
202    public Subband getLL() {
203        return subb_LL;
204    }
205
206    /**
207     * Returns the HL (horizontal high-pass) child subband of this
208     * subband.
209     *
210     * @return The HL child subband, or null if there are no childs.
211     *
212     *
213     * */
214    public Subband getHL() {
215        return subb_HL;
216    }
217
218    /**
219     * Returns the LH (vertical high-pass) child subband of this
220     * subband.
221     *
222     * @return The LH child subband, or null if there are no childs.
223     *
224     *
225     * */
226    public Subband getLH() {
227        return subb_LH;
228    }
229
230    /**
231     * Returns the HH child subband of this subband.
232     *
233     * @return The HH child subband, or null if there are no childs.
234     *
235     *
236     * */
237    public Subband getHH() {
238        return subb_HH;
239    }
240
241    /**
242     * Splits the current subband in its four subbands. It changes the
243     * status of this element (from a leaf to a node, and sets the
244     * filters), creates the childs and initializes them. An
245     * IllegalArgumentException is thrown if this subband is not a
246     * leaf.
247     *
248     * <P>It uses the initChilds() method to initialize the childs.
249     *
250     * @param hfilter The horizontal wavelet filter used to decompose
251     * this subband. It has to be a AnWTFilter object.
252     *
253     * @param vfilter The vertical wavelet filter used to decompose this
254     * subband. It has to be a AnWTFilter object.
255     *
256     * @return  A reference to the LL leaf (subb_LL).
257     *
258     * @see Subband#initChilds
259     *
260     *
261     * */
262    protected Subband split(WaveletFilter hfilter, WaveletFilter vfilter) {
263        // Test that this is a node
264        if (isNode) {
265            throw new IllegalArgumentException();
266        }
267
268        // Modify this element into a node and set the filters
269        isNode = true;
270        this.hFilter = (AnWTFilter) hfilter;
271        this.vFilter = (AnWTFilter) vfilter;
272
273        // Create childs
274        subb_LL = new SubbandAn();
275        subb_LH = new SubbandAn();
276        subb_HL = new SubbandAn();
277        subb_HH = new SubbandAn();
278
279        // Assign parent
280        subb_LL.parent = this;
281        subb_HL.parent = this;
282        subb_LH.parent = this;
283        subb_HH.parent = this;
284
285        // Initialize childs
286        initChilds();
287
288        // Return reference to LL subband
289        return subb_LL;
290    }
291
292    /**
293     * Calculates the basis waveform of the first leaf for which the
294     * L2-norm has not been calculated yet. This method searches
295     * recursively for the first leaf for which the value has not been
296     * calculated yet, and then calculates the L2-norm on the return
297     * path.
298     *
299     * <P>The wfs argument should be a size 2 array of float arrays
300     * (i.e. 2D array) and it must be of length 2 (or more). When
301     * returning, wfs[0] will contain the line waveform, and wfs[1]
302     * will contain the column waveform.
303     *
304     * <P>This method can not be called on an element that ahs a
305     * non-negative value in l2Norm, since that means that we are
306     * done.
307     *
308     * @param wfs An size 2 array where the line and column waveforms
309     * will be returned.
310     *
311     *
312     * */
313    private void calcBasisWaveForms(float wfs[][]) {
314        if (l2Norm < 0) {
315            // We are not finished with this element yet
316            if (isNode) {
317                // We are on a node => search on childs
318                if (subb_LL.l2Norm < 0f) {
319                    subb_LL.calcBasisWaveForms(wfs);
320                    wfs[0] =
321                        hFilter.getLPSynWaveForm(wfs[0],null);
322                    wfs[1] =
323                        vFilter.getLPSynWaveForm(wfs[1],null);
324                }
325                else if (subb_HL.l2Norm < 0f) {
326                    subb_HL.calcBasisWaveForms(wfs);
327                    wfs[0] =
328                        hFilter.getHPSynWaveForm(wfs[0],null);
329                    wfs[1] =
330                        vFilter.getLPSynWaveForm(wfs[1],null);
331                }
332                else if (subb_LH.l2Norm < 0f) {
333                    subb_LH.calcBasisWaveForms(wfs);
334                    wfs[0] =
335                        hFilter.getLPSynWaveForm(wfs[0],null);
336                    wfs[1] =
337                        vFilter.getHPSynWaveForm(wfs[1],null);
338                }
339                else if (subb_HH.l2Norm < 0f) {
340                    subb_HH.calcBasisWaveForms(wfs);
341                    wfs[0] =
342                        hFilter.getHPSynWaveForm(wfs[0],null);
343                    wfs[1] =
344                        vFilter.getHPSynWaveForm(wfs[1],null);
345                }
346                else {
347                    // There is an error! If all childs have
348                    // non-negative l2norm, then this node should have
349                    // non-negative l2norm
350                    throw new Error("You have found a bug in JJ2000!");
351                }
352            }
353            else {
354                // This is a leaf, just use diracs (null is
355                // equivalent to dirac)
356                wfs[0] = new float[1];
357                wfs[0][0] = 1.0f;
358                wfs[1] = new float[1];
359                wfs[1][0] = 1.0f;
360            }
361
362        }
363        else {
364            // This is an error! The calcBasisWaveForms() method is
365            // never called on an element with non-negative l2norm
366            throw new Error("You have found a bug in JJ2000!");
367        }
368    }
369
370    /**
371     * Assigns the given L2-norm to the first leaf that does not have
372     * an L2-norm value yet (i.e. l2norm is negative). The search is
373     * done recursively and in the same order as that of the
374     * calcBasisWaveForms() method, so that this method is used to
375     * assigne the l2norm of the previously computed waveforms.
376     *
377     * <P>This method can not be called on an element that ahs a
378     * non-negative value in l2Norm, since that means that we are
379     * done.
380     *
381     * @param l2n The L2-norm to assign.
382     *
383     *
384     * */
385    private void assignL2Norm(float l2n) {
386        if (l2Norm < 0) {
387            // We are not finished with this element yet
388            if (isNode) {
389                // We are on a node => search on childs
390                if (subb_LL.l2Norm < 0f) {
391                    subb_LL.assignL2Norm(l2n);
392                }
393                else if (subb_HL.l2Norm < 0f) {
394                    subb_HL.assignL2Norm(l2n);
395                }
396                else if (subb_LH.l2Norm < 0f) {
397                    subb_LH.assignL2Norm(l2n);
398                }
399                else if (subb_HH.l2Norm < 0f) {
400                    subb_HH.assignL2Norm(l2n);
401                    // If child now is done, we are done
402                    if (subb_HH.l2Norm >= 0f) {
403                        l2Norm = 0f; // We are on a node, any non-neg value OK
404                    }
405                }
406                else {
407                    // There is an error! If all childs have
408                    // non-negative l2norm, then this node should have
409                    // non-negative l2norm
410                    throw new Error("You have found a bug in JJ2000!");
411                }
412            }
413            else {
414                // This is a leaf, assign the L2-norm
415                l2Norm = l2n;
416            }
417
418        }
419        else {
420            // This is an error! The assignL2Norm() method is
421            // never called on an element with non-negative l2norm
422            throw new Error("You have found a bug in JJ2000!");
423        }
424    }
425
426
427    /**
428     * Calculates the L2-norm of the sythesis waveforms of every leaf
429     * in the tree. This method should only be called on the root
430     * element.
431     *
432     *
433     * */
434    private void calcL2Norms() {
435        int i;
436        float wfs[][] = new float[2][];
437        double acc;
438        float l2n;
439
440        // While we are not done on the root element, compute basis
441        // functions and assign L2-norm
442        while (l2Norm < 0f) {
443            calcBasisWaveForms(wfs);
444            // Compute line L2-norm, which is the product of the line
445            // and column L2-norms
446            acc = 0.0;
447            for (i=wfs[0].length-1; i>=0; i--) {
448                acc += wfs[0][i]*wfs[0][i];
449            }
450            l2n = (float) Math.sqrt(acc);
451            // Compute column L2-norm
452            acc = 0.0;
453            for (i=wfs[1].length-1; i>=0; i--) {
454                acc += wfs[1][i]*wfs[1][i];
455            }
456            l2n *= (float) Math.sqrt(acc);
457            // Release waveforms
458            wfs[0] = null;
459            wfs[1] = null;
460            // Assign the value
461            assignL2Norm(l2n);
462        }
463    }
464
465    /**
466     * This function returns the horizontal wavelet filter relevant to this
467     * subband
468     *
469     * @return The horizontal wavelet filter
470     *
471     *
472     */
473    public WaveletFilter getHorWFilter(){
474        return hFilter;
475    }
476
477    /**
478     * This function returns the vertical wavelet filter relevant to this
479     * subband
480     *
481     * @return The vertical wavelet filter
482     *
483     *
484     */
485    public WaveletFilter getVerWFilter(){
486        return hFilter;
487    }
488}