001/*
002 * $RCSfile: QuantStepSizeSpec.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:17 $
005 * $State: Exp $
006 *
007 * Class:                   QuantStepSizeSpec
008 *
009 * Description:    Quantization base normalized step size specifications
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.quantization;
048
049import java.util.StringTokenizer;
050
051import jj2000.j2k.ModuleSpec;
052
053import com.github.jaiimageio.jpeg2000.impl.J2KImageWriteParamJava;
054
055/**
056 * This class extends ModuleSpec class in order to hold specifications about
057 * the quantization base normalized step size to use in each tile-component.
058 *
059 * @see ModuleSpec
060 * */
061public class QuantStepSizeSpec extends ModuleSpec {
062
063    private String defaultValue = "0.0078125";
064
065    /**
066     * Constructs an empty 'QuantStepSizeSpec' with specified number of
067     * tile and components. This constructor is called by the decoder.
068     *
069     * @param nt Number of tiles
070     *
071     * @param nc Number of components
072     *
073     * @param type the type of the specification module i.e. tile specific,
074     * component specific or both.
075     * */
076    public QuantStepSizeSpec(int nt, int nc, byte type){
077        super(nt, nc, type);
078    }
079
080    /**
081     * Constructs a new 'QuantStepSizeSpec' for the specified number of
082     * components and tiles and the arguments of "-Qstep" option.
083     *
084     * @param nt The number of tiles
085     *
086     * @param nc The number of components
087     *
088     * @param type the type of the specification module i.e. tile specific,
089     * component specific or both.
090     * */
091    public QuantStepSizeSpec(int nt, int nc, byte type, J2KImageWriteParamJava wp, String values){
092        super(nt, nc, type);
093
094        if(values==null){
095            // XXX: setDefault
096            setDefault(new Float(defaultValue));
097            //throw new IllegalArgumentException("Qstep option not specified");
098        }
099        specified = values;
100
101        // XXX: need change
102        String param = specified;
103        if (param == null)
104            param = defaultValue;
105
106        // Parse argument
107        StringTokenizer stk = new StringTokenizer(param);
108        String word; // current word
109        byte curSpecType = SPEC_DEF; // Specification type of the
110        // current parameter
111        boolean[] tileSpec = null; // Tiles concerned by the specification
112        boolean[] compSpec = null; // Components concerned by the specification
113        Float value; // value of the current step size
114
115        while(stk.hasMoreTokens()){
116            word = stk.nextToken().toLowerCase();
117
118            switch(word.charAt(0)){
119            case 't': // Tiles specification
120                tileSpec = parseIdx(word,nTiles);
121                if(curSpecType==SPEC_COMP_DEF)
122                    curSpecType = SPEC_TILE_COMP;
123                else
124                    curSpecType = SPEC_TILE_DEF;
125                break;
126            case 'c': // Components specification
127                compSpec = parseIdx(word,nComp);
128                if(curSpecType==SPEC_TILE_DEF)
129                    curSpecType = SPEC_TILE_COMP;
130                else
131                    curSpecType = SPEC_COMP_DEF;
132                break;
133            default: // Step size value
134                try{
135                    value = new Float(word);
136                }
137                catch(NumberFormatException e){
138                    throw new IllegalArgumentException("Bad parameter for "+
139                                                       "-Qstep option : "+
140                                                       word);
141                }
142
143                if (value.floatValue() <= 0.0f) {
144                    throw new IllegalArgumentException("Normalized base step "+
145                                                       "must be positive : "+
146                                                       value);
147                }
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                setDefault(new Float(defaultValue));
198            }
199            else{
200                // All tile-component have been specified, takes the first
201                // tile-component value as default.
202                setDefault(getTileCompVal(0,0));
203                switch(specValType[0][0]){
204                case SPEC_TILE_DEF:
205                    for(int c=nc-1; c>=0; c--){
206                        if(specValType[0][c]==SPEC_TILE_DEF)
207                            specValType[0][c] = SPEC_DEF;
208                    }
209                    tileDef[0] = null;
210                    break;
211                case SPEC_COMP_DEF:
212                    for(int t=nt-1; t>=0; t--){
213                        if(specValType[t][0]==SPEC_COMP_DEF)
214                            specValType[t][0] = SPEC_DEF;
215                    }
216                    compDef[0] = null;
217                    break;
218                case SPEC_TILE_COMP:
219                    specValType[0][0] = SPEC_DEF;
220                    tileCompVal.put("t0c0",null);
221                    break;
222                }
223            }
224        }
225   }
226
227}