001/*
002 * $RCSfile: GuardBitsSpec.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:17 $
005 * $State: Exp $
006 *
007 * Class:                   GuardBitsSpec
008 *
009 * Description:             Guard bits specifications
010 *
011 * COPYRIGHT:
012 *
013 * This software module was originally developed by Raphaël Grosbois and
014 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
015 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
016 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
017 * Centre France S.A) in the course of development of the JPEG2000
018 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
019 * software module is an implementation of a part of the JPEG 2000
020 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
021 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
022 * Partners) agree not to assert against ISO/IEC and users of the JPEG
023 * 2000 Standard (Users) any of their rights under the copyright, not
024 * including other intellectual property rights, for this software module
025 * with respect to the usage by ISO/IEC and Users of this software module
026 * or modifications thereof for use in hardware or software products
027 * claiming conformance to the JPEG 2000 Standard. Those intending to use
028 * this software module in hardware or software products are advised that
029 * their use may infringe existing patents. The original developers of
030 * this software module, JJ2000 Partners and ISO/IEC assume no liability
031 * for use of this software module or modifications thereof. No license
032 * or right to this software module is granted for non JPEG 2000 Standard
033 * conforming products. JJ2000 Partners have full right to use this
034 * software module for his/her own purpose, assign or donate this
035 * software module to any third party and to inhibit third parties from
036 * using this software module for non JPEG 2000 Standard conforming
037 * products. This copyright notice must be included in all copies or
038 * derivative works of this software module.
039 *
040 * Copyright (c) 1999/2000 JJ2000 Partners.
041 *
042 *
043 *
044 */
045package jj2000.j2k.quantization;
046
047import java.util.StringTokenizer;
048
049import jj2000.j2k.ModuleSpec;
050
051import com.github.jaiimageio.jpeg2000.impl.J2KImageWriteParamJava;
052/**
053 * This class extends ModuleSpec class in order to hold specifications about
054 * number of guard bits in each tile-component.
055 *
056 * @see ModuleSpec
057 * */
058public class GuardBitsSpec extends ModuleSpec{
059
060    private String defaultValue = "2";
061
062    /**
063     * Constructs an empty 'GuardBitsSpec' with specified number of tile and
064     * 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 GuardBitsSpec(int nt, int nc, byte type){
074        super(nt,nc,type);
075    }
076
077    /**
078     * Constructs a new 'GuardBitsSpec' for the specified number of components
079     * and tiles and the arguments of "-Qguard_bits" option.
080     *
081     * @param nt The number of tiles
082     *
083     * @param nc The number of components
084     *
085     * @param type the type of the specification module i.e. tile specific,
086     * component specific or both.
087     * */
088    public GuardBitsSpec(int nt, int nc, byte type, J2KImageWriteParamJava wp, String values){
089        super(nt,nc,type);
090
091
092        if(values==null){
093            setDefault(new Integer(defaultValue));
094            return;
095            //throw new IllegalArgumentException("Qguard_bits option not "+
096            //                                 "specified");
097        }
098
099        String param = values;
100        // Parse argument
101        StringTokenizer stk = new StringTokenizer(param);
102        String word; // current word
103        byte curSpecType = SPEC_DEF; // Specification type of the
104        // current parameter
105        boolean[] tileSpec = null; // Tiles concerned by the specification
106        boolean[] compSpec = null; // Components concerned by the specification
107        Integer value; // value of the guard bits
108
109        while(stk.hasMoreTokens()){
110            word = stk.nextToken().toLowerCase();
111
112            switch(word.charAt(0)){
113            case 't': // Tiles specification
114                tileSpec = parseIdx(word,nTiles);
115                if(curSpecType==SPEC_COMP_DEF)
116                    curSpecType = SPEC_TILE_COMP;
117                else
118                    curSpecType = SPEC_TILE_DEF;
119                break;
120            case 'c': // Components specification
121                compSpec = parseIdx(word,nComp);
122                if(curSpecType==SPEC_TILE_DEF)
123                    curSpecType = SPEC_TILE_COMP;
124                else
125                    curSpecType = SPEC_COMP_DEF;
126                break;
127            default: // Step size value
128                try{
129                    value = new Integer(word);
130                }
131                catch(NumberFormatException e){
132                    throw new IllegalArgumentException("Bad parameter for "+
133                                                       "-Qguard_bits option"+
134                                                       " : "+word);
135                }
136
137                if (value.floatValue() <= 0.0f) {
138                    throw new IllegalArgumentException("Guard bits value "+
139                                                       "must be positive : "+
140                                                       value);
141                }
142
143
144                if(curSpecType==SPEC_DEF){
145                    setDefault(value);
146                }
147                else if(curSpecType==SPEC_TILE_DEF){
148                    for(int i=tileSpec.length-1; i>=0; i--)
149                        if(tileSpec[i]){
150                            setTileDef(i,value);
151                        }
152                }
153                else if(curSpecType==SPEC_COMP_DEF){
154                    for(int i=compSpec.length-1; i>=0; i--)
155                        if(compSpec[i]){
156                            setCompDef(i,value);
157                        }
158                }
159                else{
160                    for(int i=tileSpec.length-1; i>=0; i--){
161                        for(int j=compSpec.length-1; j>=0 ; j--){
162                            if(tileSpec[i] && compSpec[j]){
163                                setTileCompVal(i,j,value);
164                            }
165                        }
166                    }
167                }
168
169                // Re-initialize
170                curSpecType = SPEC_DEF;
171                tileSpec = null;
172                compSpec = null;
173                break;
174            }
175        }
176
177        // Check that default value has been specified
178        if(getDefault()==null){
179            int ndefspec = 0;
180            for(int t=nt-1; t>=0; t--){
181                for(int c=nc-1; c>=0 ; c--){
182                    if(specValType[t][c] == SPEC_DEF){
183                        ndefspec++;
184                    }
185                }
186            }
187
188            // If some tile-component have received no specification, it takes
189            // the default value
190            if(ndefspec!=0){
191                setDefault(new Integer(defaultValue));
192            }
193            else{
194                // All tile-component have been specified, takes the first
195                // tile-component value as default.
196                setDefault(getTileCompVal(0,0));
197                switch(specValType[0][0]){
198                case SPEC_TILE_DEF:
199                    for(int c=nc-1; c>=0; c--){
200                        if(specValType[0][c]==SPEC_TILE_DEF)
201                            specValType[0][c] = SPEC_DEF;
202                    }
203                    tileDef[0] = null;
204                    break;
205                case SPEC_COMP_DEF:
206                    for(int t=nt-1; t>=0; t--){
207                        if(specValType[t][0]==SPEC_COMP_DEF)
208                            specValType[t][0] = SPEC_DEF;
209                    }
210                    compDef[0] = null;
211                    break;
212                case SPEC_TILE_COMP:
213                    specValType[0][0] = SPEC_DEF;
214                    tileCompVal.put("t0c0",null);
215                    break;
216                }
217            }
218        }
219   }
220
221}