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