001/*
002 * $RCSfile: AnWTFilter.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:28 $
005 * $State: Exp $
006 *
007 * Class:                   AnWTFilter
008 *
009 * Description:             The abstract class for all analysis wavelet filters
010 *
011 *
012 *
013 * COPYRIGHT:
014 *
015 * This software module was originally developed by Raphaël Grosbois and
016 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
017 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
018 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
019 * Centre France S.A) in the course of development of the JPEG2000
020 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
021 * software module is an implementation of a part of the JPEG 2000
022 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
023 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
024 * Partners) agree not to assert against ISO/IEC and users of the JPEG
025 * 2000 Standard (Users) any of their rights under the copyright, not
026 * including other intellectual property rights, for this software module
027 * with respect to the usage by ISO/IEC and Users of this software module
028 * or modifications thereof for use in hardware or software products
029 * claiming conformance to the JPEG 2000 Standard. Those intending to use
030 * this software module in hardware or software products are advised that
031 * their use may infringe existing patents. The original developers of
032 * this software module, JJ2000 Partners and ISO/IEC assume no liability
033 * for use of this software module or modifications thereof. No license
034 * or right to this software module is granted for non JPEG 2000 Standard
035 * conforming products. JJ2000 Partners have full right to use this
036 * software module for his/her own purpose, assign or donate this
037 * software module to any third party and to inhibit third parties from
038 * using this software module for non JPEG 2000 Standard conforming
039 * products. This copyright notice must be included in all copies or
040 * derivative works of this software module.
041 *
042 * Copyright (c) 1999/2000 JJ2000 Partners.
043 *
044 *
045 *
046 */
047package jj2000.j2k.wavelet.analysis;
048
049import jj2000.j2k.codestream.writer.*;
050import jj2000.j2k.wavelet.*;
051import jj2000.j2k.image.*;
052import jj2000.j2k.util.*;
053
054import java.util.*;
055import java.io.*;
056
057/**
058 * This abstract class defines the methods of all analysis wavelet
059 * filters. Specialized abstract classes that work on particular data
060 * types (int, float) provide more specific method calls while
061 * retaining the generality of this one. See the AnWTFilterInt
062 * and AnWTFilterFloat classes. Implementations of analysis
063 * filters should inherit from one of those classes.
064 *
065 * <P>All analysis wavelet filters should follow the following conventions:
066 *
067 * <P>- The first sample to filter is the low-pass one. As a
068 * consequence, if the input signal is of odd-length then the low-pass
069 * output signal is one sample longer than the high-pass output
070 * one. Therefore, if the length of input signal is N, the low-pass
071 * output signal is of length N/2 if N is even and N/2+1/2 if N is
072 * odd, while the high-pass output signal is of length N/2 if N is
073 * even and N/2-1/2 if N is odd.
074 *
075 * <P>- The normalization is 1 for the DC gain and 2 for the Nyquist
076 * gain (Type I normalization), for both reversible and non-reversible
077 * filters.
078 *
079 * <P>If the length of input signal is N, the low-pass output signal
080 * is of length N/2 if N is even and N/2+1/2 if N is odd, while the
081 * high-pass output sample is of length N/2 if N is even and N/2-1/2
082 * if N is odd.
083 *
084 * <P>The analyze method may seem very complicated, but is designed to
085 * minimize the amount of data copying and redundant calculations when
086 * used for block-based or line-based wavelet transform
087 * implementations, while being applicable to full-frame transforms as
088 * well.
089 *
090 * <P>All filters should implement the equals() method of the Object
091 * class. The call x.equals(y) should test if the 'x' and 'y' filters are the
092 * same or not, in what concerns the bit stream header syntax (two filters are
093 * the same if the same filter code should be output to the bit stream).
094 *
095 * @see AnWTFilterInt
096 *
097 * @see AnWTFilterFloat
098 * */
099public abstract class AnWTFilter implements WaveletFilter{
100
101    /** The prefix for wavelet filter options: 'F' */
102    public final static char OPT_PREFIX = 'F';
103
104    /** The list of parameters that is accepted for wavelet filters. Options
105     * for wavelet filters start with a 'F'. */
106    private final static String [][] pinfo = {
107        { "Ffilters", "[<tile-component idx>] <id> "+
108          "[ [<tile-component idx>] <id> ...]",
109          "Specifies which filters to use for specified tile-component.\n"+
110          "<tile-component idx>: see general note\n"+
111          "<id>: ',' separates horizontal and vertical filters, ':' separates"+
112          " decomposition levels filters. JPEG 2000 part I only supports w5x3"+
113          " and w9x7 filters.",null},
114    };
115
116    /**
117     * Filters the input signal by this analysis filter, decomposing
118     * it in a low-pass and a high-pass signal. This method performs
119     * the filtering and the subsampling with the low pass first
120     * filtering convention.
121     *
122     * <P>The input signal resides in the inSig array. The index of
123     * the first sample to filter (i.e. that will generate the first
124     * low-pass output sample) is given by inOff. The number of
125     * samples to filter is given by inLen. This array must be of the
126     * same type as the one for which the particular implementation
127     * works with (which is returned by the getDataType() method).
128     *
129     * <P>The input signal can be interleaved with other signals in
130     * the same inSig array, and this is determined by the inStep
131     * argument. This means that the first sample of the input signal
132     * is inSig[inOff], the second is inSig[inOff+inStep], the third
133     * is inSig[inOff+2*inStep], and so on. Therefore if inStep is 1
134     * there is no interleaving. This feature allows to filter columns
135     * of a 2-D signal, when it is stored in a line by line order in
136     * inSig, without having to copy the data, in this case the inStep
137     * argument should be the line width.
138     *
139     * <P>This method also allows to apply the analysis wavelet filter
140     * by parts in the input signal using an overlap and thus
141     * producing the same coefficients at the output. The tailOvrlp
142     * argument specifies how many samples in the input signal, before
143     * the first one to be filtered, can be used for overlap. Then,
144     * the filter instead of extending the input signal will use those
145     * samples to calculate the first output samples. The argument
146     * tailOvrlp can be 0 for no overlap, or some value that provides
147     * partial or full overlap. There should be enough samples in the
148     * input signal, before the first sample to be filtered, to
149     * support the overlap. The headOvrlp provides the same
150     * functionality but at the end of the input signal. The inStep
151     * argument also applies to samples used for overlap. This overlap
152     * feature can be used for line-based wavelet transforms (in which
153     * case it will only be used when filtering the columns) or for
154     * overlapping block-based wavelet transforms (in which case it
155     * will be used when filtering lines and columns).
156     *
157     * <P>The low-pass output signal is placed in the lowSig
158     * array. The lowOff and lowStep arguments are analogous to the
159     * inOff and inStep ones, but they apply to the lowSig array. The
160     * lowSig array must be long enough to hold the low-pass output
161     * signal.
162     *
163     * <P>The high-pass output signal is placed in the highSig
164     * array. The highOff and highStep arguments are analogous to the
165     * inOff and inStep ones, but they apply to the highSig array. The
166     * highSig array must be long enough to hold the high-pass output
167     * signal.
168     *
169     * @param inSig This is the array that contains the input
170     * signal. It must be of the correct type (e.g., it must be int[]
171     * if getDataType() returns TYPE_INT).
172     *
173     * @param inOff This is the index in inSig of the first sample to
174     * filter.
175     *
176     * @param inLen This is the number of samples in the input signal
177     * to filter.
178     *
179     * @param inStep This is the step, or interleave factor, of the
180     * input signal samples in the inSig array. See above.
181     *
182     * @param tailOvrlp This is the number of samples in the input
183     * signal before the first sample to filter that can be used for
184     * overlap. See above.
185     *
186     * @param headOvrlp This is the number of samples in the input
187     * signal after the last sample to filter that can be used for
188     * overlap. See above.
189     *
190     * @param lowSig This is the array where the low-pass output
191     * signal is placed. It must be of the same type as inSig and it
192     * should be long enough to contain the output signal.
193     *
194     * @param lowOff This is the index in lowSig of the element where
195     * to put the first low-pass output sample.
196     *
197     * @param lowStep This is the step, or interleave factor, of the
198     * low-pass output samples in the lowSig array. See above.
199     *
200     * @param highSig This is the array where the high-pass output
201     * signal is placed. It must be of the same type as inSig and it
202     * should be long enough to contain the output signal.
203     *
204     * @param highOff This is the index in highSig of the element where
205     * to put the first high-pass output sample.
206     *
207     * @param highStep This is the step, or interleave factor, of the
208     * high-pass output samples in the highSig array. See above.
209     *
210     * @see WaveletFilter#getDataType
211     *
212     *
213     *
214     *
215     * */
216    public abstract
217        void analyze_lpf(Object inSig, int inOff, int inLen, int inStep,
218                     Object lowSig, int lowOff, int lowStep,
219                     Object highSig, int highOff, int highStep);
220
221    /**
222     * Filters the input signal by this analysis filter, decomposing
223     * it in a low-pass and a high-pass signal. This method performs
224     * the filtering and the subsampling with the high pass first filtering
225     * convention.
226     *
227     * <P>The input signal resides in the inSig array. The index of
228     * the first sample to filter (i.e. that will generate the first
229     * high-pass output sample) is given by inOff. The number of
230     * samples to filter is given by inLen. This array must be of the
231     * same type as the one for which the particular implementation
232     * works with (which is returned by the getDataType() method).
233     *
234     * <P>The input signal can be interleaved with other signals in
235     * the same inSig array, and this is determined by the inStep
236     * argument. This means that the first sample of the input signal
237     * is inSig[inOff], the second is inSig[inOff+inStep], the third
238     * is inSig[inOff+2*inStep], and so on. Therefore if inStep is 1
239     * there is no interleaving. This feature allows to filter columns
240     * of a 2-D signal, when it is stored in a line by line order in
241     * inSig, without having to copy the data, in this case the inStep
242     * argument should be the line width.
243     *
244     * <P>The low-pass output signal is placed in the lowSig
245     * array. The lowOff and lowStep arguments are analogous to the
246     * inOff and inStep ones, but they apply to the lowSig array. The
247     * lowSig array must be long enough to hold the low-pass output
248     * signal.
249     *
250     * <P>The high-pass output signal is placed in the highSig
251     * array. The highOff and highStep arguments are analogous to the
252     * inOff and inStep ones, but they apply to the highSig array. The
253     * highSig array must be long enough to hold the high-pass output
254     * signal.
255     *
256     * @param inSig This is the array that contains the input
257     * signal. It must be of the correct type (e.g., it must be int[]
258     * if getDataType() returns TYPE_INT).
259     *
260     * @param inOff This is the index in inSig of the first sample to
261     * filter.
262     *
263     * @param inLen This is the number of samples in the input signal
264     * to filter.
265     *
266     * @param inStep This is the step, or interleave factor, of the
267     * input signal samples in the inSig array. See above.
268     *
269     * @param lowSig This is the array where the low-pass output
270     * signal is placed. It must be of the same type as inSig and it
271     * should be long enough to contain the output signal.
272     *
273     * @param lowOff This is the index in lowSig of the element where
274     * to put the first low-pass output sample.
275     *
276     * @param lowStep This is the step, or interleave factor, of the
277     * low-pass output samples in the lowSig array. See above.
278     *
279     * @param highSig This is the array where the high-pass output
280     * signal is placed. It must be of the same type as inSig and it
281     * should be long enough to contain the output signal.
282     *
283     * @param highOff This is the index in highSig of the element where
284     * to put the first high-pass output sample.
285     *
286     * @param highStep This is the step, or interleave factor, of the
287     * high-pass output samples in the highSig array. See above.
288     *
289     * @see WaveletFilter#getDataType
290     *
291     *
292     *
293     *
294     * */
295    public abstract
296        void analyze_hpf(Object inSig, int inOff, int inLen, int inStep,
297                     Object lowSig, int lowOff, int lowStep,
298                     Object highSig, int highOff, int highStep);
299
300    /**
301     * Returns the time-reversed low-pass synthesis waveform of the
302     * filter, which is the low-pass filter. This is the time-reversed
303     * impulse response of the low-pass synthesis filter. It is used
304     * to calculate the L2-norm of the synthesis basis functions for a
305     * particular subband (also called energy weight).
306     *
307     * <P>The returned array may not be modified (i.e. a reference to
308     * the internal array may be returned by the implementation of
309     * this method).
310     *
311     * @return The time-reversed low-pass synthesis waveform of the
312     * filter.
313     *
314     *
315     * */
316    public abstract float[] getLPSynthesisFilter();
317
318    /**
319     * Returns the time-reversed high-pass synthesis waveform of the
320     * filter, which is the high-pass filter. This is the
321     * time-reversed impulse response of the high-pass synthesis
322     * filter. It is used to calculate the L2-norm of the synthesis
323     * basis functions for a particular subband (also called energy
324     * weight).
325     *
326     * <P>The returned array may not be modified (i.e. a reference to
327     * the internal array may be returned by the implementation of
328     * this method).
329     *
330     * @return The time-reversed high-pass synthesis waveform of the
331     * filter.
332     *
333     *
334     * */
335    public abstract float[] getHPSynthesisFilter();
336
337    /**
338     * Returns the equivalent low-pass synthesis waveform of a cascade
339     * of filters, given the syhthesis waveform of the previous
340     * stage. This is the result of upsampling 'in' by 2, and
341     * concolving it with the low-pass synthesis waveform of the
342     * filter. The length of the returned signal is 2*in_l+lp_l-2,
343     * where in_l is the length of 'in' and 'lp_l' is the lengthg of
344     * the low-pass synthesis filter.
345     *
346     * <P>The length of the low-pass synthesis filter is
347     * getSynLowNegSupport()+getSynLowPosSupport().
348     *
349     * @param in The synthesis waveform of the previous stage.
350     *
351     * @param out If non-null this array is used to store the
352     * resulting signal. It must be long enough, or an
353     * IndexOutOfBoundsException is thrown.
354     *
355     * @see #getSynLowNegSupport
356     *
357     * @see #getSynLowPosSupport
358     *
359     *
360     * */
361    public float[] getLPSynWaveForm(float in[], float out[]) {
362        return upsampleAndConvolve(in,getLPSynthesisFilter(),out);
363    }
364
365    /**
366     * Returns the equivalent high-pass synthesis waveform of a
367     * cascade of filters, given the syhthesis waveform of the
368     * previous stage. This is the result of upsampling 'in' by 2, and
369     * concolving it with the high-pass synthesis waveform of the
370     * filter. The length of the returned signal is 2*in_l+hp_l-2,
371     * where in_l is the length of 'in' and 'hp_l' is the lengthg of
372     * the high-pass synthesis filter.
373     *
374     * <P>The length of the high-pass synthesis filter is
375     * getSynHighNegSupport()+getSynHighPosSupport().
376     *
377     * @param in The synthesis waveform of the previous stage.
378     *
379     * @param out If non-null this array is used to store the
380     * resulting signal. It must be long enough, or an
381     * IndexOutOfBoundsException is thrown.
382     *
383     * @see #getSynHighNegSupport
384     *
385     * @see #getSynHighPosSupport
386     *
387     *
388     * */
389    public float[] getHPSynWaveForm(float in[], float out[]) {
390        return upsampleAndConvolve(in,getHPSynthesisFilter(),out);
391    }
392
393    /**
394     * Returns the signal resulting of upsampling (by 2) the input
395     * signal 'in' and then convolving it with the time-reversed
396     * signal 'wf'. The returned signal is of length l_in*2+l_wf-2,
397     * where l_in is the length of 'in', and l_wf is the length of
398     * 'wf'.
399     *
400     * <P>The 'wf' signal has to be already time-reversed, therefore
401     * only a dot-product is performed (instead of a
402     * convolution). This is equivalent to convolving with the
403     * non-time-reversed 'wf' signal.
404     *
405     * @param in The signal to upsample and filter. If null it is
406     * considered to be a dirac.
407     *
408     * @param wf The time-reversed impulse response used for
409     * filtering.
410     *
411     * @param out If non-null this array is used to store the
412     * resulting signal, it must be of length in.length*2+wf.length-2
413     * at least. An IndexOutOfBoundsException is thrown if this is not
414     * the case.
415     *
416     * @return The resulting signal, of length in.length*2+wf.length-2
417     *
418     *
419     * */
420    private static
421        float[] upsampleAndConvolve(float in[], float wf[], float out[]) {
422        // NOTE: the effective length of the signal 'in' upsampled by
423        // 2 is 2*in.length-1 (not 2*in.length), so the resulting signal
424        // (after convolution) is of length 2*in.length-1+wf.length-1,
425        // which is 2*in.length+wf.length-2
426
427        int i,k,j;
428        float tmp;
429        int maxi,maxk;
430
431        // If in null, then simulate dirac
432        if (in == null) {
433            in = new float[1];
434            in[0] = 1.0f;
435        }
436
437        // Get output buffer if necessary
438        if (out == null) {
439            out = new float[in.length*2+wf.length-2];
440        }
441        // Convolve the signals
442        for (i=0, maxi=in.length*2+wf.length-2; i<maxi; i++) {
443            tmp = 0.0f;
444
445            // Calculate limits of loop below
446            k = (i-wf.length+2)/2;
447            if (k<0) k = 0;
448            maxk = i/2+1;
449            if (maxk > in.length) maxk = in.length;
450
451            // Calculate dot-product with upsampling of 'in' by 2.
452            for (j = 2*k-i+wf.length-1; k<maxk; k++, j+=2) {
453                tmp += in[k]*wf[j];
454            }
455            // Store result
456            out[i] = tmp;
457        }
458
459        return out;
460    }
461
462    /**
463     * Returns the type of filter used according to the FilterTypes
464     * interface.
465     *
466     * @see FilterTypes
467     *
468     * @return The filter type.
469     *
470     */
471    public abstract int getFilterType();
472
473    /**
474     * Returns the parameters that are used in this class and
475     * implementing classes. It returns a 2D String array. Each of the
476     * 1D arrays is for a different option, and they have 3
477     * elements. The first element is the option name, the second one
478     * is the synopsis, the third one is a long description of what
479     * the parameter is and the fourth is its default value. The
480     * synopsis or description may be 'null', in which case it is
481     * assumed that there is no synopsis or description of the option,
482     * respectively. Null may be returned if no options are supported.
483     *
484     * @return the options name, their synopsis and their explanation,
485     * or null if no options are supported.
486     *
487     *
488     * */
489    public static String[][] getParameterInfo() {
490        return pinfo;
491    }
492
493}