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 jj2000.j2k.util.*;
050import jj2000.j2k.*;
051
052import java.util.*;
053
054import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
055
056/**
057 * This class extends ModuleSpec class in order to hold specifications about
058 * the quantization base normalized step size to use in each tile-component.
059 *
060 * @see ModuleSpec
061 * */
062public class QuantStepSizeSpec extends ModuleSpec {
063
064    private String defaultValue = "0.0078125";
065
066    /**
067     * Constructs an empty 'QuantStepSizeSpec' with specified number of
068     * tile and components. This constructor is called by the decoder.
069     *
070     * @param nt Number of tiles
071     *
072     * @param nc Number of components
073     *
074     * @param type the type of the specification module i.e. tile specific,
075     * component specific or both.
076     * */
077    public QuantStepSizeSpec(int nt, int nc, byte type){
078        super(nt, nc, type);
079    }
080
081    /**
082     * Constructs a new 'QuantStepSizeSpec' for the specified number of
083     * components and tiles and the arguments of "-Qstep" option.
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    public QuantStepSizeSpec(int nt, int nc, byte type, J2KImageWriteParamJava wp, String values){
093        super(nt, nc, type);
094
095        if(values==null){
096            // XXX: setDefault
097            setDefault(new Float(defaultValue));
098            //throw new IllegalArgumentException("Qstep option not specified");
099        }
100        specified = values;
101
102        // XXX: need change
103        String param = specified;
104        if (param == null)
105            param = defaultValue;
106
107        // Parse argument
108        StringTokenizer stk = new StringTokenizer(param);
109        String word; // current word
110        byte curSpecType = SPEC_DEF; // Specification type of the
111        // current parameter
112        boolean[] tileSpec = null; // Tiles concerned by the specification
113        boolean[] compSpec = null; // Components concerned by the specification
114        Float value; // value of the current step size
115
116        while(stk.hasMoreTokens()){
117            word = stk.nextToken().toLowerCase();
118
119            switch(word.charAt(0)){
120            case 't': // Tiles specification
121                tileSpec = parseIdx(word,nTiles);
122                if(curSpecType==SPEC_COMP_DEF)
123                    curSpecType = SPEC_TILE_COMP;
124                else
125                    curSpecType = SPEC_TILE_DEF;
126                break;
127            case 'c': // Components specification
128                compSpec = parseIdx(word,nComp);
129                if(curSpecType==SPEC_TILE_DEF)
130                    curSpecType = SPEC_TILE_COMP;
131                else
132                    curSpecType = SPEC_COMP_DEF;
133                break;
134            default: // Step size value
135                try{
136                    value = new Float(word);
137                }
138                catch(NumberFormatException e){
139                    throw new IllegalArgumentException("Bad parameter for "+
140                                                       "-Qstep option : "+
141                                                       word);
142                }
143
144                if (value.floatValue() <= 0.0f) {
145                    throw new IllegalArgumentException("Normalized base step "+
146                                                       "must be positive : "+
147                                                       value);
148                }
149
150
151                if(curSpecType==SPEC_DEF){
152                    setDefault(value);
153                }
154                else if(curSpecType==SPEC_TILE_DEF){
155                    for(int i=tileSpec.length-1; i>=0; i--)
156                        if(tileSpec[i]){
157                            setTileDef(i,value);
158                        }
159                }
160                else if(curSpecType==SPEC_COMP_DEF){
161                    for(int i=compSpec.length-1; i>=0; i--)
162                        if(compSpec[i]){
163                            setCompDef(i,value);
164                        }
165                }
166                else{
167                    for(int i=tileSpec.length-1; i>=0; i--){
168                        for(int j=compSpec.length-1; j>=0 ; j--){
169                            if(tileSpec[i] && compSpec[j]){
170                                setTileCompVal(i,j,value);
171                            }
172                        }
173                    }
174                }
175
176                // Re-initialize
177                curSpecType = SPEC_DEF;
178                tileSpec = null;
179                compSpec = null;
180                break;
181            }
182        }
183
184        // Check that default value has been specified
185        if(getDefault()==null){
186            int ndefspec = 0;
187            for(int t=nt-1; t>=0; t--){
188                for(int c=nc-1; c>=0 ; c--){
189                    if(specValType[t][c] == SPEC_DEF){
190                        ndefspec++;
191                    }
192                }
193            }
194
195            // If some tile-component have received no specification, it takes
196            // the default value
197            if(ndefspec!=0){
198                setDefault(new Float(defaultValue));
199            }
200            else{
201                // All tile-component have been specified, takes the first
202                // tile-component value as default.
203                setDefault(getTileCompVal(0,0));
204                switch(specValType[0][0]){
205                case SPEC_TILE_DEF:
206                    for(int c=nc-1; c>=0; c--){
207                        if(specValType[0][c]==SPEC_TILE_DEF)
208                            specValType[0][c] = SPEC_DEF;
209                    }
210                    tileDef[0] = null;
211                    break;
212                case SPEC_COMP_DEF:
213                    for(int t=nt-1; t>=0; t--){
214                        if(specValType[t][0]==SPEC_COMP_DEF)
215                            specValType[t][0] = SPEC_DEF;
216                    }
217                    compDef[0] = null;
218                    break;
219                case SPEC_TILE_COMP:
220                    specValType[0][0] = SPEC_DEF;
221                    tileCompVal.put("t0c0",null);
222                    break;
223                }
224            }
225        }
226   }
227
228}