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