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