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