001/**
002 * $RCSfile: IntegerSpec.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:01:58 $
005 * $State: Exp $
006 *
007 * Class:                   IntegerSpec
008 *
009 * Description:             Holds specs corresponding to an Integer
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 *
044 *
045 *
046 */
047package jj2000.j2k;
048
049import jj2000.j2k.util.*;
050import jj2000.j2k.*;
051
052import java.util.*;
053import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
054
055/**
056 * This class extends ModuleSpec and is responsible of Integer
057 * specifications for each tile-component.
058 *
059 * @see ModuleSpec
060 * */
061public class IntegerSpec extends ModuleSpec{
062
063
064    /** The largest value of type int */
065    protected static int MAX_INT = Integer.MAX_VALUE;
066
067    /**
068     * Constructs a new 'IntegerSpec' for the specified number of
069     * tiles and components and with allowed type of
070     * specifications. This constructor is normally called at decoder
071     * side.
072     *
073     * @param nt The number of tiles
074     *
075     * @param nc The number of components
076     *
077     * @param type The type of allowed specifications
078     *
079     * */
080    public IntegerSpec(int nt,int nc,byte type){
081        super(nt,nc,type);
082    }
083
084    /**
085     * Constructs a new 'IntegerSpec' for the specified number of
086     * tiles and components, the allowed specifications type
087     * instance. This constructor is normally called at
088     * encoder side and parse arguments of specified option.
089     *
090     * @param nt The number of tiles
091     *
092     * @param nc The number of components
093     *
094     * @param type The allowed specifications type
095     *
096     * @param optName The name of the option to process
097     *
098     * */
099    public IntegerSpec(int nt, int nc, byte type, J2KImageWriteParamJava wp, String values,
100                         String defaultValue) {
101        super(nt,nc,type);
102
103        if(values==null){ // No parameter specified
104            try{
105                setDefault(new Integer(defaultValue));
106            }
107            catch(NumberFormatException e){
108                    throw new IllegalArgumentException("Non recognized value"+
109                                                       " for option -"+
110                                                       ": "+defaultValue);
111            }
112            return;
113        }
114
115        Integer value;
116
117        // Parse argument
118        StringTokenizer stk = new StringTokenizer(values);
119        String word; // current word
120        byte curSpecType = SPEC_DEF; // Specification type of the
121        // current parameter
122        boolean[] tileSpec = null; // Tiles concerned by the specification
123        boolean[] compSpec = null; // Components concerned by the specification
124
125        while(stk.hasMoreTokens()){
126            word = stk.nextToken();
127
128            switch(word.charAt(0)){
129            case 't': // Tiles specification
130                tileSpec = parseIdx(word,nTiles);
131                if(curSpecType==SPEC_COMP_DEF)
132                    curSpecType = SPEC_TILE_COMP;
133                else
134                    curSpecType = SPEC_TILE_DEF;
135                break;
136            case 'c': // Components specification
137                compSpec = parseIdx(word,nComp);
138                if(curSpecType==SPEC_TILE_DEF)
139                    curSpecType = SPEC_TILE_COMP;
140                else
141                    curSpecType = SPEC_COMP_DEF;
142                break;
143            default:
144                try{
145                    value = new Integer(word);
146                }
147                catch(NumberFormatException e){
148                    throw new IllegalArgumentException("Non recognized value"+
149                                                       " for option -: "+word);
150                }
151
152                if(curSpecType==SPEC_DEF){
153                    setDefault(value);
154                }
155                else if(curSpecType==SPEC_TILE_DEF){
156                    for(int i=tileSpec.length-1; i>=0; i--)
157                        if(tileSpec[i]){
158                            setTileDef(i,value);
159                        }
160                }
161                else if(curSpecType==SPEC_COMP_DEF){
162                    for(int i=compSpec.length-1; i>=0; i--)
163                        if(compSpec[i]){
164                            setCompDef(i,value);
165                        }
166                }
167                else{
168                    for(int i=tileSpec.length-1; i>=0; i--){
169                        for(int j=compSpec.length-1; j>=0 ; j--){
170                            if(tileSpec[i] && compSpec[j]){
171                                setTileCompVal(i,j,value);
172                            }
173                        }
174                    }
175                }
176
177                // Re-initialize
178                curSpecType = SPEC_DEF;
179                tileSpec = null;
180                compSpec = null;
181                break;
182            }
183        }
184
185        // Check that default value has been specified
186        if(getDefault()==null){
187            int ndefspec = 0;
188            for(int t=nt-1; t>=0; t--){
189                for(int c=nc-1; c>=0 ; c--){
190                    if(specValType[t][c] == SPEC_DEF){
191                        ndefspec++;
192                    }
193                }
194            }
195
196            // If some tile-component have received no specification, it takes
197            // the default value
198            if(ndefspec!=0){
199                try{
200                    setDefault(new Integer(defaultValue));
201                }
202                catch(NumberFormatException e){
203                    throw new IllegalArgumentException("Non recognized value"+
204                                                       " for option - : " + defaultValue);
205                }
206            }
207            else{
208                // All tile-component have been specified, takes the first
209                // tile-component value as default.
210                setDefault(getTileCompVal(0,0));
211                switch(specValType[0][0]){
212                case SPEC_TILE_DEF:
213                    for(int c=nc-1; c>=0; c--){
214                        if(specValType[0][c]==SPEC_TILE_DEF)
215                            specValType[0][c] = SPEC_DEF;
216                    }
217                    tileDef[0] = null;
218                    break;
219                case SPEC_COMP_DEF:
220                    for(int t=nt-1; t>=0; t--){
221                        if(specValType[t][0]==SPEC_COMP_DEF)
222                            specValType[t][0] = SPEC_DEF;
223                    }
224                    compDef[0] = null;
225                    break;
226                case SPEC_TILE_COMP:
227                    specValType[0][0] = SPEC_DEF;
228                    tileCompVal.put("t0c0",null);
229                    break;
230                }
231            }
232        }
233    }
234
235    /**
236     * Get the maximum value of each tile-component
237     *
238     * @return The maximum value
239     *
240     */
241    public int getMax(){
242        int max = ((Integer)def).intValue();
243        int tmp;
244
245        for(int t=0; t<nTiles; t++){
246            for(int c=0; c<nComp; c++){
247                tmp = ((Integer)getSpec(t,c)).intValue();
248                if(max<tmp)
249                    max = tmp;
250            }
251        }
252
253        return max;
254    }
255
256    /**
257     * Get the minimum value of each tile-component
258     *
259     * @return The minimum value
260     *
261     */
262    public int getMin(){
263        int min = ((Integer)def).intValue();
264        int tmp;
265
266        for(int t=0; t<nTiles; t++){
267            for(int c=0; c<nComp; c++){
268                tmp = ((Integer)getSpec(t,c)).intValue();
269                if(min>tmp)
270                    min = tmp;
271            }
272        }
273
274        return min;
275    }
276
277    /**
278     * Get the maximum value of each tile for specified component
279     *
280     * @param c The component index
281     *
282     * @return The maximum value
283     *
284     */
285    public int getMaxInComp(int c){
286        int max = 0;
287        int tmp;
288
289        for(int t=0; t<nTiles; t++){
290            tmp = ((Integer)getSpec(t,c)).intValue();
291            if(max<tmp)
292                max = tmp;
293        }
294
295        return max;
296    }
297
298    /**
299     * Get the minimum value of each tile for specified component
300     *
301     * @param c The component index
302     *
303     * @return The minimum value
304     *
305     */
306    public int getMinInComp(int c){
307        int min = MAX_INT; // Big value
308        int tmp;
309
310        for(int t=0; t<nTiles; t++){
311            tmp = ((Integer)getSpec(t,c)).intValue();
312            if(min>tmp)
313                min = tmp;
314        }
315
316        return min;
317    }
318
319    /**
320     * Get the maximum value of each component in specified tile
321     *
322     * @param t The tile index
323     *
324     * @return The maximum value
325     *
326     */
327    public int getMaxInTile(int t){
328        int max = 0;
329        int tmp;
330
331        for(int c=0; c<nComp; c++){
332            tmp = ((Integer)getSpec(t,c)).intValue();
333            if(max<tmp)
334                max = tmp;
335        }
336
337        return max;
338    }
339
340    /**
341     * Get the minimum value of each component in specified tile
342     *
343     * @param t The tile index
344     *
345     * @return The minimum value
346     *
347     */
348    public int getMinInTile(int t){
349        int min = MAX_INT; // Big value
350        int tmp;
351
352        for(int c=0; c<nComp; c++){
353            tmp = ((Integer)getSpec(t,c)).intValue();
354            if(min>tmp)
355                min = tmp;
356        }
357
358        return min;
359    }
360}
361