001/*
002 * $RCSfile: ImgDataConverter.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:13 $
005 * $State: Exp $
006 *
007 * Interface:           ImgDataConverter
008 *
009 * Description:         The abstract class for classes that provide
010 *                      Image Data Convertres (int -> float, float->int).
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.image;
046
047import jj2000.j2k.image.*;
048import jj2000.j2k.*;
049
050/**
051 * This class is responsible of all data type conversions. It should be used,
052 * at encoder side, between Tiler and ForwardWT modules and, at decoder side,
053 * between InverseWT/CompDemixer and ImgWriter modules. The conversion is
054 * realized when a block of data is requested: if source and destination data
055 * type are the same one, it does nothing, else appropriate cast is done. All
056 * the methods of the 'ImgData' interface are implemented by the
057 * 'ImgDataAdapter' class that is the superclass of this one, so they don't
058 * need to be reimplemented by subclasses.
059 * */
060public class ImgDataConverter extends ImgDataAdapter implements BlkImgDataSrc {
061
062    /** The block used to request data from the source in the case that a
063     * conversion seems necessary. It can be either int or float at
064     * initialization time. It will be checked (and corrected if necessary) by
065     * the source whenever necessary */
066    private DataBlk srcBlk = new DataBlkInt();
067
068    /** The source of image data */
069    private BlkImgDataSrc src;
070
071    /** The number of fraction bits in the casted ints */
072    private int fp;
073
074    /**
075     * Constructs a new ImgDataConverter object that operates on the specified
076     * source of image data.
077     *
078     * @param imgSrc The source from where to get the data to be transformed
079     *
080     * @param fp The number of fraction bits in the casted ints
081     *
082     * @see BlkImgDataSrc
083     * */
084    public ImgDataConverter(BlkImgDataSrc imgSrc, int fp) {
085        super(imgSrc);
086        src = imgSrc;
087        this.fp=fp;
088    }
089
090    /**
091     * Constructs a new ImgDataConverter object that operates on the specified
092     * source of image data.
093     *
094     * @param imgSrc The source from where to get the data to be transformed
095     *
096     * @see BlkImgDataSrc
097     * */
098    public ImgDataConverter(BlkImgDataSrc imgSrc) {
099        super(imgSrc);
100        src = imgSrc;
101        fp = 0;
102    }
103
104    /**
105     * Returns the position of the fixed point in the specified
106     * component. This is the position of the least significant integral
107     * (i.e. non-fractional) bit, which is equivalent to the number of
108     * fractional bits. For instance, for fixed-point values with 2 fractional
109     * bits, 2 is returned. For floating-point data this value does not apply
110     * and 0 should be returned. Position 0 is the position of the least
111     * significant bit in the data.
112     *
113     * @param c The index of the component.
114     *
115     * @return The position of the fixed-point, which is the same as the
116     * number of fractional bits.
117     * */
118     public int getFixedPoint(int c){
119        return fp;
120     }
121
122    /**
123     * Returns, in the blk argument, a block of image data containing the
124     * specifed rectangular area, in the specified component, using the
125     * 'transfer type' specified in the block given as argument. The data is
126     * returned, as a copy of the internal data, therefore the returned data
127     * can be modified "in place".
128     *
129     * <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w'
130     * and 'h' members of the 'blk' argument, relative to the current
131     * tile. These members are not modified by this method. The 'offset' of
132     * the returned data is 0, and the 'scanw' is the same as the block's
133     * width. See the 'DataBlk' class.
134     *
135     * <P>This method, in general, is less efficient than the
136     * 'getInternCompData()' method since, in general, it copies the
137     * data. However if the array of returned data is to be modified by the
138     * caller then this method is preferable.
139     *
140     * <P>If the data array in 'blk' is 'null', then a new one is created. If
141     * the data array is not 'null' then it is reused, and it must be large
142     * enough to contain the block's data. Otherwise an 'ArrayStoreException'
143     * or an 'IndexOutOfBoundsException' is thrown by the Java system.
144     *
145     * <P>The returned data may have its 'progressive' attribute set. In this
146     * case the returned data is only an approximation of the "final" data.
147     *
148     * @param blk Its coordinates and dimensions specify the area to return,
149     * relative to the current tile. If it contains a non-null data array,
150     * then it must be large enough. If it contains a null data array a new
151     * one is created. Some fields in this object are modified to return the
152     * data.
153     *
154     * @param c The index of the component from which to get the data.
155     *
156     * @see #getInternCompData
157     * */
158    public DataBlk getCompData(DataBlk blk, int c){
159        return getData(blk,c,false);
160    }
161
162    /**
163     * Returns, in the blk argument, a block of image data containing the
164     * specifed rectangular area, in the specified component, using the
165     * 'transfer type' defined in the block given as argument. The data is
166     * returned, as a reference to the internal data, if any, instead of as a
167     * copy, therefore the returned data should not be modified.
168     *
169     * <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w'
170     * and 'h' members of the 'blk' argument, relative to the current
171     * tile. These members are not modified by this method. The 'offset' and
172     * 'scanw' of the returned data can be arbitrary. See the 'DataBlk' class.
173     *
174     * <P> If source data and expected data (blk) are using the same type,
175     * block returned without any modification. If not appropriate cast is
176     * used.
177     *
178     * <P>This method, in general, is more efficient than the 'getCompData()'
179     * method since it may not copy the data. However if the array of returned
180     * data is to be modified by the caller then the other method is probably
181     * preferable.
182     *
183     * <P>If the data array in <tt>blk</tt> is <tt>null</tt>, then a new one
184     * is created if necessary. The implementation of this interface may
185     * choose to return the same array or a new one, depending on what is more
186     * efficient. Therefore, the data array in <tt>blk</tt> prior to the
187     * method call should not be considered to contain the returned data, a
188     * new array may have been created. Instead, get the array from
189     * <tt>blk</tt> after the method has returned.
190     *
191     * <P>The returned data may have its 'progressive' attribute set. In this
192     * case the returned data is only an approximation of the "final" data.
193     *
194     * @param blk Its coordinates and dimensions specify the area to return,
195     * relative to the current tile. Some fields in this object are modified
196     * to return the data.
197     *
198     * @param c The index of the component from which to get the data.
199     *
200     * @return The requested DataBlk
201     *
202     * @see #getCompData
203     * */
204    public final DataBlk getInternCompData(DataBlk blk, int c){
205        return getData(blk,c,true);
206    }
207
208    /**
209     * Implements the 'getInternCompData()' and the 'getCompData()'
210     * methods. The 'intern' flag signals which of the two methods should run
211     * as.
212     *
213     * @param blk The data block to get.
214     *
215     * @param c The index of the component from which to get the data.
216     *
217     * @param intern If true behave as 'getInternCompData(). Otherwise behave
218     * as 'getCompData()'
219     *
220     * @return The requested data block
221     *
222     * @see #getInternCompData
223     *
224     * @see #getCompData
225     * */
226    private DataBlk getData(DataBlk blk, int c, boolean intern) {
227        DataBlk reqBlk;    // Reference to block used in request to source
228
229        // Keep request data type
230        int otype = blk.getDataType();
231
232        if (otype == srcBlk.getDataType()) {
233            // Probably requested type is same as source type
234            reqBlk = blk;
235        }
236        else {
237            // Probably requested type is not the same as source type
238            reqBlk = srcBlk;
239            // We need to copy requested coordinates and size
240            reqBlk.ulx = blk.ulx;
241            reqBlk.uly = blk.uly;
242            reqBlk.w = blk.w;
243            reqBlk.h = blk.h;
244        }
245
246        // Get source data block
247        if (intern) {
248            // We can use the intern variant
249            srcBlk = src.getInternCompData(reqBlk,c);
250        }
251        else {
252            // Do not use the intern variant. Note that this is not optimal
253            // since if we are going to convert below then we could have used
254            // the intern variant. But there is currently no way to know if we
255            // will need to do conversion or not before getting the data.
256            srcBlk = src.getCompData(reqBlk,c);
257        }
258
259        // Check if casting is needed
260        if(srcBlk.getDataType()==otype){
261            return srcBlk;
262        }
263
264        int i;
265        int k,kSrc,kmin;
266        float mult;
267        int w=srcBlk.w;
268        int h=srcBlk.h;
269
270        switch(otype){
271        case DataBlk.TYPE_FLOAT: // Cast INT -> FLOAT
272
273            float farr[];
274            int srcIArr[];
275
276            // Get data array from resulting blk
277            farr = (float[]) blk.getData();
278            if (farr == null || farr.length < w*h) {
279                farr = new float[w*h];
280                blk.setData(farr);
281            }
282
283            blk.scanw = srcBlk.w;
284            blk.offset = 0;
285            blk.progressive = srcBlk.progressive;
286            srcIArr=(int[]) srcBlk.getData();
287
288            // Cast data from source to blk
289            fp=src.getFixedPoint(c);
290            if(fp!=0){
291                mult=1.0f/(1<<fp);
292                for (i=h-1, k=w*h-1, kSrc =
293                         srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) {
294                    for (kmin = k-w; k>kmin; k--, kSrc--) {
295                        farr[k]=((srcIArr[kSrc]*mult));
296                    }
297                    // Jump to geggining of next line in source
298                    kSrc -= srcBlk.scanw - w;
299                }
300            }
301            else{
302                for (i=h-1, k=w*h-1, kSrc =
303                         srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) {
304                    for (kmin = k-w; k>kmin; k--, kSrc--) {
305                        farr[k]=((float)(srcIArr[kSrc]));
306                    }
307                    // Jump to geggining of next line in source
308                    kSrc -= srcBlk.scanw - w;
309                }
310            }
311            break; // End of cast INT-> FLOAT
312
313        case DataBlk.TYPE_INT: // cast FLOAT -> INT
314                int iarr[];
315                float srcFArr[];
316
317                // Get data array from resulting blk
318                iarr = (int[]) blk.getData();
319                if (iarr == null || iarr.length < w*h) {
320                    iarr = new int[w*h];
321                    blk.setData(iarr);
322                }
323                blk.scanw = srcBlk.w;
324                blk.offset = 0;
325                blk.progressive = srcBlk.progressive;
326                srcFArr=(float[]) srcBlk.getData();
327
328                // Cast data from source to blk
329                if(fp!=0){
330                    mult=(float)(1<<fp);
331                    for (i=h-1, k=w*h-1, kSrc =
332                             srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) {
333                        for (kmin = k-w; k>kmin; k--, kSrc--) {
334                            if (srcFArr[kSrc] > 0.0f) {
335                                iarr[k] = (int) (srcFArr[kSrc]*mult+0.5f);
336                            }
337                            else {
338                                iarr[k] = (int) (srcFArr[kSrc]*mult-0.5f);
339                            }
340                        }
341                        // Jump to geggining of next line in source
342                        kSrc -= srcBlk.scanw - w;
343                    }
344                }
345                else{
346                    for (i=h-1, k=w*h-1, kSrc =
347                             srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) {
348                        for (kmin = k-w; k>kmin; k--, kSrc--) {
349                            if (srcFArr[kSrc] > 0.0f) {
350                                iarr[k] = (int) (srcFArr[kSrc]+0.5f);
351                            }
352                            else {
353                                iarr[k] = (int) (srcFArr[kSrc]-0.5f);
354                            }
355                        }
356                        // Jump to geggining of next line in source
357                        kSrc -= srcBlk.scanw - w;
358                    }
359                }
360                break; // End cast FLOAT -> INT
361        default:
362            throw
363                new IllegalArgumentException("Only integer and float data "+
364                                             "are "+
365                                             "supported by JJ2000");
366        }
367        return blk;
368    }
369}
370
371