001: /**
002: * $RCSfile: IntegerSpec.java,v $
003: * $Revision: 1.1 $
004: * $Date: 2005/02/11 05:01:58 $
005: * $State: Exp $
006: *
007: * Class: IntegerSpec
008: *
009: * Description: Holds specs corresponding to an Integer
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: */package jj2000.j2k;
047:
048: import jj2000.j2k.util.*;
049: import jj2000.j2k.*;
050:
051: import java.util.*;
052: import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
053:
054: /**
055: * This class extends ModuleSpec and is responsible of Integer
056: * specifications for each tile-component.
057: *
058: * @see ModuleSpec
059: * */
060: public class IntegerSpec extends ModuleSpec {
061:
062: /** The largest value of type int */
063: protected static int MAX_INT = Integer.MAX_VALUE;
064:
065: /**
066: * Constructs a new 'IntegerSpec' for the specified number of
067: * tiles and components and with allowed type of
068: * specifications. This constructor is normally called at decoder
069: * side.
070: *
071: * @param nt The number of tiles
072: *
073: * @param nc The number of components
074: *
075: * @param type The type of allowed specifications
076: *
077: * */
078: public IntegerSpec(int nt, int nc, byte type) {
079: super (nt, nc, type);
080: }
081:
082: /**
083: * Constructs a new 'IntegerSpec' for the specified number of
084: * tiles and components, the allowed specifications type
085: * instance. This constructor is normally called at
086: * encoder side and parse arguments of specified option.
087: *
088: * @param nt The number of tiles
089: *
090: * @param nc The number of components
091: *
092: * @param type The allowed specifications type
093: *
094: * @param optName The name of the option to process
095: *
096: * */
097: public IntegerSpec(int nt, int nc, byte type,
098: J2KImageWriteParamJava wp, String values,
099: String defaultValue) {
100: super (nt, nc, type);
101:
102: if (values == null) { // No parameter specified
103: try {
104: setDefault(new Integer(defaultValue));
105: } catch (NumberFormatException e) {
106: throw new IllegalArgumentException(
107: "Non recognized value" + " for option -" + ": "
108: + defaultValue);
109: }
110: return;
111: }
112:
113: Integer value;
114:
115: // Parse argument
116: StringTokenizer stk = new StringTokenizer(values);
117: String word; // current word
118: byte curSpecType = SPEC_DEF; // Specification type of the
119: // current parameter
120: boolean[] tileSpec = null; // Tiles concerned by the specification
121: boolean[] compSpec = null; // Components concerned by the specification
122:
123: while (stk.hasMoreTokens()) {
124: word = stk.nextToken();
125:
126: switch (word.charAt(0)) {
127: case 't': // Tiles specification
128: tileSpec = parseIdx(word, nTiles);
129: if (curSpecType == SPEC_COMP_DEF)
130: curSpecType = SPEC_TILE_COMP;
131: else
132: curSpecType = SPEC_TILE_DEF;
133: break;
134: case 'c': // Components specification
135: compSpec = parseIdx(word, nComp);
136: if (curSpecType == SPEC_TILE_DEF)
137: curSpecType = SPEC_TILE_COMP;
138: else
139: curSpecType = SPEC_COMP_DEF;
140: break;
141: default:
142: try {
143: value = new Integer(word);
144: } catch (NumberFormatException e) {
145: throw new IllegalArgumentException(
146: "Non recognized value" + " for option -: "
147: + word);
148: }
149:
150: if (curSpecType == SPEC_DEF) {
151: setDefault(value);
152: } else if (curSpecType == SPEC_TILE_DEF) {
153: for (int i = tileSpec.length - 1; i >= 0; i--)
154: if (tileSpec[i]) {
155: setTileDef(i, value);
156: }
157: } else if (curSpecType == SPEC_COMP_DEF) {
158: for (int i = compSpec.length - 1; i >= 0; i--)
159: if (compSpec[i]) {
160: setCompDef(i, value);
161: }
162: } else {
163: for (int i = tileSpec.length - 1; i >= 0; i--) {
164: for (int j = compSpec.length - 1; j >= 0; j--) {
165: if (tileSpec[i] && compSpec[j]) {
166: setTileCompVal(i, j, value);
167: }
168: }
169: }
170: }
171:
172: // Re-initialize
173: curSpecType = SPEC_DEF;
174: tileSpec = null;
175: compSpec = null;
176: break;
177: }
178: }
179:
180: // Check that default value has been specified
181: if (getDefault() == null) {
182: int ndefspec = 0;
183: for (int t = nt - 1; t >= 0; t--) {
184: for (int c = nc - 1; c >= 0; c--) {
185: if (specValType[t][c] == SPEC_DEF) {
186: ndefspec++;
187: }
188: }
189: }
190:
191: // If some tile-component have received no specification, it takes
192: // the default value
193: if (ndefspec != 0) {
194: try {
195: setDefault(new Integer(defaultValue));
196: } catch (NumberFormatException e) {
197: throw new IllegalArgumentException(
198: "Non recognized value" + " for option - : "
199: + defaultValue);
200: }
201: } else {
202: // All tile-component have been specified, takes the first
203: // tile-component value as default.
204: setDefault(getTileCompVal(0, 0));
205: switch (specValType[0][0]) {
206: case SPEC_TILE_DEF:
207: for (int c = nc - 1; c >= 0; c--) {
208: if (specValType[0][c] == SPEC_TILE_DEF)
209: specValType[0][c] = SPEC_DEF;
210: }
211: tileDef[0] = null;
212: break;
213: case SPEC_COMP_DEF:
214: for (int t = nt - 1; t >= 0; t--) {
215: if (specValType[t][0] == SPEC_COMP_DEF)
216: specValType[t][0] = SPEC_DEF;
217: }
218: compDef[0] = null;
219: break;
220: case SPEC_TILE_COMP:
221: specValType[0][0] = SPEC_DEF;
222: tileCompVal.put("t0c0", null);
223: break;
224: }
225: }
226: }
227: }
228:
229: /**
230: * Get the maximum value of each tile-component
231: *
232: * @return The maximum value
233: *
234: */
235: public int getMax() {
236: int max = ((Integer) def).intValue();
237: int tmp;
238:
239: for (int t = 0; t < nTiles; t++) {
240: for (int c = 0; c < nComp; c++) {
241: tmp = ((Integer) getSpec(t, c)).intValue();
242: if (max < tmp)
243: max = tmp;
244: }
245: }
246:
247: return max;
248: }
249:
250: /**
251: * Get the minimum value of each tile-component
252: *
253: * @return The minimum value
254: *
255: */
256: public int getMin() {
257: int min = ((Integer) def).intValue();
258: int tmp;
259:
260: for (int t = 0; t < nTiles; t++) {
261: for (int c = 0; c < nComp; c++) {
262: tmp = ((Integer) getSpec(t, c)).intValue();
263: if (min > tmp)
264: min = tmp;
265: }
266: }
267:
268: return min;
269: }
270:
271: /**
272: * Get the maximum value of each tile for specified component
273: *
274: * @param c The component index
275: *
276: * @return The maximum value
277: *
278: */
279: public int getMaxInComp(int c) {
280: int max = 0;
281: int tmp;
282:
283: for (int t = 0; t < nTiles; t++) {
284: tmp = ((Integer) getSpec(t, c)).intValue();
285: if (max < tmp)
286: max = tmp;
287: }
288:
289: return max;
290: }
291:
292: /**
293: * Get the minimum value of each tile for specified component
294: *
295: * @param c The component index
296: *
297: * @return The minimum value
298: *
299: */
300: public int getMinInComp(int c) {
301: int min = MAX_INT; // Big value
302: int tmp;
303:
304: for (int t = 0; t < nTiles; t++) {
305: tmp = ((Integer) getSpec(t, c)).intValue();
306: if (min > tmp)
307: min = tmp;
308: }
309:
310: return min;
311: }
312:
313: /**
314: * Get the maximum value of each component in specified tile
315: *
316: * @param t The tile index
317: *
318: * @return The maximum value
319: *
320: */
321: public int getMaxInTile(int t) {
322: int max = 0;
323: int tmp;
324:
325: for (int c = 0; c < nComp; c++) {
326: tmp = ((Integer) getSpec(t, c)).intValue();
327: if (max < tmp)
328: max = tmp;
329: }
330:
331: return max;
332: }
333:
334: /**
335: * Get the minimum value of each component in specified tile
336: *
337: * @param t The tile index
338: *
339: * @return The minimum value
340: *
341: */
342: public int getMinInTile(int t) {
343: int min = MAX_INT; // Big value
344: int tmp;
345:
346: for (int c = 0; c < nComp; c++) {
347: tmp = ((Integer) getSpec(t, c)).intValue();
348: if (min > tmp)
349: min = tmp;
350: }
351:
352: return min;
353: }
354: }
|