001/*
002 * $RCSfile: Quantizer.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:20 $
005 * $State: Exp $
006 *
007 * Class:                   Quantizer
008 *
009 * Description:             An abstract class for quantizers
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 *  */
044package jj2000.j2k.quantization.quantizer;
045
046import jj2000.j2k.codestream.writer.*;
047import jj2000.j2k.wavelet.analysis.*;
048import jj2000.j2k.quantization.*;
049import jj2000.j2k.wavelet.*;
050//import jj2000.j2k.encoder.*;
051import jj2000.j2k.image.*;
052import jj2000.j2k.util.*;
053
054import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
055
056/**
057 * This abstract class provides the general interface for quantizers. The
058 * input of a quantizer is the output of a wavelet transform. The output of
059 * the quantizer is the set of quantized wavelet coefficients represented in
060 * sign-magnitude notation (see below).
061 *
062 * <p>This class provides default implementation for most of the methods
063 * (wherever it makes sense), under the assumption that the image, component
064 * dimensions, and the tiles, are not modifed by the quantizer. If it is not
065 * the case for a particular implementation, then the methods should be
066 * overriden.</p>
067 *
068 * <p>Sign magnitude representation is used (instead of two's complement) for
069 * the output data. The most significant bit is used for the sign (0 if
070 * positive, 1 if negative). Then the magnitude of the quantized coefficient
071 * is stored in the next M most significat bits. The rest of the bits (least
072 * significant bits) can contain a fractional value of the quantized
073 * coefficient. This fractional value is not to be coded by the entropy
074 * coder. However, it can be used to compute rate-distortion measures with
075 * greater precision.</p>
076 *
077 * <p>The value of M is determined for each subband as the sum of the number
078 * of guard bits G and the nominal range of quantized wavelet coefficients in
079 * the corresponding subband (Rq), minus 1:</p>
080 *
081 * <p>M = G + Rq -1</p>
082 *
083 * <p>The value of G should be the same for all subbands. The value of Rq
084 * depends on the quantization step size, the nominal range of the component
085 * before the wavelet transform and the analysis gain of the subband (see
086 * Subband).</p>
087 *
088 * <p>The blocks of data that are requested should not cross subband
089 * boundaries.</p>
090 *
091 * <p>NOTE: At the moment only quantizers that implement the
092 * 'CBlkQuantDataSrcEnc' interface are supported.</p>
093 *
094 * @see Subband
095 * */
096public abstract class Quantizer extends ImgDataAdapter
097    implements CBlkQuantDataSrcEnc {
098
099    /** The prefix for quantizer options: 'Q' */
100    public final static char OPT_PREFIX = 'Q';
101
102    /** The list of parameters that is accepted for quantization. Options 
103     * for quantization start with 'Q'. */
104    private final static String [][] pinfo = {
105        { "Qtype", "[<tile-component idx>] <id> "+
106          "[ [<tile-component idx>] <id> ...]",
107          "Specifies which quantization type to use for specified "+
108          "tile-component. The default type is either 'reversible' or "+
109          "'expounded' depending on whether or not the '-lossless' option "+
110          " is specified.\n"+
111          "<tile-component idx> : see general note.\n"+
112          "<id>: Supported quantization types specification are : "+
113          "'reversible' "+
114          "(no quantization), 'derived' (derived quantization step size) and "+
115          "'expounded'.\n"+
116          "Example: -Qtype reversible or -Qtype t2,4-8 c2 reversible t9 "+
117          "derived.",null},
118        { "Qstep", "[<tile-component idx>] <bnss> "+
119          "[ [<tile-component idx>] <bnss> ...]",
120          "This option specifies the base normalized quantization step "+
121          "size (bnss) for tile-components. It is normalized to a "+
122          "dynamic range of 1 in the image domain. This parameter is "+
123          "ignored in reversible coding. The default value is '1/128'"+
124          " (i.e. 0.0078125).",
125          "0.0078125"},
126        { "Qguard_bits", "[<tile-component idx>] <gb> "+
127          "[ [<tile-component idx>] <gb> ...]",
128          "The number of bits used for each tile-component in the quantizer"+
129          " to avoid overflow (gb).","2"},
130    };
131
132    /** The source of wavelet transform coefficients */
133    protected CBlkWTDataSrc src;
134
135    /**
136     * Initializes the source of wavelet transform coefficients.
137     *
138     * @param src The source of wavelet transform coefficients.
139     * */
140    public Quantizer(CBlkWTDataSrc src) {
141        super(src);
142        this.src = src;
143    }
144
145    /**
146     * Returns the number of guard bits used by this quantizer in the
147     * given tile-component.
148     *
149     * @param t Tile index
150     *
151     * @param c Component index
152     *
153     * @return The number of guard bits
154     * */
155    public abstract int getNumGuardBits(int t,int c);
156
157    /**
158     * Returns true if the quantizer of given tile-component uses derived
159     * quantization step sizes.
160     *
161     * @param t Tile index
162     *
163     * @param c Component index
164     *
165     * @return True if derived quantization is used.
166     * */
167    public abstract boolean isDerived(int t,int c);
168
169    /**
170     * Calculates the parameters of the SubbandAn objects that depend on the
171     * Quantizer. The 'stepWMSE' field is calculated for each subband which is
172     * a leaf in the tree rooted at 'sb', for the specified component. The
173     * subband tree 'sb' must be the one for the component 'n'.
174     *
175     * @param sb The root of the subband tree.
176     *
177     * @param n The component index.
178     *
179     * @see SubbandAn#stepWMSE
180     * */
181    protected abstract void calcSbParams(SubbandAn sb, int n);
182
183    /**
184     * Returns a reference to the subband tree structure representing the
185     * subband decomposition for the specified tile-component.
186     *
187     * <P>This method gets the subband tree from the source and then
188     * calculates the magnitude bits for each leaf using the method
189     * calcSbParams().
190     *
191     * @param t The index of the tile.
192     *
193     * @param c The index of the component.
194     *
195     * @return The subband tree structure, see SubbandAn.
196     *
197     * @see SubbandAn
198     *
199     * @see Subband
200     *
201     * @see #calcSbParams
202     * */
203    public SubbandAn getAnSubbandTree(int t,int c) {
204        SubbandAn sbba;
205
206        // Ask for the wavelet tree of the source
207        sbba = src.getAnSubbandTree(t,c);
208        // Calculate the stepWMSE
209        calcSbParams(sbba,c);
210        return sbba;
211    }
212
213    /**
214     * Returns the horizontal offset of the code-block partition. Allowable
215     * values are 0 and 1, nothing else.
216     * */
217    public int getCbULX() {
218        return src.getCbULX();
219    }
220
221    /**
222     * Returns the vertical offset of the code-block partition. Allowable
223     * values are 0 and 1, nothing else.
224     * */
225    public int getCbULY() {
226        return src.getCbULY();
227    }
228
229    /**
230     * Returns the parameters that are used in this class and implementing
231     * classes. It returns a 2D String array. Each of the 1D arrays is for a
232     * different option, and they have 3 elements. The first element is the
233     * option name, the second one is the synopsis, the third one is a long
234     * description of what the parameter is and the fourth is its default
235     * value. The synopsis or description may be 'null', in which case it is
236     * assumed that there is no synopsis or description of the option,
237     * respectively. Null may be returned if no options are supported.
238     *
239     * @return the options name, their synopsis and their explanation, 
240     * or null if no options are supported.
241     * */
242    public static String[][] getParameterInfo() {
243        return pinfo;
244    }
245
246    /**
247     * Creates a Quantizer object for the appropriate type of quantization
248     * specified in the options in the parameter list 'pl', and having 'src'
249     * as the source of data to be quantized. The 'rev' flag indicates if the
250     * quantization should be reversible.
251     *
252     * NOTE: At the moment only sources of wavelet data that implement the
253     * 'CBlkWTDataSrc' interface are supported.
254     *
255     * @param src The source of data to be quantized
256     *
257     * @param encSpec Encoder specifications
258     *
259     * @exception IllegalArgumentException If an error occurs while parsing
260     * the options in 'pl'
261     * */
262    public static Quantizer createInstance(CBlkWTDataSrc src,
263                                           J2KImageWriteParamJava wp) {
264        // Instantiate quantizer
265        return new StdQuantizer(src,wp);
266    }
267
268    /** 
269     * Returns the maximum number of magnitude bits in any subband in the
270     * current tile.
271     *
272     * @param c the component number
273     *
274     * @return The maximum number of magnitude bits in all subbands of the
275     * current tile.
276     * */
277    public abstract int getMaxMagBits(int c);
278 
279}
280