001/*
002 * $RCSfile: ROIDeScaler.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:21 $
005 * $State: Exp $
006 *
007 *
008 * Class:                   ROIDeScaler
009 *
010 * Description:             The class taking care of de-scaling ROI coeffs.
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 * */
045package jj2000.j2k.roi;
046
047import jj2000.j2k.decoder.DecoderSpecs;
048import jj2000.j2k.image.DataBlk;
049import jj2000.j2k.quantization.dequantizer.CBlkQuantDataSrcDec;
050import jj2000.j2k.wavelet.synthesis.MultiResImgDataAdapter;
051import jj2000.j2k.wavelet.synthesis.SubbandSyn;
052
053import com.github.jaiimageio.jpeg2000.impl.J2KImageReadParamJava;
054
055/**
056 * This class takes care of the de-scaling of ROI coefficients. The de-scaler
057 * works on a tile basis and any mask that is generated is for the current
058 * mask only
059 *
060 * <P>Default implementations of the methods in 'MultiResImgData' are provided
061 * through the 'MultiResImgDataAdapter' abstract class.
062 *
063 * <P>Sign magnitude representation is used (instead of two's complement) for
064 * the output data. The most significant bit is used for the sign (0 if
065 * positive, 1 if negative). Then the magnitude of the quantized coefficient
066 * is stored in the next most significat bits. The most significant magnitude
067 * bit corresponds to the most significant bit-plane and so on.
068 * */
069public class ROIDeScaler extends MultiResImgDataAdapter
070    implements CBlkQuantDataSrcDec{
071
072    /** The MaxShiftSpec containing the scaling values for all tile-components
073     * */
074    private MaxShiftSpec mss;
075
076    /** The prefix for ROI decoder options: 'R' */
077    public final static char OPT_PREFIX = 'R';
078
079    /** The list of parameters that is accepted by the entropy decoders. They
080     * start with 'R'. */
081    private final static String [][] pinfo = {
082        { "Rno_roi",null,
083          "This argument makes sure that the no ROI de-scaling is performed. "+
084          "Decompression is done like there is no ROI in the image",null},
085    };
086
087    /** The entropy decoder from where to get the compressed data (the source)
088     * */
089    private CBlkQuantDataSrcDec src;
090
091    /**
092     * Constructor of the ROI descaler, takes EntropyDEcoder as source of data
093     * to de-scale.
094     *
095     * @param src The EntropyDecoder that is the source of data.
096     *
097     * @param mss The MaxShiftSpec containing the scaling values for all
098     * tile-components
099     * */
100    public ROIDeScaler(CBlkQuantDataSrcDec src, MaxShiftSpec mss){
101        super(src);
102        this.src=src;
103        this.mss=mss;
104    }
105
106    /**
107     * Returns the subband tree, for the specified tile-component. This method
108     * returns the root element of the subband tree structure, see Subband and
109     * SubbandSyn. The tree comprises all the available resolution levels.
110     *
111     * <P>The number of magnitude bits ('magBits' member variable) for each
112     * subband is not initialized.
113     *
114     * @param t The index of the tile, from 0 to T-1.
115     *
116     * @param c The index of the component, from 0 to C-1.
117     *
118     * @return The root of the tree structure.
119     * */
120    public SubbandSyn getSynSubbandTree(int t,int c) {
121        return src.getSynSubbandTree(t,c);
122    }
123
124    /**
125     * Returns the horizontal code-block partition origin. Allowable values
126     * are 0 and 1, nothing else.
127     * */
128    public int getCbULX() {
129        return src.getCbULX();
130    }
131
132    /**
133     * Returns the vertical code-block partition origin. Allowable values are
134     * 0 and 1, nothing else.
135     * */
136    public int getCbULY() {
137        return src.getCbULY();
138    }
139
140    /**
141     * Returns the parameters that are used in this class and implementing
142     * classes. It returns a 2D String array. Each of the 1D arrays is for a
143     * different option, and they have 3 elements. The first element is the
144     * option name, the second one is the synopsis and the third one is a long
145     * description of what the parameter is. The synopsis or description may
146     * be 'null', in which case it is assumed that there is no synopsis or
147     * description of the option, respectively. Null may be returned if no
148     * options are supported.
149     *
150     * @return the options name, their synopsis and their explanation, or null
151     * if no options are supported.
152     * */
153    public static String[][] getParameterInfo() {
154        return pinfo;
155    }
156
157    /**
158     * Returns the specified code-block in the current tile for the specified
159     * component, as a copy (see below).
160     *
161     * <P>The returned code-block may be progressive, which is indicated by
162     * the 'progressive' variable of the returned 'DataBlk' object. If a
163     * code-block is progressive it means that in a later request to this
164     * method for the same code-block it is possible to retrieve data which is
165     * a better approximation, since meanwhile more data to decode for the
166     * code-block could have been received. If the code-block is not
167     * progressive then later calls to this method for the same code-block
168     * will return the exact same data values.
169     *
170     * <P>The data returned by this method is always a copy of the internal
171     * data of this object, if any, and it can be modified "in place" without
172     * any problems after being returned. The 'offset' of the returned data is
173     * 0, and the 'scanw' is the same as the code-block width. See the
174     * 'DataBlk' class.
175     *
176     * <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object contain
177     * the coordinates of the top-left corner of the block, with respect to
178     * the tile, not the subband.
179     *
180     * @param c The component for which to return the next code-block.
181     *
182     * @param m The vertical index of the code-block to return, in the
183     * specified subband.
184     *
185     * @param n The horizontal index of the code-block to return, in the
186     * specified subband.
187     *
188     * @param sb The subband in which the code-block to return is.
189     *
190     * @param cblk If non-null this object will be used to return the new
191     * code-block. If null a new one will be allocated and returned. If the
192     * "data" array of the object is non-null it will be reused, if possible,
193     * to return the data.
194     *
195     * @return The next code-block in the current tile for component 'n', or
196     * null if all code-blocks for the current tile have been returned.
197     *
198     * @see DataBlk
199     * */
200    public DataBlk getCodeBlock(int c, int m, int n, SubbandSyn sb,
201                                  DataBlk cblk){
202        return getInternCodeBlock(c,m,n,sb,cblk);
203    }
204
205    /**
206     * Returns the specified code-block in the current tile for the specified
207     * component (as a reference or copy).
208     *
209     * <P>The returned code-block may be progressive, which is indicated by
210     * the 'progressive' variable of the returned 'DataBlk' object. If a
211     * code-block is progressive it means that in a later request to this
212     * method for the same code-block it is possible to retrieve data which is
213     * a better approximation, since meanwhile more data to decode for the
214     * code-block could have been received. If the code-block is not
215     * progressive then later calls to this method for the same code-block
216     * will return the exact same data values.
217     *
218     * <P>The data returned by this method can be the data in the internal
219     * buffer of this object, if any, and thus can not be modified by the
220     * caller. The 'offset' and 'scanw' of the returned data can be
221     * arbitrary. See the 'DataBlk' class.
222     *
223     * <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object contain
224     * the coordinates of the top-left corner of the block, with respect to
225     * the tile, not the subband.
226     *
227     * @param c The component for which to return the next code-block.
228     *
229     * @param m The vertical index of the code-block to return, in the
230     * specified subband.
231     *
232     * @param n The horizontal index of the code-block to return, in the
233     * specified subband.
234     *
235     * @param sb The subband in which the code-block to return is.
236     *
237     * @param cblk If non-null this object will be used to return the new
238     * code-block. If null a new one will be allocated and returned. If the
239     * "data" array of the object is non-null it will be reused, if possible,
240     * to return the data.
241     *
242     * @return The requested code-block in the current tile for component 'c'.
243     *
244     * @see DataBlk
245     * */
246    public DataBlk getInternCodeBlock(int c, int m, int n, SubbandSyn sb,
247                                        DataBlk cblk){
248        int mi,i,j,k,wrap;
249        int ulx, uly, w, h;
250        int[] data;                       // local copy of quantized data
251        int tmp;
252        int limit;
253
254        // Get data block from entropy decoder
255        cblk = src.getInternCodeBlock(c,m,n,sb,cblk);
256
257        // If there are no ROIs in the tile, Or if we already got all blocks
258        boolean noRoiInTile = false;
259        if(mss==null || mss.getTileCompVal(getTileIdx(),c)==null )
260            noRoiInTile = true;
261
262        if (noRoiInTile || cblk==null) {
263            return cblk;
264        }
265        data = (int[])cblk.getData();
266        ulx = cblk.ulx;
267        uly = cblk.uly;
268        w = cblk.w;
269        h = cblk.h;
270
271        // Scale coefficients according to magnitude. If the magnitude of a
272        // coefficient is lower than 2 pow 31-magbits then it is a background
273        // coeff and should be up-scaled
274        int boost = ((Integer) mss.getTileCompVal(getTileIdx(),c)).intValue();
275        int mask = ((1<<sb.magbits)-1)<<(31-sb.magbits);
276        int mask2 = (~mask)&0x7FFFFFFF;
277
278        wrap=cblk.scanw-w;
279        i=cblk.offset+cblk.scanw*(h-1)+w-1;
280        for(j=h;j>0;j--){
281            for(k=w;k>0;k--,i--){
282                tmp=data[i];
283                if((tmp & mask) == 0 ) { // BG
284                    data[i] = (tmp & 0x80000000) | (tmp << boost);
285                }
286                else { // ROI
287                    if ((tmp & mask2) != 0) {
288                        // decoded more than magbits bit-planes, set
289                        // quantization mid-interval approx. bit just after
290                        // the magbits.
291                        data[i] = (tmp&(~mask2)) | (1<<(30-sb.magbits));
292                    }
293                }
294            }
295            i-=wrap;
296        }
297        return cblk;
298    }
299
300    /**
301     * Creates a ROIDeScaler object. The information needed to create the
302     * object is the Entropy decoder used and the parameters.
303     *
304     * @param src The source of data that is to be descaled
305     *
306     * @param pl The parameter list (or options).
307     *
308     * @param decSpec The decoding specifications
309     *
310     * @exception IllegalArgumentException If an error occurs while parsing
311     * the options in 'pl'
312     * */
313    public static ROIDeScaler createInstance(CBlkQuantDataSrcDec src,
314                                             J2KImageReadParamJava j2krparam,
315                                             DecoderSpecs decSpec){
316        // Check if no_roi specified in command line or no roi signalled
317        // in bit stream
318        boolean noRoi = j2krparam.getNoROIDescaling();
319        if (noRoi || decSpec.rois == null) {
320            // no_roi specified in commandline!
321            return new ROIDeScaler(src,null);
322        }
323
324        return new ROIDeScaler(src, decSpec.rois );
325    }
326}