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