001/*
002 * $RCSfile: StringSpec.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:01:59 $
005 * $State: Exp $
006 *
007 * Class:                   StringSpec
008 *
009 * Description:             String specification for an option
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 * */
044package jj2000.j2k;
045
046import jj2000.j2k.util.*;
047import jj2000.j2k.*;
048
049import java.util.*;
050
051import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
052/**
053 * This class extends ModuleSpec class in order to hold tile-component
054 * specifications using Strings.
055 *
056 * @see ModuleSpec
057 * */
058public class StringSpec extends ModuleSpec{
059
060    private String specified;
061
062    /**
063     * Constructs an empty 'StringSpec' with specified number of
064     * tile and components. This constructor is called by the decoder.
065     *
066     * @param nt Number of tiles
067     *
068     * @param nc Number of components
069     *
070     * @param type the type of the specification module i.e. tile specific,
071     * component specific or both.
072     * */
073    public StringSpec(int nt, int nc, byte type){
074        super(nt, nc, type);
075    }
076
077    /**
078     * Constructs a new 'StringSpec' for the specified number of
079     * components:tiles and the arguments of <tt>optName</tt>
080     * option. This constructor is called by the encoder. It also
081     * checks that the arguments belongs to the recognized arguments
082     * list.
083     *
084     * <P><u>Note:</u> The arguments must not start with 't' or 'c'
085     * since it is reserved for respectively tile and components
086     * indexes specification.
087     *
088     * @param nt The number of tiles
089     *
090     * @param nc The number of components
091     *
092     * @param type the type of the specification module i.e. tile specific,
093     * component specific or both.
094     *
095     * @param name of the option using boolean spec.
096     *
097     * @param list The list of all recognized argument in a String array
098     *
099     * */
100    public StringSpec(int nt, int nc, byte type, String defaultValue,
101                       String[] list, J2KImageWriteParamJava wp, String values){
102        super(nt,nc,type);
103        specified = values;
104
105        boolean recognized = false;
106
107        String param = values;
108
109        if(values==null){
110            for(int i=list.length-1; i>=0; i--)
111                if(defaultValue.equalsIgnoreCase(list[i]))
112                    recognized = true;
113            if(!recognized)
114                throw new IllegalArgumentException("Default parameter of "+
115                                                   "option - not"+
116                                                   " recognized: "+defaultValue);
117            setDefault(defaultValue);
118            return;
119        }
120
121        // Parse argument
122        StringTokenizer stk = new StringTokenizer(specified);
123        String word; // current word
124        byte curSpecType = SPEC_DEF; // Specification type of the
125        // current parameter
126        boolean[] tileSpec = null; // Tiles concerned by the
127        // specification
128        boolean[] compSpec = null; // Components concerned by the specification
129        Boolean value;
130
131        while(stk.hasMoreTokens()){
132            word = stk.nextToken();
133
134            if (word.matches("t[0-9]*")) {
135                tileSpec = parseIdx(word,nTiles);
136                if(curSpecType==SPEC_COMP_DEF){
137                    curSpecType = SPEC_TILE_COMP;
138                }
139                else{
140                    curSpecType = SPEC_TILE_DEF;
141                }
142            } else if (word.matches("c[0-9]*")) {
143                compSpec = parseIdx(word,nComp);
144                if(curSpecType==SPEC_TILE_DEF){
145                    curSpecType = SPEC_TILE_COMP;
146                }
147                else
148                    curSpecType = SPEC_COMP_DEF;
149            } else {
150                recognized = false;
151
152                for(int i=list.length-1; i>=0; i--)
153                    if(word.equalsIgnoreCase(list[i]))
154                        recognized = true;
155                if(!recognized)
156                    throw new IllegalArgumentException("Default parameter of "+
157                                                       "option not"+
158                                                       " recognized: "+word);
159
160                if(curSpecType==SPEC_DEF){
161                    setDefault(word);
162                }
163                else if(curSpecType==SPEC_TILE_DEF){
164                    for(int i=tileSpec.length-1; i>=0; i--)
165                        if(tileSpec[i]){
166                            setTileDef(i,word);
167                        }
168                }
169                else if(curSpecType==SPEC_COMP_DEF){
170                    for(int i=compSpec.length-1; i>=0; i--)
171                        if(compSpec[i]){
172                            setCompDef(i,word);
173                        }
174                }
175                else{
176                    for(int i=tileSpec.length-1; i>=0; i--){
177                        for(int j=compSpec.length-1; j>=0 ; j--){
178                            if(tileSpec[i] && compSpec[j]){
179                                setTileCompVal(i,j,word);
180                            }
181                        }
182                    }
183                }
184
185                // Re-initialize
186                curSpecType = SPEC_DEF;
187                tileSpec = null;
188                compSpec = null;
189            }
190        }
191
192        // Check that default value has been specified
193        if(getDefault()==null){
194            int ndefspec = 0;
195            for(int t=nt-1; t>=0; t--){
196                for(int c=nc-1; c>=0 ; c--){
197                    if(specValType[t][c] == SPEC_DEF){
198                        ndefspec++;
199                    }
200                }
201            }
202
203            // If some tile-component have received no specification, it takes
204            // the default value defined in ParameterList
205            if(ndefspec!=0){
206                param = defaultValue;
207                for(int i=list.length-1; i>=0; i--)
208                    if(param.equalsIgnoreCase(list[i]))
209                        recognized = true;
210                if(!recognized)
211                    throw new IllegalArgumentException("Default parameter of "+
212                                                       "option not"+
213                                                       " recognized: "+specified);
214                setDefault(param);
215            }
216            else{
217                // All tile-component have been specified, takes the first
218                // tile-component value as default.
219                setDefault(getSpec(0,0));
220                switch(specValType[0][0]){
221                case SPEC_TILE_DEF:
222                    for(int c=nc-1; c>=0; c--){
223                        if(specValType[0][c]==SPEC_TILE_DEF)
224                            specValType[0][c] = SPEC_DEF;
225                    }
226                    tileDef[0] = null;
227                    break;
228                case SPEC_COMP_DEF:
229                    for(int t=nt-1; t>=0; t--){
230                        if(specValType[t][0]==SPEC_COMP_DEF)
231                            specValType[t][0] = SPEC_DEF;
232                    }
233                    compDef[0] = null;
234                    break;
235                case SPEC_TILE_COMP:
236                    specValType[0][0] = SPEC_DEF;
237                    tileCompVal.put("t0c0",null);
238                    break;
239                }
240            }
241        }
242    }
243
244    public String getSpecified() {
245        return specified;
246    }
247}