001/*
002 * $RCSfile: EntropyCoder.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:08 $
005 * $State: Exp $
006 *
007 * Class:                   EntropyCoder
008 *
009 * Description:             The abstract class for entropy encoders
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.entropy.encoder;
045
046import jj2000.j2k.StringSpec;
047import jj2000.j2k.entropy.CBlkSizeSpec;
048import jj2000.j2k.entropy.PrecinctSizeSpec;
049import jj2000.j2k.entropy.StdEntropyCoderOptions;
050import jj2000.j2k.image.ImgDataAdapter;
051import jj2000.j2k.quantization.quantizer.CBlkQuantDataSrcEnc;
052import jj2000.j2k.quantization.quantizer.Quantizer;
053import jj2000.j2k.wavelet.Subband;
054import jj2000.j2k.wavelet.analysis.SubbandAn;
055
056import com.github.jaiimageio.jpeg2000.impl.J2KImageWriteParamJava;
057/**
058 * This abstract class provides the general interface for block-based entropy
059 * encoders. The input to the entropy coder is the quantized wavelet
060 * coefficients, or codewords, represented in sign magnitude. The output is a
061 * compressed code-block with rate-distortion information.
062 *
063 * <P>The source of data for objects of this class are 'CBlkQuantDataSrcEnc'
064 * objects.
065 *
066 * <P>For more details on the sign magnitude representation used see the
067 * Quantizer class.
068 *
069 * <P>This class provides default implemenations for most of the methods
070 * (wherever it makes sense), under the assumption that the image and
071 * component dimensions, and the tiles, are not modifed by the entropy
072 * coder. If that is not the case for a particular implementation then the
073 * methods should be overriden.
074 *
075 * @see Quantizer
076 * @see CBlkQuantDataSrcEnc
077 * */
078public abstract class EntropyCoder extends ImgDataAdapter
079    implements CodedCBlkDataSrcEnc, StdEntropyCoderOptions {
080
081    /** The prefix for entropy coder options: 'C' */
082    public final static char OPT_PREFIX = 'C';
083
084    /** The list of parameters that is accepted for entropy coding. Options
085     * for entropy coding start with 'C'. */
086    private final static String [][] pinfo = {
087        {"Cblksiz", "[<tile-component idx>] <width> <height> "+
088         "[[<tile-component idx>] <width> <height>]",
089         "Specifies the maximum code-block size to use for tile-component. "+
090         "The maximum width and height is 1024, however the surface area "+
091         "(i.e. width x height) must not exceed 4096. The minimum width and "+
092         "height is 4.","64 64"},
093        {"Cbypass", "[<tile-component idx>] true|false"+
094         "[ [<tile-component idx>] true|false ...]",
095         "Uses the lazy coding mode with the entropy coder. This will bypass "+
096         "the MQ coder for some of the coding passes, where the distribution "+
097         "is often close to uniform. Since the MQ codeword will be "+
098         "terminated "+
099         "at least once per lazy pass, it is important to use an efficient "+
100         "termination algorithm, see the 'Cterm' option."+
101         "'true' enables, 'false' disables it.","false"},
102        {"CresetMQ", "[<tile-component idx>] true|false"+
103         "[ [<tile-component idx>] true|false ...]",
104         "If this is enabled the probability estimates of the MQ coder are "+
105         "reset after each arithmetically coded (i.e. non-lazy) coding pass. "+
106         "'true' enables, 'false' disables it.","false"},
107        {"Creg_term", "[<tile-component idx>] true|false"+
108         "[ [<tile-component idx>] true|false ...]",
109         "If this is enabled the codeword (raw or MQ) is terminated on a "+
110         "byte boundary after each coding pass. In this case it is important "+
111         "to use an efficient termination algorithm, see the 'Cterm' option. "+
112         "'true' enables, 'false' disables it.","false"},
113        {"Ccausal","[<tile-component idx>] true|false"+
114         "[ [<tile-component idx>] true|false ...]",
115         "Uses vertically stripe causal context formation. If this is "+
116         "enabled "+
117         "the context formation process in one stripe is independant of the "+
118         "next stripe (i.e. the one below it). 'true' "+
119         "enables, 'false' disables it.","false"},
120        {"Cseg_symbol","[<tile-component idx>] true|false"+
121         "[ [<tile-component idx>] true|false ...]",
122         "Inserts an error resilience segmentation symbol in the MQ "+
123         "codeword at the end of "+
124         "each bit-plane (cleanup pass). Decoders can use this "+
125         "information to detect and "+
126         "conceal errors.'true' enables, 'false' disables "+
127         "it.","false"},
128        {"Cterm", "[<tile-component idx>] near_opt|easy|predict|full"+
129         "[ [<tile-component idx>] near_opt|easy|predict|full ...]",
130         "Specifies the algorithm used to terminate the MQ codeword. "+
131         "The most efficient one is 'near_opt', which delivers a codeword "+
132         "which in almost all cases is the shortest possible. The 'easy' is "+
133         "a simpler algorithm that delivers a codeword length that is close "+
134         "to the previous one (in average 1 bit longer). The 'predict' is"+
135         " almost "+
136         "the same as the 'easy' but it leaves error resilient information "+
137         "on "+
138         "the spare least significant bits (in average 3.5 bits), which can "+
139         "be used by a decoder to detect errors. The 'full' algorithm "+
140         "performs a full flush of the MQ coder and is highly inefficient.\n"+
141         "It is important to use a good termination policy since the MQ "+
142         "codeword can be terminated quite often, specially if the 'Cbypass'"+
143         " or "+
144         "'Creg_term' options are enabled (in the normal case it would be "+
145         "terminated once per code-block, while if 'Creg_term' is specified "+
146         "it will be done almost 3 times per bit-plane in each code-block).",
147         "near_opt"},
148        {"Clen_calc","[<tile-component idx>] near_opt|lazy_good|lazy"+
149         "[ [<tile-component idx>] ...]",
150         "Specifies the algorithm to use in calculating the necessary MQ "+
151         "length for each decoding pass. The best one is 'near_opt', which "+
152         "performs a rather sophisticated calculation and provides the best "+
153         "results. The 'lazy_good' and 'lazy' are very simple algorithms "+
154         "that "+
155         "provide rather conservative results, 'lazy_good' one being "+
156         "slightly "+
157         "better. Do not change this option unless you want to experiment "+
158         "the effect of different length calculation algorithms.","near_opt"},
159        {"Cpp","[<tile-component idx>] <dim> <dim> [<dim> <dim>] " +
160         "[ [<tile-component idx>] ...]",
161         "Specifies precinct partition dimensions for tile-component. The "+
162         "first "+
163         "two values apply to the highest resolution and the following ones "+
164         "(if "+
165         "any) apply to the remaining resolutions in decreasing order. If "+
166         "less "+
167         "values than the number of decomposition levels are specified, "+
168         "then the "+
169         "last two values are used for the remaining resolutions.", null},
170    };
171
172    /** The source of quantized wavelet coefficients */
173    protected CBlkQuantDataSrcEnc src;
174
175    /**
176     * Initializes the source of quantized wavelet coefficients.
177     *
178     * @param src The source of quantized wavelet coefficients.
179     * */
180    public EntropyCoder(CBlkQuantDataSrcEnc src) {
181        super(src);
182        this.src = src;
183    }
184
185    /**
186     * Returns the code-block width for the specified tile and component.
187     *
188     * @param t The tile index
189     *
190     * @param c the component index
191     *
192     * @return The code-block width for the specified tile and component
193     * */
194    public abstract int getCBlkWidth(int t, int c);
195
196    /**
197     * Returns the code-block height for the specified tile and component.
198     *
199     * @param t The tile index
200     *
201     * @param c the component index
202     *
203     * @return The code-block height for the specified tile and component
204     * */
205    public abstract int getCBlkHeight(int t, int c);
206
207    /**
208     * Returns the reversibility of the tile-component data that is provided
209     * by the object.  Data is reversible when it is suitable for lossless and
210     * lossy-to-lossless compression.
211     *
212     * <P>Since entropy coders themselves are always reversible, it returns
213     * the reversibility of the data that comes from the 'CBlkQuantDataSrcEnc'
214     * source object (i.e. ROIScaler).
215     *
216     * @param t Tile index
217     *
218     * @param c Component index
219     *
220     * @return true is the data is reversible, false if not.
221     *
222     * @see jj2000.j2k.roi.encoder.ROIScaler
223     * */
224    public boolean isReversible(int t,int c) {
225        return src.isReversible(t,c);
226    }
227
228    /**
229     * Returns a reference to the root of subband tree structure representing
230     * the subband decomposition for the specified tile-component.
231     *
232     * @param t The index of the tile.
233     *
234     * @param c The index of the component.
235     *
236     * @return The root of the subband tree structure, see Subband.
237     *
238     * @see SubbandAn
239     *
240     * @see Subband
241     * */
242    public SubbandAn getAnSubbandTree(int t,int c) {
243        return src.getAnSubbandTree(t,c);
244    }
245
246    /**
247     * Returns the horizontal offset of the code-block partition. Allowable
248     * values are 0 and 1, nothing else.
249     * */
250    public int getCbULX() {
251        return src.getCbULX();
252    }
253
254    /**
255     * Returns the vertical offset of the code-block partition. Allowable
256     * values are 0 and 1, nothing else.
257     * */
258    public int getCbULY() {
259        return src.getCbULY();
260    }
261
262    /**
263     * Returns the parameters that are used in this class and
264     * implementing classes. It returns a 2D String array. Each of the
265     * 1D arrays is for a different option, and they have 3
266     * elements. The first element is the option name, the second one
267     * is the synopsis, the third one is a long description of what
268     * the parameter is and the fourth is its default value. The
269     * synopsis or description may be 'null', in which case it is
270     * assumed that there is no synopsis or description of the option,
271     * respectively. Null may be returned if no options are supported.
272     *
273     * @return the options name, their synopsis and their explanation,
274     * or null if no options are supported.
275     * */
276    public static String[][] getParameterInfo() {
277        return pinfo;
278    }
279
280    /**
281     * Creates a EntropyCoder object for the appropriate entropy coding
282     * parameters in the parameter list 'pl', and having 'src' as the source
283     * of quantized data.
284     *
285     * @param src The source of data to be entropy coded
286     *
287     * @param wp The parameter list (or options).
288     *
289     * @param cbks Code-block size specifications
290     *
291     * @param pss Precinct partition specifications
292     *
293     * @param bms By-pass mode specifications
294     *
295     * @param mqrs MQ-reset specifications
296     *
297     * @param rts Regular termination specifications
298     *
299     * @param css Causal stripes specifications
300     *
301     * @param sss Error resolution segment symbol use specifications
302     *
303     * @param lcs Length computation specifications
304     *
305     * @param tts Termination type specifications
306     *
307     * @exception IllegalArgumentException If an error occurs while parsing
308     * the options in 'pl'
309     * */
310    public static EntropyCoder createInstance(CBlkQuantDataSrcEnc src,
311                                              J2KImageWriteParamJava wp,
312                                              CBlkSizeSpec cblks,
313                                              PrecinctSizeSpec pss,
314                                              StringSpec bms,StringSpec mqrs,
315                                              StringSpec rts,StringSpec css,
316                                              StringSpec sss,StringSpec lcs,
317                                              StringSpec tts) {
318        // Check parameters
319        //pl.checkList(OPT_PREFIX,pl.toNameArray(pinfo));
320        return new StdEntropyCoder(src,cblks,pss,bms,mqrs,rts,css,sss,lcs,tts);
321    }
322}